Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store options in array serializers #837

Merged
merged 2 commits into from
Mar 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,19 +176,20 @@ def attributes(options = {})
end

def each_association(&block)
self.class._associations.dup.each do |name, options|
self.class._associations.dup.each do |name, association_options|
next unless object

association = object.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(
association_value,
serializer_from_options(options)
serializer_from_options(association_options).merge(options)
) if serializer_class

if block_given?
block.call(name, serializer, options[:association_options])
block.call(name, serializer, association_options[:association_options])
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion lib/active_model/serializer/array_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ class ArraySerializer
attr_reader :meta, :meta_key

def initialize(objects, options = {})
options.merge!(root: nil)

@objects = objects.map do |object|
serializer_class = options.fetch(
:serializer,
ActiveModel::Serializer.serializer_for(object)
)
serializer_class.new(object)
serializer_class.new(object, options)
end
@meta = options[:meta]
@meta_key = options[:meta_key]
Expand Down
4 changes: 3 additions & 1 deletion test/array_serializer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class ArraySerializerTest < Minitest::Test
def setup
@comment = Comment.new
@post = Post.new
@serializer = ArraySerializer.new([@comment, @post])
@serializer = ArraySerializer.new([@comment, @post], {some: :options})
end

def test_respond_to_each
Expand All @@ -21,6 +21,8 @@ def test_each_object_should_be_serialized_with_appropriate_serializer

assert_kind_of PostSerializer, serializers.last
assert_kind_of Post, serializers.last.object

assert_equal serializers.last.custom_options[:some], :options
end
end
end
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/poro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ module Spam; end
def blog
Blog.new(id: 999, name: "Custom blog")
end

def custom_options
options
end
end

SpammyPostSerializer = Class.new(ActiveModel::Serializer) do
Expand All @@ -95,6 +99,10 @@ def self.root_name

belongs_to :post
belongs_to :author

def custom_options
options
end
end

AuthorSerializer = Class.new(ActiveModel::Serializer) do
Expand Down
10 changes: 9 additions & 1 deletion test/serializers/associations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def setup
@post.author = @author
@author.posts = [@post]

@post_serializer = PostSerializer.new(@post)
@post_serializer = PostSerializer.new(@post, {custom_options: true})
@author_serializer = AuthorSerializer.new(@author)
@comment_serializer = CommentSerializer.new(@comment)
end
Expand Down Expand Up @@ -65,6 +65,14 @@ def test_has_many_and_has_one
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
assert_equal(
{ post: { type: :belongs_to, association_options: {} },
Expand Down