-
Notifications
You must be signed in to change notification settings - Fork 133
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
Provide a hook for custom ActiveModel
types
#2112
Provide a hook for custom ActiveModel
types
#2112
Conversation
aca3e3b
to
d103457
Compare
You can define a "generic" For example: class GenericType < ActiveModel::Type::Value
def initialize(type)
@type = type
end
def __sorbet_type = @type
end
class User
end
class Foo
include ActiveModel::Attributes
attribute :foo, GenericType.new(User)
end
p Foo.attribute_types.first.last.__sorbet_type # => User |
Thanks for the suggestion @paracycle. I'll update this PR to work that way. |
d103457
to
158c705
Compare
PR updated. Thank you for the suggestion. I think this is a much better solution. |
ActiveModel
types
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @rzane, we like this change and want to merge it in. We just got a bit of internal feedback, that this method is really more for Tapioca, and not Sorbet itself.
__type_for_tapioca
and __tapica_type
was suggested, which would make that more clear, and better help readers understand where to look for callers of this (seemingly unused) method.
Would you mind renaming the method to one of those?
Thanks @amomchilov, updated 👍 |
3760515
to
e198957
Compare
e198957
to
e6839dc
Compare
I just want to call out that there might be an existing convention for this sort of thing: tapioca/lib/tapioca/dsl/compilers/json_api_client_resource.rb Lines 149 to 150 in cb40b4d
The type isn't used by Sorbet directly, but it does have to be a type that is compatible with Sorbet and will eventually be consumed by Sorbet after generating DSL files. |
@amomchilov are there any remaining blockers here? |
Nope, looks good! Thanks for the changes. I'm deprecating |
Motivation
We use
ActiveModel::Attributes
extensively. In many cases, we're passing complex values to these models, such as aUser
. It wouldn't be practical in our situation to define anActiveModel::Type
class for each and every possible value that gets passed into a model.Any of the following solutions would solve my issue:
Implementation
This PR allows instances of
ActiveModel::Type::Value
to define an optional method#__tapioca_type
. It returns a type.For example, you could use this hook to implement an interface like this:
Tests
Tests are included.