Skip to content

Commit 69f19b2

Browse files
Merge pull request rails#5412 from tilsammans/stored_attributes
Added `stored_attributes` hash which contains the attributes stored using ActiveRecord::Store. This allows you to retrieve the list of attributes you've defined. class User < ActiveRecord::Base store :settings, accessors: [:color, :homepage] end User.stored_attributes[:settings] # [:color, :homepage]
2 parents df2104e + 8593964 commit 69f19b2

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

activerecord/CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
## Rails 4.0.0 (unreleased) ##
22

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.
6+
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*
14+
315
* `composed_of` was removed. You'll have to write your own accessor
416
and mutator methods if you'd like to use value objects to represent some
517
portion of your models. So, instead of:

activerecord/lib/active_record/store.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
require 'active_support/concern'
12
require 'active_support/core_ext/hash/indifferent_access'
3+
require 'active_support/core_ext/class/attribute'
24

35
module ActiveRecord
46
# Store gives you a thin wrapper around serialize for the purpose of storing hashes in a single column.
@@ -33,17 +35,27 @@ module ActiveRecord
3335
# class SuperUser < User
3436
# store_accessor :settings, :privileges, :servants
3537
# end
38+
#
39+
# The stored attribute names can be retrieved using +stored_attributes+.
40+
#
41+
# User.stored_attributes[:settings] # [:color, :homepage]
3642
module Store
3743
extend ActiveSupport::Concern
3844

45+
included do
46+
class_attribute :stored_attributes
47+
self.stored_attributes = {}
48+
end
49+
3950
module ClassMethods
4051
def store(store_attribute, options = {})
4152
serialize store_attribute, IndifferentCoder.new(options[:coder])
4253
store_accessor(store_attribute, options[:accessors]) if options.has_key? :accessors
4354
end
4455

4556
def store_accessor(store_attribute, *keys)
46-
keys.flatten.each do |key|
57+
keys = keys.flatten
58+
keys.each do |key|
4759
define_method("#{key}=") do |value|
4860
initialize_store_attribute(store_attribute)
4961
send(store_attribute)[key] = value
@@ -55,6 +67,8 @@ def store_accessor(store_attribute, *keys)
5567
send(store_attribute)[key]
5668
end
5769
end
70+
71+
self.stored_attributes[store_attribute] = keys
5872
end
5973
end
6074

activerecord/test/cases/store_test.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class StoreTest < ActiveRecord::TestCase
1313
assert_equal 'black', @john.color
1414
assert_nil @john.homepage
1515
end
16-
16+
1717
test "writing store attributes through accessors" do
1818
@john.color = 'red'
1919
@john.homepage = '37signals.com'
@@ -111,4 +111,8 @@ class StoreTest < ActiveRecord::TestCase
111111
@john.is_a_good_guy = false
112112
assert_equal false, @john.is_a_good_guy
113113
end
114+
115+
test "stored attributes are returned" do
116+
assert_equal [:color, :homepage], Admin::User.stored_attributes[:settings]
117+
end
114118
end

0 commit comments

Comments
 (0)