-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
adds polymorphic option to association definition which includes asso… #1726
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,10 +8,17 @@ class PolymorphicTest < ActiveSupport::TestCase | |
@employee = Employee.new(id: 42, name: 'Zoop Zoopler', email: 'zoop@example.com') | ||
@picture = @employee.pictures.new(id: 1, title: 'headshot-1.jpg') | ||
@picture.imageable = @employee | ||
end | ||
|
||
def serialization(resource, adapter = :attributes) | ||
serializable(resource, adapter: adapter, serializer: PolymorphicBelongsToSerializer).as_json | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pulled out into a helper method, as serialization variations grew by 2 more for empty relationship testing |
||
end | ||
|
||
@attributes_serialization = serializable(@picture, serializer: PolymorphicBelongsToSerializer) # uses default adapter: attributes | ||
@json_serialization = serializable(@picture, adapter: :json, serializer: PolymorphicBelongsToSerializer) | ||
@json_api_serialization = serializable(@picture, adapter: :json_api, serializer: PolymorphicBelongsToSerializer) | ||
def tag_serialization(adapter = :attributes) | ||
tag = PolyTag.new(id: 1, phrase: 'foo') | ||
tag.object_tags << ObjectTag.new(id: 1, poly_tag_id: 1, taggable: @employee) | ||
tag.object_tags << ObjectTag.new(id: 5, poly_tag_id: 1, taggable: @picture) | ||
serializable(tag, adapter: adapter, serializer: PolymorphicTagSerializer, include: '*.*').as_json | ||
end | ||
|
||
def test_attributes_serialization | ||
|
@@ -20,31 +27,123 @@ def test_attributes_serialization | |
id: 1, | ||
title: 'headshot-1.jpg', | ||
imageable: { | ||
id: 42, | ||
name: 'Zoop Zoopler' | ||
type: 'employee', | ||
employee: { | ||
id: 42, | ||
name: 'Zoop Zoopler' | ||
} | ||
} | ||
} | ||
|
||
assert_equal(expected, @attributes_serialization.as_json) | ||
assert_equal(expected, serialization(@picture)) | ||
end | ||
|
||
def test_json_serializer | ||
def test_attributes_serialization_without_polymorphic_association | ||
expected = | ||
{ | ||
id: 2, | ||
title: 'headshot-2.jpg', | ||
imageable: nil | ||
} | ||
|
||
simple_picture = Picture.new(id: 2, title: 'headshot-2.jpg') | ||
assert_equal(expected, serialization(simple_picture)) | ||
end | ||
|
||
def test_attributes_serialization_with_polymorphic_has_many | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @beauby 'heterogeneous has-many" tests for :json and :attribute adapters added. If my understanding was correct, all the plumbing of the Attributes class acted as expected, tests demonstrate the nesting (which I feel is in alignment with previous versions [https://github.com//pull/1726#issuecomment-219323927]) |
||
expected = | ||
{ | ||
id: 1, | ||
phrase: 'foo', | ||
object_tags: [ | ||
{ | ||
id: 1, | ||
taggable: { | ||
type: 'employee', | ||
employee: { | ||
id: 42 | ||
} | ||
} | ||
}, | ||
{ | ||
id: 5, | ||
taggable: { | ||
type: 'picture', | ||
picture: { | ||
id: 1 | ||
} | ||
} | ||
} | ||
] | ||
} | ||
assert_equal(expected, tag_serialization) | ||
end | ||
|
||
def test_json_serialization | ||
expected = | ||
{ | ||
picture: { | ||
id: 1, | ||
title: 'headshot-1.jpg', | ||
imageable: { | ||
id: 42, | ||
name: 'Zoop Zoopler' | ||
type: 'employee', | ||
employee: { | ||
id: 42, | ||
name: 'Zoop Zoopler' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nested assoication |
||
} | ||
} | ||
} | ||
} | ||
|
||
assert_equal(expected, @json_serialization.as_json) | ||
assert_equal(expected, serialization(@picture, :json)) | ||
end | ||
|
||
def test_json_serialization_without_polymorphic_association | ||
expected = | ||
{ | ||
picture: { | ||
id: 2, | ||
title: 'headshot-2.jpg', | ||
imageable: nil | ||
} | ||
} | ||
|
||
simple_picture = Picture.new(id: 2, title: 'headshot-2.jpg') | ||
assert_equal(expected, serialization(simple_picture, :json)) | ||
end | ||
|
||
def test_json_serialization_with_polymorphic_has_many | ||
expected = | ||
{ | ||
poly_tag: { | ||
id: 1, | ||
phrase: 'foo', | ||
object_tags: [ | ||
{ | ||
id: 1, | ||
taggable: { | ||
type: 'employee', | ||
employee: { | ||
id: 42 | ||
} | ||
} | ||
}, | ||
{ | ||
id: 5, | ||
taggable: { | ||
type: 'picture', | ||
picture: { | ||
id: 1 | ||
} | ||
} | ||
} | ||
] | ||
} | ||
} | ||
assert_equal(expected, tag_serialization(:json)) | ||
end | ||
|
||
def test_json_api_serializer | ||
def test_json_api_serialization | ||
expected = | ||
{ | ||
data: { | ||
|
@@ -64,7 +163,7 @@ def test_json_api_serializer | |
} | ||
} | ||
|
||
assert_equal(expected, @json_api_serialization.as_json) | ||
assert_equal(expected, serialization(@picture, :json_api)) | ||
end | ||
end | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,10 +70,21 @@ def cache_key | |
|
||
class Employee < ActiveRecord::Base | ||
has_many :pictures, as: :imageable | ||
has_many :object_tags, as: :taggable | ||
end | ||
|
||
class ObjectTag < ActiveRecord::Base | ||
belongs_to :poly_tag | ||
belongs_to :taggable, polymorphic: true | ||
end | ||
|
||
class Picture < ActiveRecord::Base | ||
belongs_to :imageable, polymorphic: true | ||
has_many :object_tags, as: :taggable | ||
end | ||
|
||
class PolyTag < ActiveRecord::Base | ||
has_many :object_tags | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There were other tests using a non-active record version of Tags. Didnt want to do to much upheaval to that pattern so made a new Tag Class |
||
end | ||
|
||
module Spam; end | ||
|
@@ -245,7 +256,23 @@ def maker | |
PolymorphicBelongsToSerializer = Class.new(ActiveModel::Serializer) do | ||
attributes :id, :title | ||
|
||
has_one :imageable, serializer: PolymorphicHasManySerializer | ||
has_one :imageable, serializer: PolymorphicHasManySerializer, polymorphic: true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. optional association polymorphic: flag |
||
end | ||
|
||
PolymorphicSimpleSerializer = Class.new(ActiveModel::Serializer) do | ||
attributes :id | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. simple serializer to demonstrate polymorph inclusions (as they didnt have their own) |
||
|
||
PolymorphicObjectTagSerializer = Class.new(ActiveModel::Serializer) do | ||
attributes :id | ||
|
||
has_many :taggable, serializer: PolymorphicSimpleSerializer, polymorphic: true | ||
end | ||
|
||
PolymorphicTagSerializer = Class.new(ActiveModel::Serializer) do | ||
attributes :id, :phrase | ||
|
||
has_many :object_tags, serializer: PolymorphicObjectTagSerializer | ||
end | ||
|
||
Spam::UnrelatedLinkSerializer = Class.new(ActiveModel::Serializer) do | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@beauby good call, added check for empty relationship which wont wrap the resultant in a polymorphic type if there is no association (corresponding tests added for :json and :attributes adapters)