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.
This commit is contained in:
parent
b838059817
commit
a15424b92c
@ -78,8 +78,10 @@ def serializable_hash(options = nil)
|
||||
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)
|
||||
|
@ -77,6 +77,15 @@ def test_should_not_call_methods_that_dont_respond
|
||||
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}}
|
||||
|
@ -1004,9 +1004,17 @@ def test_to_xml_with_element_name
|
||||
|
||||
def test_to_xml_with_private_method_name_as_attribute
|
||||
Person.format = :xml
|
||||
assert_nothing_raised(ArgumentError) {
|
||||
Customer.new(:test => true).to_xml
|
||||
}
|
||||
|
||||
customer = Customer.new(:foo => "foo")
|
||||
customer.singleton_class.class_eval do
|
||||
def foo
|
||||
"bar"
|
||||
end
|
||||
private :foo
|
||||
end
|
||||
|
||||
assert !customer.to_xml.include?("<foo>bar</foo>")
|
||||
assert customer.to_xml.include?("<foo>foo</foo>")
|
||||
ensure
|
||||
Person.format = :json
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user