Merge pull request #913 from groyoh/fix_911

Avoiding the serializer option when instantiating a new one for ArraySerializer Fixed #911
This commit is contained in:
João Moura 2015-05-18 10:25:31 -03:00
commit 1e4ff264cf
4 changed files with 28 additions and 1 deletions

View File

@ -14,7 +14,7 @@ module ActiveModel
:serializer, :serializer,
ActiveModel::Serializer.serializer_for(object) ActiveModel::Serializer.serializer_for(object)
) )
serializer_class.new(object, options) serializer_class.new(object, options.except(:serializer))
end end
@meta = options[:meta] @meta = options[:meta]
@meta_key = options[:meta_key] @meta_key = options[:meta_key]

View File

@ -22,6 +22,20 @@ module ActiveModel
ActionController::Base.cache_store.clear ActionController::Base.cache_store.clear
end end
def test_with_serializer_option
@blog.special_attribute = "Special"
@blog.articles = [@first_post, @second_post]
@serializer = ArraySerializer.new([@blog], serializer: CustomBlogSerializer)
@adapter = ActiveModel::Serializer::Adapter::Json.new(@serializer)
expected = [{
id: 1,
special_attribute: "Special",
articles: [{id: 1,title: "Hello!!", body: "Hello, world!!"}, {id: 2, title: "New Post", body: "Body"}]
}]
assert_equal expected, @adapter.serializable_hash
end
def test_include_multiple_posts def test_include_multiple_posts
expected = [{ expected = [{
title: "Hello!!", title: "Hello!!",

View File

@ -25,6 +25,12 @@ module ActiveModel
assert_equal serializers.last.custom_options[:some], :options assert_equal serializers.last.custom_options[:some], :options
end end
def test_serializer_option_not_passed_to_each_serializer
serializers = ArraySerializer.new([@post], {serializer: PostSerializer}).to_a
refute serializers.first.custom_options.key?(:serializer)
end
def test_meta_and_meta_key_attr_readers def test_meta_and_meta_key_attr_readers
meta_content = {meta: "the meta", meta_key: "the meta key"} meta_content = {meta: "the meta", meta_key: "the meta key"}
@serializer = ArraySerializer.new([@comment, @post], meta_content) @serializer = ArraySerializer.new([@comment, @post], meta_content)

View File

@ -178,6 +178,13 @@ AlternateBlogSerializer = Class.new(ActiveModel::Serializer) do
attribute :name, key: :title attribute :name, key: :title
end end
CustomBlogSerializer = Class.new(ActiveModel::Serializer) do
attribute :id
attribute :special_attribute
has_many :articles
end
CommentPreviewSerializer = Class.new(ActiveModel::Serializer) do CommentPreviewSerializer = Class.new(ActiveModel::Serializer) do
attributes :id attributes :id