diff options
author | José Valim <jose.valim@gmail.com> | 2011-09-30 14:20:41 +0200 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2011-09-30 14:20:41 +0200 |
commit | d4457dc32b4d5dc9fde6c852f55a0d43ee021282 (patch) | |
tree | 143dbc2eb36c94fc5d4ac7d1f20857db1821a955 /activemodel | |
parent | 37b9594a8e08916e1e9ae9a6aaffc13ef516ad11 (diff) | |
download | rails-d4457dc32b4d5dc9fde6c852f55a0d43ee021282.tar.gz rails-d4457dc32b4d5dc9fde6c852f55a0d43ee021282.tar.bz2 rails-d4457dc32b4d5dc9fde6c852f55a0d43ee021282.zip |
Provide read_attribute_for_serialization as the API to serialize attributes.
Diffstat (limited to 'activemodel')
-rw-r--r-- | activemodel/lib/active_model/serialization.rb | 25 | ||||
-rw-r--r-- | activemodel/test/cases/serialization_test.rb | 6 |
2 files changed, 26 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 |