aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2012-05-30 23:18:08 -0700
committerJeremy Kemper <jeremy@bitsweat.net>2012-05-30 23:21:57 -0700
commitd5354af1149496e1a6ebccc5434dbba3bde86265 (patch)
tree0bd3a964279fcfd2f4be4225637e76b1efc1907e /activerecord/lib/active_record
parent9a30d82d455a5b8578f7dfe6f5f9479fca44c78f (diff)
downloadrails-d5354af1149496e1a6ebccc5434dbba3bde86265.tar.gz
rails-d5354af1149496e1a6ebccc5434dbba3bde86265.tar.bz2
rails-d5354af1149496e1a6ebccc5434dbba3bde86265.zip
Fix backward compatibility with stored Hash values. Wrap coders to convert serialized values to indifferent access.
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/store.rb34
1 files changed, 32 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/store.rb b/activerecord/lib/active_record/store.rb
index fdd82b489a..d70e02e379 100644
--- a/activerecord/lib/active_record/store.rb
+++ b/activerecord/lib/active_record/store.rb
@@ -38,7 +38,7 @@ module ActiveRecord
module ClassMethods
def store(store_attribute, options = {})
- serialize store_attribute, options.fetch(:coder, ActiveSupport::HashWithIndifferentAccess)
+ serialize store_attribute, IndifferentCoder.new(options[:coder])
store_accessor(store_attribute, options[:accessors]) if options.has_key? :accessors
end
@@ -47,7 +47,7 @@ module ActiveRecord
define_method("#{key}=") do |value|
initialize_store_attribute(store_attribute)
send(store_attribute)[key] = value
- send("#{store_attribute}_will_change!")
+ send :"#{store_attribute}_will_change!"
end
define_method(key) do
@@ -71,5 +71,35 @@ module ActiveRecord
send :"#{store_attribute}=", ActiveSupport::HashWithIndifferentAccess.new
end
end
+
+ class IndifferentCoder
+ def initialize(coder_or_class_name)
+ @coder =
+ if coder_or_class_name.respond_to?(:load) && coder_or_class_name.respond_to?(:dump)
+ coder_or_class_name
+ else
+ ActiveRecord::Coders::YAMLColumn.new(coder_or_class_name || Object)
+ end
+ end
+
+ def dump(obj)
+ @coder.dump self.class.as_indifferent_hash(obj)
+ end
+
+ def load(yaml)
+ self.class.as_indifferent_hash @coder.load(yaml)
+ end
+
+ def self.as_indifferent_hash(obj)
+ case obj
+ when ActiveSupport::HashWithIndifferentAccess
+ obj
+ when Hash
+ obj.with_indifferent_access
+ else
+ HashWithIndifferentAccess.new
+ end
+ end
+ end
end
end