mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Refactor Association into Field like everything else (#1897)
* Make assocations asserts easier to understand * Refactor Association into Field like everything else * Make assocation serializer/links/meta lazier * Push association deeper into relationship * Simplify association usage in relationships * Better naming of reflection parent serializer * Easier to read association method
This commit is contained in:
committed by
L. Preston Sego III
parent
7d2997b3ff
commit
20e394d512
@@ -31,13 +31,13 @@ module ActiveModel
|
||||
|
||||
case key
|
||||
when :posts
|
||||
assert_equal({ include_data: true }, options)
|
||||
assert_equal true, options.fetch(:include_data)
|
||||
assert_kind_of(ActiveModelSerializers.config.collection_serializer, serializer)
|
||||
when :bio
|
||||
assert_equal({ include_data: true }, options)
|
||||
assert_equal true, options.fetch(:include_data)
|
||||
assert_nil serializer
|
||||
when :roles
|
||||
assert_equal({ include_data: true }, options)
|
||||
assert_equal true, options.fetch(:include_data)
|
||||
assert_kind_of(ActiveModelSerializers.config.collection_serializer, serializer)
|
||||
else
|
||||
flunk "Unknown association: #{key}"
|
||||
@@ -79,7 +79,7 @@ module ActiveModel
|
||||
flunk "Unknown association: #{key}"
|
||||
end
|
||||
|
||||
assert_equal({ include_data: true }, association.options)
|
||||
assert_equal true, association.options.fetch(:include_data)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -291,11 +291,23 @@ module ActiveModel
|
||||
end
|
||||
|
||||
class InheritedSerializerTest < ActiveSupport::TestCase
|
||||
class PostSerializer < ActiveModel::Serializer
|
||||
belongs_to :author
|
||||
has_many :comments
|
||||
belongs_to :blog
|
||||
end
|
||||
|
||||
class InheritedPostSerializer < PostSerializer
|
||||
belongs_to :author, polymorphic: true
|
||||
has_many :comments, key: :reviews
|
||||
end
|
||||
|
||||
class AuthorSerializer < ActiveModel::Serializer
|
||||
has_many :posts
|
||||
has_many :roles
|
||||
has_one :bio
|
||||
end
|
||||
|
||||
class InheritedAuthorSerializer < AuthorSerializer
|
||||
has_many :roles, polymorphic: true
|
||||
has_one :bio, polymorphic: true
|
||||
@@ -333,9 +345,18 @@ module ActiveModel
|
||||
end
|
||||
|
||||
test 'a serializer inheriting from another serializer can redefine belongs_to associations' do
|
||||
expected = [:author, :comments, :blog].sort
|
||||
result = (@inherited_post_associations - @post_associations).map(&:name).sort
|
||||
assert_equal(result, expected)
|
||||
assert_equal [:author, :comments, :blog], @post_associations.map(&:name)
|
||||
assert_equal [:author, :comments, :blog, :comments], @inherited_post_associations.map(&:name)
|
||||
|
||||
refute @post_associations.detect { |assoc| assoc.name == :author }.options.key?(:polymorphic)
|
||||
assert_equal true, @inherited_post_associations.detect { |assoc| assoc.name == :author }.options.fetch(:polymorphic)
|
||||
|
||||
refute @post_associations.detect { |assoc| assoc.name == :comments }.options.key?(:key)
|
||||
original_comment_assoc, new_comments_assoc = @inherited_post_associations.select { |assoc| assoc.name == :comments }
|
||||
refute original_comment_assoc.options.key?(:key)
|
||||
assert_equal :reviews, new_comments_assoc.options.fetch(:key)
|
||||
|
||||
assert_equal @post_associations.detect { |assoc| assoc.name == :blog }, @inherited_post_associations.detect { |assoc| assoc.name == :blog }
|
||||
end
|
||||
|
||||
test 'a serializer inheriting from another serializer can have an additional association with the same name but with different key' do
|
||||
|
||||
Reference in New Issue
Block a user