Skip to content

Commit

Permalink
Add test for nested included polymoprhic associations
Browse files Browse the repository at this point in the history
  • Loading branch information
twinturbo committed Jul 15, 2012
1 parent c4e5cd5 commit cbd7d7d
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions test/serializer_test.rb
Expand Up @@ -1093,7 +1093,10 @@ def test_multiple_polymorphic_associations
end

orange_serializer = Class.new(ActiveModel::Serializer) do
embed :ids, :include => true

attributes :plu, :id
has_one :readable, :polymorphic => true
end

email_class = Class.new(Model) do
Expand All @@ -1111,8 +1114,12 @@ def self.to_s
"Orange"
end

def readable
@attributes[:readable]
end

define_method :active_model_serializer do
orange_serializer
orange_serializer
end
end

Expand All @@ -1128,7 +1135,7 @@ def self.to_s
end

email = email_class.new :id => 1, :subject => "Hello", :body => "World"
orange = orange_class.new :id => 1, :plu => "3027"
orange = orange_class.new :id => 1, :plu => "3027", readable: email

attachment = Attachment.new({
:name => 'logo.png',
Expand All @@ -1141,22 +1148,25 @@ def self.to_s
actual = attachment_serializer.new(attachment, {}).as_json

assert_equal({
:emails => [{
:subject => "Hello",
:body => "World",
:id => 1
}],

:oranges => [{
:plu => "3027",
:id => 1,
:readable => { :email => 1 }
}],

:attachment => {
:name => 'logo.png',
:url => 'http://example.com/logo.png',
:attachable => { :email => 1 },
:readable => { :email => 1 },
:edible => { :orange => 1 }
},
:emails => [{
:id => 1,
:subject => "Hello",
:body => "World"
}],
:oranges => [{
:id => 1,
:plu => "3027"
}]
}
}, actual)
end
end

0 comments on commit cbd7d7d

Please sign in to comment.