Skip to content
Permalink
Browse files

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.
  • Loading branch information...
senny committed Apr 14, 2014
1 parent 97b5253 commit 2e000ab23d187e425a1be1ce8ce43a0d4c6f5be7
Showing with 16 additions and 5 deletions.
  1. +16 −5 activerecord/lib/active_record/attribute_methods/serialization.rb
@@ -30,21 +30,32 @@ module ClassMethods
# ==== 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
#
# # Serialize a preferences attribute.
# 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

0 comments on commit 2e000ab

Please sign in to comment.
You can’t perform that action at this time.