diff options
author | Yves Senn <yves.senn@gmail.com> | 2014-04-14 14:47:28 +0200 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2014-04-14 14:49:26 +0200 |
commit | 2e000ab23d187e425a1be1ce8ce43a0d4c6f5be7 (patch) | |
tree | 6a2c071d45c21d80a90e0008f5f332895ea2ee2e /activerecord/lib | |
parent | 97b5253a37eccf49ac2f9f4c5330801684312285 (diff) | |
download | rails-2e000ab23d187e425a1be1ce8ce43a0d4c6f5be7.tar.gz rails-2e000ab23d187e425a1be1ce8ce43a0d4c6f5be7.tar.bz2 rails-2e000ab23d187e425a1be1ce8ce43a0d4c6f5be7.zip |
docs, double meaning of `serialize` argument. Closes #14284.
The second argument to serialize has double meaning:
* `class_name` of the Type to serialize
* `coder` to use then serializing.
The new variable name and the docs better describe that behavior.
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/attribute_methods/serialization.rb | 21 |
1 files changed, 16 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/attribute_methods/serialization.rb b/activerecord/lib/active_record/attribute_methods/serialization.rb index 67abbbc2a0..c3466153d6 100644 --- a/activerecord/lib/active_record/attribute_methods/serialization.rb +++ b/activerecord/lib/active_record/attribute_methods/serialization.rb @@ -30,7 +30,8 @@ module ActiveRecord # ==== Parameters # # * +attr_name+ - The field name that should be serialized. - # * +class_name+ - Optional, class name that the object type should be equal to. + # * +class_name_or_coder+ - Optional, a coder object, which responds to `.load` / `.dump` + # or a class name that the object type should be equal to. # # ==== Example # @@ -38,13 +39,23 @@ module ActiveRecord # class User < ActiveRecord::Base # serialize :preferences # end - def serialize(attr_name, class_name = Object) + # + # # Serialize preferences using JSON as coder. + # class User < ActiveRecord::Base + # serialize :preferences, JSON + # end + # + # # Serialize preferences as Hash using YAML coder. + # class User < ActiveRecord::Base + # serialize :preferences, Hash + # end + def serialize(attr_name, class_name_or_coder = Object) include Behavior - coder = if [:load, :dump].all? { |x| class_name.respond_to?(x) } - class_name + coder = if [:load, :dump].all? { |x| class_name_or_coder.respond_to?(x) } + class_name_or_coder else - Coders::YAMLColumn.new(class_name) + Coders::YAMLColumn.new(class_name_or_coder) end # merge new serialized attribute and create new hash to ensure that each class in inheritance hierarchy |