Skip to content

Commit 307a519

Browse files
committed
Merge pull request rails#14833 from jyao6/attribute_inheritance
Fixed Attribute Inheritance Issue
2 parents 374d19a + 6de710d commit 307a519

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

activerecord/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
* Fix `stored_attributes` to correctly merge the details of stored
2+
attributes defined in parent classes.
3+
4+
Fixes #14672.
5+
6+
*Brad Bennett*, *Jessica Yao*, *Lakshmi Parthasarathy*
7+
18
* `change_column_default` allows `[]` as argument to `change_column_default`.
29

310
Fixes #11586.

activerecord/lib/active_record/store.rb

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@ module Store
6666
extend ActiveSupport::Concern
6767

6868
included do
69-
class_attribute :stored_attributes, instance_accessor: false
70-
self.stored_attributes = {}
69+
class << self
70+
attr_accessor :local_stored_attributes
71+
end
7172
end
7273

7374
module ClassMethods
@@ -93,9 +94,9 @@ def store_accessor(store_attribute, *keys)
9394

9495
# assign new store attribute and create new hash to ensure that each class in the hierarchy
9596
# has its own hash of stored attributes.
96-
self.stored_attributes = {} if self.stored_attributes.blank?
97-
self.stored_attributes[store_attribute] ||= []
98-
self.stored_attributes[store_attribute] |= keys
97+
self.local_stored_attributes ||= {}
98+
self.local_stored_attributes[store_attribute] ||= []
99+
self.local_stored_attributes[store_attribute] |= keys
99100
end
100101

101102
def _store_accessors_module
@@ -105,6 +106,14 @@ def _store_accessors_module
105106
mod
106107
end
107108
end
109+
110+
def stored_attributes
111+
parent = superclass.respond_to?(:stored_attributes) ? superclass.stored_attributes : {}
112+
if self.local_stored_attributes
113+
parent.merge!(self.local_stored_attributes) { |k, a, b| a | b }
114+
end
115+
parent
116+
end
108117
end
109118

110119
protected

activerecord/test/cases/store_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,22 @@ class StoreTest < ActiveRecord::TestCase
163163
assert_equal [:width, :height], second_model.stored_attributes[:data]
164164
end
165165

166+
test "stored_attributes are tracked per subclass" do
167+
first_model = Class.new(ActiveRecord::Base) do
168+
store_accessor :data, :color
169+
end
170+
second_model = Class.new(first_model) do
171+
store_accessor :data, :width, :height
172+
end
173+
third_model = Class.new(first_model) do
174+
store_accessor :data, :area, :volume
175+
end
176+
177+
assert_equal [:color], first_model.stored_attributes[:data]
178+
assert_equal [:color, :width, :height], second_model.stored_attributes[:data]
179+
assert_equal [:color, :area, :volume], third_model.stored_attributes[:data]
180+
end
181+
166182
test "YAML coder initializes the store when a Nil value is given" do
167183
assert_equal({}, @john.params)
168184
end

0 commit comments

Comments
 (0)