Repository navigation
Ensure attributes are always inherited - #39467
jonathanhefner wants to merge 1 commit into
Conversation
Prior to this commit, attributes were not inherited by a subclass if they were added to the parent class after the subclass had added its own attributes. This was due to the way attribute definitions were stored in an append-only `class_attribute`. This commit changes the way attribute definitions are stored to ensure they are always inherited.
| super | ||
| attributes_to_define_after_schema_loads.each do |name, (type, options)| | ||
|
|
||
| deferred = ancestors.map { |klass| klass.try(:deferred_attribute_definitions) }.compact.reduce(&:reverse_merge) |
There was a problem hiding this comment.
I find this line a bit hard to grok, what about merging more imperatively:
| deferred = ancestors.map { |klass| klass.try(:deferred_attribute_definitions) }.compact.reduce(&:reverse_merge) | |
| deferred = ancestors.each_with_object({}) do |mod, memo| | |
| memo.reverse_merge!(mod.deferred_attribute_definitions) if mod.respond_to?(:deferred_attribute_definitions) | |
| end |
Or walking up the superclass chain:
diff --git a/activerecord/lib/active_record/attributes.rb b/activerecord/lib/active_record/attributes.rb
index 962e15c1e8..88b250505b 100644
--- a/activerecord/lib/active_record/attributes.rb
+++ b/activerecord/lib/active_record/attributes.rb
@@ -240,17 +240,24 @@ def define_attribute(
def load_schema! # :nodoc:
super
+ define_deferred_attributes
+ end
- deferred = ancestors.map { |klass| klass.try(:deferred_attribute_definitions) }.compact.reduce(&:reverse_merge)
+ def define_deferred_attributes(klass: self, defined: Set.new) # :nodoc:
+ deferred_attribute_definitions.each do |name, (type, options)|
+ next if defined.include?(name)
- deferred.each do |name, (type, options)|
if type.is_a?(Symbol)
- adapter_name = ActiveRecord::Type.adapter_name_from(self)
+ adapter_name = ActiveRecord::Type.adapter_name_from(klass)
type = ActiveRecord::Type.lookup(type, **options.except(:default), adapter: adapter_name)
end
- define_attribute(name, type, **options.slice(:default))
+ klass.define_attribute(name, type, **options.slice(:default))
+
+ defined.add(name)
end
+
+ superclass.try(:define_deferred_attributes, klass: klass, defined: defined)
end
privateThere was a problem hiding this comment.
If we use try here, we should require the core extension (active_support/core_ext/object/try).
There was a problem hiding this comment.
It might be a matter of preference though, I'd not prefer to change both setter side and using side (and drop exisiting attributes_to_define_after_schema_loads and introduce new one are too), unless it is obviously easier to understand.
How about a way to address it only in setter side (in attribute) like as #39473?
This is a common `class_attribute` implementation issue (e.g. rails#39467, rails#39468, rails#38871). For now, we should take care of the change propagation by themselves if we want to ensure the propagation after the `class_attribute` are accessed in subclasses. Fixes rails#20678.
|
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. |
|
Closing in favor of #39487. |
Prior to this commit, attributes were not inherited by a subclass if they were added to the parent class after the subclass had added its own attributes. This was due to the way attribute definitions were stored in an append-only
class_attribute. This commit changes the way attribute definitions are stored to ensure they are always inherited.Splitting this off from #38871.