Repository navigation
Unify decorate_attribute_type and attribute - #39929
Conversation
|
I'd not love to inline complexity (inlining user-defined type decoration) in |
|
@kamipo Would you mind elaborating a little bit? The approach in this PR does not seem very complex to me. Is there future functionality that you are thinking of that this PR would compromise? I can imagine the approach in this PR being extended to provide Additionally, it seems strange to me that |
| self.attributes_to_define_after_schema_loads = | ||
| attributes_to_define_after_schema_loads.merge( | ||
| name => [cast_type || block, options] | ||
| ) | ||
| prev_cast_type, prev_options, prev_block = (attributes_to_define_after_schema_loads[name] unless cast_type) | ||
|
|
||
| self.attributes_to_define_after_schema_loads = attributes_to_define_after_schema_loads.merge( | ||
| name => [cast_type || prev_cast_type, options.presence || prev_options || {}, block || prev_block] |
There was a problem hiding this comment.
I'm not sure if you would agree, this line is most hard to read part than before.
It will allow to work prev_cast_type with options (not prev_options), people might not actually do that though.
There was a problem hiding this comment.
Working prev_cast_type with options is maybe intended?
Even so, I think this line is still hard to understand how works as intended than before.
There was a problem hiding this comment.
Yes, I did that to ensure the behavior from #39830 also works for attributes that are not from the database.
class Post < ActiveRecord::Base
attribute :x, :integer
end
class SpecialPost < Post
attribute :x, default: 42
end
# BEFORE (ignore prev_cast_type if options.present?)
SpecialPost.type_for_attribute(:x) # => #<ActiveModel::Type::Value...>
# AFTER (allow prev_cast_type)
SpecialPost.type_for_attribute(:x) # => #<ActiveModel::Type::Integer...>I can remove it though, and make it a separate PR.
But to try to explain the overall idea: When attribute is called with a cast_type, it signals a brand new attribute definition (which overwrites any previous definition of the same name). When attribute is called without a cast_type, it signals a modification to an existing attribute definition (e.g. new options or new block).
Would writing it like this be more readable?
if cast_type.nil?
cast_type = attributes_to_define_after_schema_loads.dig(name, 0)
options = attributes_to_define_after_schema_loads.dig(name, 1).to_h if options.empty?
block = attributes_to_define_after_schema_loads.dig(name, 2) if block.nil?
end
self.attributes_to_define_after_schema_loads = attributes_to_define_after_schema_loads.merge(
name => [cast_type, options, block]
)Or would it be better to break up that line for better readability?
self.attributes_to_define_after_schema_loads = attributes_to_define_after_schema_loads.merge(
name => [
cast_type || prev_cast_type,
options.presence || prev_options || {},
block || prev_block
]
)There was a problem hiding this comment.
I don't quite understand what it is we're trying to do in this code? Even with the default: explanation?
There was a problem hiding this comment.
The underlying idea is that an attribute's type, options (e.g. default value), and decorator could all be declared separately. This is already partially supported, but only when the type is derived from the database and not overridden. The changes here extend that behavior in a general way.
I've tried to clarify that code a bit. Please let me know what you think.
| type = type_for_attribute(name) | ||
| end | ||
|
|
||
| type = transform[type] if transform |
There was a problem hiding this comment.
This feature (specific type with decoration attribute :type do |type| ... end) is intentionally limited since it is not internally used.
If we'd like to provide internally unused feature, I don't think it's appropriate to sneak it in as part of the refactoring.
There was a problem hiding this comment.
Ah, I thought the block of attribute was meant to be an internal-only feature, since it is undocumented. Would it be good to make it an internal-only feature? If so, should we rename it to _block?
But if not, I can enforce the limitation by raising an ArgumentError when both cast_type and block are specified.
There was a problem hiding this comment.
But if not, I can enforce the limitation by raising an
ArgumentErrorwhen bothcast_typeand block are specified.
That sounds right to me 👍
There was a problem hiding this comment.
Ah, looks like this is actually used internally for the EnumType. Not sure how to do that in any other way without the block form? I guess you'd end up in awkward type_for_attribute before then calling attribute, doesn't sound right.
|
@kamipo Thank you for taking the time to review this! 🙏 |
|
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. |
kaspth
left a comment
There was a problem hiding this comment.
Sweet! Let me reopen this.
|
|
||
| type = transform[type] if transform | ||
|
|
||
| define_attribute(name, type, **options.slice(:default)) |
There was a problem hiding this comment.
| define_attribute(name, type, **options.slice(:default)) | |
| define_attribute(name, type, default: options[:default]) |
Since we're only passing one value, would it be clearer as just this?
There was a problem hiding this comment.
That would behave differently when there is no default. When there is no default value, **options.slice(:default) is equivalent to **{}, but default: options[:default] is equivalent to default: nil, which would override NO_DEFAULT_PROVIDED.
| type = type_for_attribute(name) | ||
| end | ||
|
|
||
| type = transform[type] if transform |
There was a problem hiding this comment.
Ah, looks like this is actually used internally for the EnumType. Not sure how to do that in any other way without the block form? I guess you'd end up in awkward type_for_attribute before then calling attribute, doesn't sound right.
| self.attributes_to_define_after_schema_loads = | ||
| attributes_to_define_after_schema_loads.merge( | ||
| name => [cast_type || block, options] | ||
| ) | ||
| prev_cast_type, prev_options, prev_block = (attributes_to_define_after_schema_loads[name] unless cast_type) | ||
|
|
||
| self.attributes_to_define_after_schema_loads = attributes_to_define_after_schema_loads.merge( | ||
| name => [cast_type || prev_cast_type, options.presence || prev_options || {}, block || prev_block] |
There was a problem hiding this comment.
I don't quite understand what it is we're trying to do in this code? Even with the default: explanation?
57cd297 to
ac58275
Compare
decorate_attribute_type and attribute
There was a problem hiding this comment.
Would this be clearer if we had a separate setter method?
def attribute(name, cast_type = nil, **options, &decorator)
name = name.to_s
reload_schema_from_cache
override_attribute_defined_after_schema_load name do |prev_cast_type, prev_options, prev_decorator|
[ cast_type || prev_cast_type,
options.presence || prev_options,
decorator || prev_decorator ] unless cast_type && prev_cast_type
end
endOr potentially the more wasteful option (hey zip!):
def attribute(name, cast_type = nil, **options, &decorator)
name = name.to_s
reload_schema_from_cache
override_attribute_defined_after_schema_load name do |previous_definition|
[ cast_type, options, decorator ].zip(previous_definition).map { |option| option.find(&:present?) }
end
endFollow-up to 75c309c. As a result of these changes, attributes can have their type and default value configured separately. Similar behavior was implemented in rails#39380, but only for attributes that derive (and do not override) their type from the database.
ac58275 to
4cc9c0f
Compare
|
Cool, let's go for this and then the PR intersection with heritable value can swoop back in here. |
…riginal-attribute-type" This reverts commit 79d0c17, reversing changes made to bc828f7. Fixes rails#41138.
This uses the changes from rails#39929. Instead of doing a manual casting of the previous value as part of the type, it leverages the existing support to do this now. It also fixes a problem when loading a class with an encrypt declaration that wasn't backed by a table was failing. CC @kaspth
Follow-up to 75c309c.
As a result of these changes, attributes can have their type and default value configured separately. Similar behavior was implemented in #39830, but only for attributes that derive (and do not override) their type from the database.