aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorCarlos Antonio da Silva <carlosantoniodasilva@gmail.com>2012-06-18 23:54:37 -0300
committerCarlos Antonio da Silva <carlosantoniodasilva@gmail.com>2012-06-18 23:54:37 -0300
commit69f19b292ad4b228f2bc5f89732a8ef3b704fab6 (patch)
tree6ef5368dd403dfb0d293dc8b8885f7d6d0c92a60 /activerecord/lib
parentdf2104e06784a4b98d8f30cb3ea4eee69304e768 (diff)
parent8593964d9741704ff030e3bdf61e0ed64526ecec (diff)
downloadrails-69f19b292ad4b228f2bc5f89732a8ef3b704fab6.tar.gz
rails-69f19b292ad4b228f2bc5f89732a8ef3b704fab6.tar.bz2
rails-69f19b292ad4b228f2bc5f89732a8ef3b704fab6.zip
Merge pull request #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]
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/store.rb16
1 files changed, 15 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/store.rb b/activerecord/lib/active_record/store.rb
index 542cb3187a..d13491502e 100644
--- a/activerecord/lib/active_record/store.rb
+++ b/activerecord/lib/active_record/store.rb
@@ -1,4 +1,6 @@
+require 'active_support/concern'
require 'active_support/core_ext/hash/indifferent_access'
+require 'active_support/core_ext/class/attribute'
module ActiveRecord
# Store gives you a thin wrapper around serialize for the purpose of storing hashes in a single column.
@@ -33,9 +35,18 @@ module ActiveRecord
# class SuperUser < User
# store_accessor :settings, :privileges, :servants
# end
+ #
+ # The stored attribute names can be retrieved using +stored_attributes+.
+ #
+ # User.stored_attributes[:settings] # [:color, :homepage]
module Store
extend ActiveSupport::Concern
+ included do
+ class_attribute :stored_attributes
+ self.stored_attributes = {}
+ end
+
module ClassMethods
def store(store_attribute, options = {})
serialize store_attribute, IndifferentCoder.new(options[:coder])
@@ -43,7 +54,8 @@ module ActiveRecord
end
def store_accessor(store_attribute, *keys)
- keys.flatten.each do |key|
+ keys = keys.flatten
+ keys.each do |key|
define_method("#{key}=") do |value|
initialize_store_attribute(store_attribute)
send(store_attribute)[key] = value
@@ -55,6 +67,8 @@ module ActiveRecord
send(store_attribute)[key]
end
end
+
+ self.stored_attributes[store_attribute] = keys
end
end