Skip to content

Commit 8593964

Browse files
Refactor and use class_attribute
1 parent 757f723 commit 8593964

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

activerecord/CHANGELOG.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
## Rails 4.0.0 (unreleased) ##
22

3-
* Added a `stored_attributes` hash which contains the attributes stored
4-
using ActiveRecord::Store. This allows you to retrieve the list of
5-
attributes you've defined.
3+
* Added `stored_attributes` hash which contains the attributes stored using
4+
ActiveRecord::Store. This allows you to retrieve the list of attributes
5+
you've defined.
66

7-
*Joost Baaij*
7+
class User < ActiveRecord::Base
8+
store :settings, accessors: [:color, :homepage]
9+
end
10+
11+
User.stored_attributes[:settings] # [:color, :homepage]
12+
13+
*Joost Baaij & Carlos Antonio da Silva*
814

915
* `composed_of` was removed. You'll have to write your own accessor
1016
and mutator methods if you'd like to use value objects to represent some

activerecord/lib/active_record/store.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'active_support/concern'
22
require 'active_support/core_ext/hash/indifferent_access'
3+
require 'active_support/core_ext/class/attribute'
34

45
module ActiveRecord
56
# Store gives you a thin wrapper around serialize for the purpose of storing hashes in a single column.
@@ -42,7 +43,7 @@ module Store
4243
extend ActiveSupport::Concern
4344

4445
included do
45-
config_attribute :stored_attributes
46+
class_attribute :stored_attributes
4647
self.stored_attributes = {}
4748
end
4849

@@ -53,7 +54,8 @@ def store(store_attribute, options = {})
5354
end
5455

5556
def store_accessor(store_attribute, *keys)
56-
keys.flatten.each do |key|
57+
keys = keys.flatten
58+
keys.each do |key|
5759
define_method("#{key}=") do |value|
5860
initialize_store_attribute(store_attribute)
5961
send(store_attribute)[key] = value
@@ -66,7 +68,7 @@ def store_accessor(store_attribute, *keys)
6668
end
6769
end
6870

69-
self.stored_attributes[store_attribute] = keys.flatten
71+
self.stored_attributes[store_attribute] = keys
7072
end
7173
end
7274

0 commit comments

Comments
 (0)