diff options
author | Jon Leighton <j@jonathanleighton.com> | 2012-05-11 17:17:29 +0100 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2012-05-11 20:11:04 +0100 |
commit | a8637cf4938d2decd17e702c399ca9c0cf1a6052 (patch) | |
tree | 5528c54f422590112a7982a6c1a0188cd66db50b /activemodel/test/cases | |
parent | b892b4c08fe2cba133882252c64d123e319eb174 (diff) | |
download | rails-a8637cf4938d2decd17e702c399ca9c0cf1a6052.tar.gz rails-a8637cf4938d2decd17e702c399ca9c0cf1a6052.tar.bz2 rails-a8637cf4938d2decd17e702c399ca9c0cf1a6052.zip |
Use respond_to?(:to_ary) rather than is_a?(Enumerable) to detect collection-thing.
Diffstat (limited to 'activemodel/test/cases')
-rw-r--r-- | activemodel/test/cases/serialization_test.rb | 18 | ||||
-rw-r--r-- | activemodel/test/cases/serializers/xml_serialization_test.rb | 17 |
2 files changed, 35 insertions, 0 deletions
diff --git a/activemodel/test/cases/serialization_test.rb b/activemodel/test/cases/serialization_test.rb index 66b18d65e5..d2ba9fd95d 100644 --- a/activemodel/test/cases/serialization_test.rb +++ b/activemodel/test/cases/serialization_test.rb @@ -105,6 +105,24 @@ class SerializationTest < ActiveModel::TestCase assert_equal expected, @user.serializable_hash(:include => :friends) end + class FriendList + def initialize(friends) + @friends = friends + end + + def to_ary + @friends + end + end + + def test_include_option_with_ary + @user.friends = FriendList.new(@user.friends) + expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David", + "friends"=>[{"name"=>'Joe', "email"=>'joe@example.com', "gender"=>'male'}, + {"name"=>'Sue', "email"=>'sue@example.com', "gender"=>'female'}]} + assert_equal expected, @user.serializable_hash(:include => :friends) + end + def test_multiple_includes expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David", "address"=>{"street"=>"123 Lane", "city"=>"Springfield", "state"=>"CA", "zip"=>11111}, diff --git a/activemodel/test/cases/serializers/xml_serialization_test.rb b/activemodel/test/cases/serializers/xml_serialization_test.rb index 38aecf51ff..5fa227e0e0 100644 --- a/activemodel/test/cases/serializers/xml_serialization_test.rb +++ b/activemodel/test/cases/serializers/xml_serialization_test.rb @@ -188,6 +188,23 @@ class XmlSerializationTest < ActiveModel::TestCase assert_match %r{<friend type="Contact">}, xml end + class FriendList + def initialize(friends) + @friends = friends + end + + def to_ary + @friends + end + end + + test "include option with ary" do + @contact.friends = FriendList.new(@contact.friends) + xml = @contact.to_xml :include => :friends, :indent => 0 + assert_match %r{<friends type="array">}, xml + assert_match %r{<friend type="Contact">}, xml + end + test "multiple includes" do xml = @contact.to_xml :indent => 0, :skip_instruct => true, :include => [ :address, :friends ] assert xml.include?(@contact.address.to_xml(:indent => 0, :skip_instruct => true)) |