Merge pull request #837 from rails-api/store-options-in-array-serializers

Store options in array serializers
This commit is contained in:
Guillermo Iguaran 2015-03-11 15:50:52 -05:00
commit 4b32e3913c
5 changed files with 28 additions and 7 deletions

View File

@ -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

View File

@ -7,12 +7,14 @@ module ActiveModel
attr_reader :meta, :meta_key attr_reader :meta, :meta_key
def initialize(objects, options = {}) def initialize(objects, options = {})
options.merge!(root: nil)
@objects = objects.map do |object| @objects = objects.map do |object|
serializer_class = options.fetch( serializer_class = options.fetch(
:serializer, :serializer,
ActiveModel::Serializer.serializer_for(object) ActiveModel::Serializer.serializer_for(object)
) )
serializer_class.new(object) serializer_class.new(object, options)
end end
@meta = options[:meta] @meta = options[:meta]
@meta_key = options[:meta_key] @meta_key = options[:meta_key]

View File

@ -6,7 +6,7 @@ module ActiveModel
def setup def setup
@comment = Comment.new @comment = Comment.new
@post = Post.new @post = Post.new
@serializer = ArraySerializer.new([@comment, @post]) @serializer = ArraySerializer.new([@comment, @post], {some: :options})
end end
def test_respond_to_each def test_respond_to_each
@ -21,6 +21,8 @@ module ActiveModel
assert_kind_of PostSerializer, serializers.last assert_kind_of PostSerializer, serializers.last
assert_kind_of Post, serializers.last.object assert_kind_of Post, serializers.last.object
assert_equal serializers.last.custom_options[:some], :options
end end
end end
end end

View File

@ -78,6 +78,10 @@ PostSerializer = Class.new(ActiveModel::Serializer) do
def blog def blog
Blog.new(id: 999, name: "Custom blog") Blog.new(id: 999, name: "Custom blog")
end end
def custom_options
options
end
end end
SpammyPostSerializer = Class.new(ActiveModel::Serializer) do SpammyPostSerializer = Class.new(ActiveModel::Serializer) do
@ -95,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

View File

@ -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: {} },