Skip to content

Commit

Permalink
stored_attributes need to be specific to a subclass.
Browse files Browse the repository at this point in the history
Currently they are all stored globally in the same `Hash`.
This commit forces the creation of a per-class variable if necessary.

The behavior was exposed through the following test-case:

```
  1) Failure:
StoreTest#test_all_stored_attributes_are_returned [/Users/senny/Projects/rails/activerecord/test/cases/store_test.rb:151]:
--- expected
+++ actual
@@ -1 +1 @@
-[:color, :homepage, :favorite_food]
+[:resolution, :color, :homepage, :favorite_food]
```

Conflicts:
	activerecord/CHANGELOG.md
	activerecord/test/cases/store_test.rb
  • Loading branch information
senny authored and matthewd committed May 14, 2014
1 parent 50bfc51 commit 69fc26f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
6 changes: 6 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
* Fix bug where `ActiveRecord::Store` used a global `Hash` to keep track of
all registered `stored_attributes`. Now every subclass of
`ActiveRecord::Base` has it's own `Hash`.

*Yves Senn*

* `change_column_default` allows `[]` as argument to `change_column_default`.

Fixes #11586.
Expand Down
3 changes: 3 additions & 0 deletions activerecord/lib/active_record/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ def store_accessor(store_attribute, *keys)
end
end

# assign new store attribute and create new hash to ensure that each class in the hierarchy
# has its own hash of stored attributes.
self.stored_attributes = {} if self.stored_attributes.blank?
self.stored_attributes[store_attribute] ||= []
self.stored_attributes[store_attribute] |= keys
end
Expand Down
12 changes: 12 additions & 0 deletions activerecord/test/cases/store_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,16 @@ class StoreTest < ActiveRecord::TestCase
assert_equal dumped, second_dump
assert_equal @john, YAML.load(second_dump)
end

test "stored_attributes are tracked per class" do
first_model = Class.new(ActiveRecord::Base) do
store_accessor :data, :color
end
second_model = Class.new(ActiveRecord::Base) do
store_accessor :data, :width, :height
end

assert_equal [:color], first_model.stored_attributes[:data]
assert_equal [:width, :height], second_model.stored_attributes[:data]
end
end

0 comments on commit 69fc26f

Please sign in to comment.