mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Passes serializer options down into associations
This commit is contained in:
parent
bcd3844e58
commit
af81a403e3
@ -176,19 +176,20 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def each_association(&block)
|
def each_association(&block)
|
||||||
self.class._associations.dup.each do |name, options|
|
self.class._associations.dup.each do |name, association_options|
|
||||||
next unless object
|
next unless object
|
||||||
|
|
||||||
association = object.send(name)
|
association = object.send(name)
|
||||||
association_value = send(name)
|
association_value = send(name)
|
||||||
serializer_class = ActiveModel::Serializer.serializer_for(association, options)
|
serializer_class = ActiveModel::Serializer.serializer_for(association, association_options)
|
||||||
|
|
||||||
serializer = serializer_class.new(
|
serializer = serializer_class.new(
|
||||||
association_value,
|
association_value,
|
||||||
serializer_from_options(options)
|
serializer_from_options(association_options).merge(options)
|
||||||
) if serializer_class
|
) if serializer_class
|
||||||
|
|
||||||
if block_given?
|
if block_given?
|
||||||
block.call(name, serializer, options[:association_options])
|
block.call(name, serializer, association_options[:association_options])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
4
test/fixtures/poro.rb
vendored
4
test/fixtures/poro.rb
vendored
@ -99,6 +99,10 @@ CommentSerializer = Class.new(ActiveModel::Serializer) do
|
|||||||
|
|
||||||
belongs_to :post
|
belongs_to :post
|
||||||
belongs_to :author
|
belongs_to :author
|
||||||
|
|
||||||
|
def custom_options
|
||||||
|
options
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
AuthorSerializer = Class.new(ActiveModel::Serializer) do
|
AuthorSerializer = Class.new(ActiveModel::Serializer) do
|
||||||
|
|||||||
@ -37,7 +37,7 @@ module ActiveModel
|
|||||||
@post.author = @author
|
@post.author = @author
|
||||||
@author.posts = [@post]
|
@author.posts = [@post]
|
||||||
|
|
||||||
@post_serializer = PostSerializer.new(@post)
|
@post_serializer = PostSerializer.new(@post, {custom_options: true})
|
||||||
@author_serializer = AuthorSerializer.new(@author)
|
@author_serializer = AuthorSerializer.new(@author)
|
||||||
@comment_serializer = CommentSerializer.new(@comment)
|
@comment_serializer = CommentSerializer.new(@comment)
|
||||||
end
|
end
|
||||||
@ -65,6 +65,14 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_serializer_options_are_passed_into_associations_serializers
|
||||||
|
@post_serializer.each_association do |name, association|
|
||||||
|
if name == :comments
|
||||||
|
assert association.first.custom_options[:custom_options]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def test_belongs_to
|
def test_belongs_to
|
||||||
assert_equal(
|
assert_equal(
|
||||||
{ post: { type: :belongs_to, association_options: {} },
|
{ post: { type: :belongs_to, association_options: {} },
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user