Skip to content

Commit

Permalink
Merge 54235f9 into c8cfe94
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Harman committed Jan 3, 2014
2 parents c8cfe94 + 54235f9 commit 6f8947d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/active_model/array_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def initialize(object, options={})
@root = options.fetch(:root, self.class._root)
@meta_key = options[:meta_key] || :meta
@meta = options[@meta_key]
@each_serializer = options[:each_serializer]
@each_serializer = options.delete(:each_serializer)
@options = options.merge(root: nil)
end
attr_accessor :object, :root, :meta_key, :meta, :options
Expand Down
2 changes: 1 addition & 1 deletion lib/active_model/serializer/associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def initialize(name, options={})
@key = options[:key]
@embedded_key = options[:root] || name

serializer = @options[:serializer]
serializer = @options.delete(:serializer)
@serializer_class = serializer.is_a?(String) ? serializer.constantize : serializer
end

Expand Down
13 changes: 13 additions & 0 deletions test/fixtures/poro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ def comments
class Comment < Model
end

class Category < Model
def posts
@attributes[:posts] ||= [Post.new(title: 'First', body: 'Post 1'),
Post.new(title: 'Second', body: 'Post 2')]
end
end

###
## Serializers
###
Expand Down Expand Up @@ -67,3 +74,9 @@ class PostSerializer < ActiveModel::Serializer
class CommentSerializer < ActiveModel::Serializer
attributes :content
end

class CategorySerializer < ActiveModel::Serializer
attributes :name

has_many :posts
end
17 changes: 17 additions & 0 deletions test/unit/active_model/serializer/associations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,22 @@ def test_associations_inheritance
another_inherited_serializer_klass._associations.keys)
end
end

class AssociationsWithCustomSerializer < ActiveModel::TestCase
def setup
post = Post.new(title: 'Hi', description: 'How are you?', comments: [Comment.new])
@category = Category.new(name: 'Hello, World!', posts: [post])
end

def test_does_not_pass_custom_serializer_option_to_nested_associations
category_serializer = Class.new(CategorySerializer) do
has_many :posts, serializer: PostSerializer
end
serializer = category_serializer.new(@category)
comments = serializer.associations[:posts].first[:comments]

assert_equal([{content:'C1'}, {content:'C2'}], comments)
end
end
end
end

0 comments on commit 6f8947d

Please sign in to comment.