Skip to content

Commit

Permalink
Merge pull request rails-api#913 from groyoh/fix_911
Browse files Browse the repository at this point in the history
Avoiding the serializer option when instantiating a new one for ArraySerializer Fixed rails-api#911
  • Loading branch information
joaomdmoura committed May 18, 2015
2 parents 46ae776 + a794a06 commit 1e4ff26
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/active_model/serializer/array_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def initialize(objects, options = {})
:serializer,
ActiveModel::Serializer.serializer_for(object)
)
serializer_class.new(object, options)
serializer_class.new(object, options.except(:serializer))
end
@meta = options[:meta]
@meta_key = options[:meta_key]
Expand Down
14 changes: 14 additions & 0 deletions test/adapter/json/collection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ def setup
ActionController::Base.cache_store.clear
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
expected = [{
title: "Hello!!",
Expand Down
6 changes: 6 additions & 0 deletions test/array_serializer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ def test_each_object_should_be_serialized_with_appropriate_serializer
assert_equal serializers.last.custom_options[:some], :options
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
meta_content = {meta: "the meta", meta_key: "the meta key"}
@serializer = ArraySerializer.new([@comment, @post], meta_content)
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/poro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,13 @@ def json_key
attribute :name, key: :title
end

CustomBlogSerializer = Class.new(ActiveModel::Serializer) do
attribute :id
attribute :special_attribute

has_many :articles
end

CommentPreviewSerializer = Class.new(ActiveModel::Serializer) do
attributes :id

Expand Down

0 comments on commit 1e4ff26

Please sign in to comment.