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...
1 parent 5372ec2 commit 329d46df119c6acf34a2cd0db72733ae29a408a3 @senny senny committed Apr 14, 2014
Showing with 16 additions and 5 deletions.
  1. +16 −5 activerecord/lib/active_record/attribute_methods/serialization.rb
View
21 activerecord/lib/active_record/attribute_methods/serialization.rb
@@ -27,21 +27,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 329d46d

Please sign in to comment.