-
Notifications
You must be signed in to change notification settings - Fork 131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rails model enum types are too generic #1700
Comments
I'm not sure I understand the problems, for the first one can you post the enum definition and the generated DSL RBI? For #2 I'm not sure what you mean by type alias. Do you have a concrete example usage you can share? |
hi @KaanOzkan, let me try making a concrete example of what I mean: Problem 1: If I have this rails model: class Person < ApplicationRecord
enum favorite_color: { blue: 'blue', red: 'red' }
end This means accessing sig { returns(T.nilable(T.any(:red, :blue))) }
def favorite_color; end Instead I get: sig { returns(T.nilable(String)) }
def favorite_color; end Poking around the class at runtime it is possible to pull the list of possible values for an enum so this might be solveable Problem 2: Assuming problem 1 is fixed and the signature is what I expect, I would like to be able to reference the sig { params(favorite_color: Person::FavoriteColorEnum) }
def do_stuff_with_favorite_color!(favorite_color)
...
end This way if changes are made to the actual enum definition, all type signatures referencing it are also updated (after running tapioca dsl) I imagine it might look something like this in class Person < ApplicationRecord
# I don't know if this is the right way to do it, just how I imagine it coming from a Typescript background
FavoriteColorEnum = T.any(:red, :blue)
...
sig { returns(T.nilable(Person::FavoriteColorEnum)) }
def favorite_color; end
end I am not sure how that constant would be available at runtime. I guess it would not: similarly to If it helps, I am coming from Typescript and only this week I started using Sorbet, so the ecosystem here is all new to me. Hopefully it's clear now 🙂 |
The individual symbols If you want to use enum values in your signatures you need to use |
I see how this can't be done now. In sorbet, However The root problem preventing this use case is that it's not possible to define a type as an enum of values |
While adopting tapioca on our project I noticed two issues with rails model enums:
T.nilable(::String)
even though the actual possible values for the enum should be available by tapioca to generateThe code generating the types for the enums seems to be this:
tapioca/lib/tapioca/dsl/compilers/active_record_enum.rb
Lines 94 to 104 in d2a27bd
I wonder if it's possible to solve both problems. The main method to get the enum value seems to be generated by some other Compiler though so it might be tricky
I might give it a shot to implement this myself, recommendations and suggestions are welcome
The text was updated successfully, but these errors were encountered: