Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
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...
commit 2e000ab23d187e425a1be1ce8ce43a0d4c6f5be7 1 parent 97b5253
@senny senny authored
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
@@ -30,7 +30,8 @@ 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
#
@@ -38,13 +39,23 @@ module ClassMethods
# 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
Please sign in to comment.
Something went wrong with that request. Please try again.