mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 15:23:06 +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
@@ -155,15 +155,17 @@ module ActiveModelSerializers
|
||||
|
||||
serializable_resource_options = {} # adapter.instance_options
|
||||
|
||||
meta = test_options.delete(:meta)
|
||||
options = test_options.delete(:options)
|
||||
links = test_options.delete(:links)
|
||||
options = test_options.delete(:options) || {}
|
||||
options[:links] = test_options.delete(:links)
|
||||
options[:meta] = test_options.delete(:meta)
|
||||
association_serializer = @serializer
|
||||
if association_serializer && association_serializer.object
|
||||
association_name = association_serializer.json_key.to_sym
|
||||
association = ::ActiveModel::Serializer::Association.new(association_name, association_serializer, options, links, meta)
|
||||
options[:serializer] = association_serializer
|
||||
association = ::ActiveModel::Serializer::Association.new(association_name, options, nil)
|
||||
else
|
||||
association = ::ActiveModel::Serializer::Association.new(:association_name_not_used, association, options, links, meta)
|
||||
options[:serializer] = association
|
||||
association = ::ActiveModel::Serializer::Association.new(:association_name_not_used, options, nil)
|
||||
end
|
||||
|
||||
relationship = Relationship.new(parent_serializer, serializable_resource_options, association)
|
||||
|
||||
@@ -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