diff options
author | Jon Leighton <j@jonathanleighton.com> | 2011-09-26 12:40:03 +0100 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2011-09-26 12:40:03 +0100 |
commit | a15424b92c182bcc4d2b693dc0001a44c9b0802f (patch) | |
tree | cefb6e86b574038704a9bf3b34f86509d9fbf21d /activemodel | |
parent | b838059817aca490f78e3bb74a070729270300db (diff) | |
download | rails-a15424b92c182bcc4d2b693dc0001a44c9b0802f.tar.gz rails-a15424b92c182bcc4d2b693dc0001a44c9b0802f.tar.bz2 rails-a15424b92c182bcc4d2b693dc0001a44c9b0802f.zip |
Make serializable_hash take attr values directly from attributes hash.
Previously, it would use send() to get the attribute. In Active
Resource, this would rely on hitting method missing. If a method with
the same name was defined further up the ancestor chain, that method
would wrongly be called.
This change fixes test_to_xml_with_private_method_name_as_attribute in
activeresource/test/cases/base_test.rb, which was broken after
51bef9d8fb0b4da7a104425ab8545e9331387743, because that change made
to_xml use serializable_hash.
Diffstat (limited to 'activemodel')
-rw-r--r-- | activemodel/lib/active_model/serialization.rb | 4 | ||||
-rw-r--r-- | activemodel/test/cases/serialization_test.rb | 9 |
2 files changed, 12 insertions, 1 deletions
diff --git a/activemodel/lib/active_model/serialization.rb b/activemodel/lib/active_model/serialization.rb index b9f6f6cbbf..a756b9f205 100644 --- a/activemodel/lib/active_model/serialization.rb +++ b/activemodel/lib/active_model/serialization.rb @@ -78,8 +78,10 @@ module ActiveModel attribute_names -= Array.wrap(except).map(&:to_s) end + hash = attributes.slice(*attribute_names) + method_names = Array.wrap(options[:methods]).select { |n| respond_to?(n) } - hash = Hash[(attribute_names + method_names).map { |n| [n, send(n)] }] + method_names.each { |n| hash[n] = send(n) } serializable_add_includes(options) do |association, records, opts| hash[association] = if records.is_a?(Enumerable) diff --git a/activemodel/test/cases/serialization_test.rb b/activemodel/test/cases/serialization_test.rb index 071cb9ff4e..29bcdeae67 100644 --- a/activemodel/test/cases/serialization_test.rb +++ b/activemodel/test/cases/serialization_test.rb @@ -77,6 +77,15 @@ class SerializationTest < ActiveModel::TestCase assert_equal expected , @user.serializable_hash(:methods => [:bar]) end + def test_should_not_call_methods_for_attributes + def @user.name + "Jon" + end + + expected = { "name" => "David" } + assert_equal expected, @user.serializable_hash(:only => :name) + end + def test_include_option_with_singular_association expected = {"name"=>"David", "gender"=>"male", "email"=>"david@example.com", :address=>{"street"=>"123 Lane", "city"=>"Springfield", "state"=>"CA", "zip"=>11111}} |