diff options
Diffstat (limited to 'activerecord/lib/active_record/store.rb')
-rw-r--r-- | activerecord/lib/active_record/store.rb | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/store.rb b/activerecord/lib/active_record/store.rb index 8cc84f81d0..1c7b839e5e 100644 --- a/activerecord/lib/active_record/store.rb +++ b/activerecord/lib/active_record/store.rb @@ -15,7 +15,7 @@ module ActiveRecord # class User < ActiveRecord::Base # store :settings, accessors: [ :color, :homepage ] # end - # + # # u = User.new(color: 'black', homepage: '37signals.com') # u.color # Accessor stored attribute # u.settings[:country] = 'Denmark' # Any attribute, even if not specified with an accessor @@ -26,7 +26,7 @@ module ActiveRecord # end module Store extend ActiveSupport::Concern - + module ClassMethods def store(store_attribute, options = {}) serialize store_attribute, Hash @@ -34,17 +34,19 @@ module ActiveRecord end def store_accessor(store_attribute, *keys) - Array(keys).flatten.each do |key| + keys.flatten.each do |key| define_method("#{key}=") do |value| + send("#{store_attribute}=", {}) unless send(store_attribute).is_a?(Hash) send(store_attribute)[key] = value send("#{store_attribute}_will_change!") end - + define_method(key) do + send("#{store_attribute}=", {}) unless send(store_attribute).is_a?(Hash) send(store_attribute)[key] end end end end end -end
\ No newline at end of file +end |