aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2011-09-30 14:20:41 +0200
committerJosé Valim <jose.valim@gmail.com>2011-09-30 14:20:41 +0200
commitd4457dc32b4d5dc9fde6c852f55a0d43ee021282 (patch)
tree143dbc2eb36c94fc5d4ac7d1f20857db1821a955
parent37b9594a8e08916e1e9ae9a6aaffc13ef516ad11 (diff)
downloadrails-d4457dc32b4d5dc9fde6c852f55a0d43ee021282.tar.gz
rails-d4457dc32b4d5dc9fde6c852f55a0d43ee021282.tar.bz2
rails-d4457dc32b4d5dc9fde6c852f55a0d43ee021282.zip
Provide read_attribute_for_serialization as the API to serialize attributes.
-rw-r--r--activemodel/lib/active_model/serialization.rb25
-rw-r--r--activemodel/test/cases/serialization_test.rb6
-rw-r--r--activeresource/lib/active_resource/base.rb4
3 files changed, 30 insertions, 5 deletions
diff --git a/activemodel/lib/active_model/serialization.rb b/activemodel/lib/active_model/serialization.rb
index a756b9f205..7bc3f997b5 100644
--- a/activemodel/lib/active_model/serialization.rb
+++ b/activemodel/lib/active_model/serialization.rb
@@ -78,7 +78,8 @@ module ActiveModel
attribute_names -= Array.wrap(except).map(&:to_s)
end
- hash = attributes.slice(*attribute_names)
+ hash = {}
+ attribute_names.each { |n| hash[n] = read_attribute_for_serialization(n) }
method_names = Array.wrap(options[:methods]).select { |n| respond_to?(n) }
method_names.each { |n| hash[n] = send(n) }
@@ -95,13 +96,33 @@ module ActiveModel
end
private
+
+ # Hook method defining how an attribute value should be retrieved for
+ # serialization. By default this is assumed to be an instance named after
+ # the attribute. Override this method in subclasses should you need to
+ # retrieve the value for a given attribute differently:
+ #
+ # class MyClass
+ # include ActiveModel::Validations
+ #
+ # def initialize(data = {})
+ # @data = data
+ # end
+ #
+ # def read_attribute_for_serialization(key)
+ # @data[key]
+ # end
+ # end
+ #
+ alias :read_attribute_for_serialization :send
+
# Add associations specified via the <tt>:include</tt> option.
#
# Expects a block that takes as arguments:
# +association+ - name of the association
# +records+ - the association record(s) to be serialized
# +opts+ - options for the association records
- def serializable_add_includes(options = {})
+ def serializable_add_includes(options = {}) #:nodoc:
return unless include = options[:include]
unless include.is_a?(Hash)
diff --git a/activemodel/test/cases/serialization_test.rb b/activemodel/test/cases/serialization_test.rb
index 29bcdeae67..1ec915d245 100644
--- a/activemodel/test/cases/serialization_test.rb
+++ b/activemodel/test/cases/serialization_test.rb
@@ -77,12 +77,12 @@ class SerializationTest < ActiveModel::TestCase
assert_equal expected , @user.serializable_hash(:methods => [:bar])
end
- def test_should_not_call_methods_for_attributes
- def @user.name
+ def test_should_use_read_attribute_for_serialization
+ def @user.read_attribute_for_serialization(n)
"Jon"
end
- expected = { "name" => "David" }
+ expected = { "name" => "Jon" }
assert_equal expected, @user.serializable_hash(:only => :name)
end
diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb
index 1ffd83b91d..03c4cc5b9e 100644
--- a/activeresource/lib/active_resource/base.rb
+++ b/activeresource/lib/active_resource/base.rb
@@ -1384,6 +1384,10 @@ module ActiveResource
private
+ def read_attribute_for_serialization(n)
+ attributes[n]
+ end
+
# Determine whether the response is allowed to have a body per HTTP 1.1 spec section 4.4.1
def response_code_allows_body?(c)
!((100..199).include?(c) || [204,304].include?(c))