diff options
author | Yves Senn <yves.senn@gmail.com> | 2013-10-10 10:32:52 +0200 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2013-10-25 08:35:35 +0200 |
commit | bf43b4c33fe323448a714854327fdabdcb3c7a14 (patch) | |
tree | eb016b3765d59b80bc90ea5bc480c851b5af568f /activerecord | |
parent | 557b8b69477ff6b7ac2bc6d2e76dc6a3e04fd9c3 (diff) | |
download | rails-bf43b4c33fe323448a714854327fdabdcb3c7a14.tar.gz rails-bf43b4c33fe323448a714854327fdabdcb3c7a14.tar.bz2 rails-bf43b4c33fe323448a714854327fdabdcb3c7a14.zip |
`stored_attributes` need to be specific to a subclass.
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]
```
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG.md | 6 | ||||
-rw-r--r-- | activerecord/lib/active_record/store.rb | 3 | ||||
-rw-r--r-- | activerecord/test/cases/store_test.rb | 12 |
3 files changed, 21 insertions, 0 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 25b905891f..ba11c7f67e 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -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* + * Save `has_one` association when primary key is manually set. Fixes #12302. diff --git a/activerecord/lib/active_record/store.rb b/activerecord/lib/active_record/store.rb index a610f479f2..5fc80cdb6d 100644 --- a/activerecord/lib/active_record/store.rb +++ b/activerecord/lib/active_record/store.rb @@ -86,6 +86,9 @@ module ActiveRecord 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 diff --git a/activerecord/test/cases/store_test.rb b/activerecord/test/cases/store_test.rb index c2c56abacd..0c9f7ccd55 100644 --- a/activerecord/test/cases/store_test.rb +++ b/activerecord/test/cases/store_test.rb @@ -150,4 +150,16 @@ class StoreTest < ActiveRecord::TestCase test "all stored attributes are returned" do assert_equal [:color, :homepage, :favorite_food], Admin::User.stored_attributes[:settings] 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 |