Skip to content

Commit 2cd568a

Browse files
committed
Handle alternative base classes in define_attribute_methods
Ref: rails@d429bfb#r136670440
1 parent e59b779 commit 2cd568a

File tree

4 files changed

+23
-15
lines changed

4 files changed

+23
-15
lines changed

activemodel/lib/active_model/attribute_methods.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,8 @@ def inherited(base) # :nodoc:
365365
super
366366
base.class_eval do
367367
@attribute_method_patterns_cache = nil
368+
@aliases_by_attribute_name = nil
369+
@generated_attribute_methods = nil
368370
end
369371
end
370372

activerecord/lib/active_record/attribute_methods.rb

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -115,27 +115,37 @@ def define_attribute_methods # :nodoc:
115115
GeneratedAttributeMethods::LOCK.synchronize do
116116
return false if @attribute_methods_generated
117117

118-
superclass.define_attribute_methods unless superclass == Base
118+
superclass.define_attribute_methods unless base_class?
119119

120120
unless abstract_class?
121121
load_schema
122122
super(attribute_names)
123-
end
124-
125-
ActiveSupport::CodeGenerator.batch(generated_attribute_methods, __FILE__, __LINE__) do |code_generator|
126-
aliases_by_attribute_name.each do |old_name, new_names|
127-
new_names.each do |new_name|
128-
generate_alias_attribute_methods(code_generator, new_name, old_name)
129-
end
130-
end
123+
alias_attribute :id_value, :id if _has_attribute?("id")
131124
end
132125

133126
@attribute_methods_generated = true
134-
@alias_attributes_mass_generated = true
127+
128+
generate_alias_attributes
135129
end
136130
true
137131
end
138132

133+
def generate_alias_attributes # :nodoc:
134+
superclass.generate_alias_attributes unless superclass == Base
135+
136+
return if @alias_attributes_mass_generated
137+
138+
ActiveSupport::CodeGenerator.batch(generated_attribute_methods, __FILE__, __LINE__) do |code_generator|
139+
aliases_by_attribute_name.each do |old_name, new_names|
140+
new_names.each do |new_name|
141+
generate_alias_attribute_methods(code_generator, new_name, old_name)
142+
end
143+
end
144+
end
145+
146+
@alias_attributes_mass_generated = true
147+
end
148+
139149
def undefine_attribute_methods # :nodoc:
140150
GeneratedAttributeMethods::LOCK.synchronize do
141151
super if @attribute_methods_generated

activerecord/lib/active_record/attribute_methods/primary_key.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,6 @@ def inherited(base)
152152
@attributes_builder = nil
153153
end
154154
end
155-
156-
def load_schema! # :nodoc:
157-
super
158-
alias_attribute :id_value, :id if @columns_hash.key?("id")
159-
end
160155
end
161156
end
162157
end

activerecord/test/cases/attribute_methods_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def setup
3737
self.table_name = "topics"
3838
end
3939

40+
new_topic_model.define_attribute_methods
4041
assert_includes new_topic_model.attribute_names, "id"
4142
assert_includes new_topic_model.attribute_aliases, "id_value"
4243
end

0 commit comments

Comments
 (0)