Skip to content

Commit 1272480

Browse files
committed
Merge branch 'Hirurg103-0-10-stable' into 0-10-stable
2 parents eb865c2 + db4e526 commit 1272480

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

lib/active_model/serializer/lazy_association.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ def instantiate_serializer(object)
7676
serializer_options[:serializer_context_class] = association_options.fetch(:parent_serializer).class
7777
serializer = reflection_options.fetch(:serializer, nil)
7878
serializer_options[:serializer] = serializer if serializer
79+
serializer_options[:namespace] = reflection_options[:namespace] if reflection_options[:namespace]
7980
serializer_class.new(object, serializer_options)
8081
end
8182

test/serializers/associations_test.rb

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,56 @@ def test_associations_namespaced_resources
285285
end
286286
end
287287

288+
class AssociationsNamespacedSerializersTest < ActiveSupport::TestCase
289+
class Post < ::Model
290+
associations :comments, :author, :description
291+
292+
def latest_comments
293+
comments[0..3]
294+
end
295+
end
296+
class Comment < ::Model; end
297+
class Author < ::Model; end
298+
class Description < ::Model; end
299+
300+
class ResourceNamespace
301+
class PostSerializer < ActiveModel::Serializer
302+
has_many :comments, namespace: ResourceNamespace
303+
has_many :latest_comments, namespace: ResourceNamespace
304+
belongs_to :author, namespace: ResourceNamespace
305+
has_one :description, namespace: ResourceNamespace
306+
end
307+
class CommentSerializer < ActiveModel::Serializer; end
308+
class AuthorSerializer < ActiveModel::Serializer; end
309+
class DescriptionSerializer < ActiveModel::Serializer; end
310+
end
311+
312+
def setup
313+
@comment = Comment.new
314+
@author = Author.new
315+
@description = Description.new
316+
@post = Post.new(comments: [@comment],
317+
author: @author,
318+
description: @description)
319+
@post_serializer = ResourceNamespace::PostSerializer.new(@post)
320+
end
321+
322+
def test_associations_namespaced_serializers
323+
@post_serializer.associations.each do |association|
324+
case association.key
325+
when :comments, :latest_comments
326+
assert_instance_of(ResourceNamespace::CommentSerializer, association.lazy_association.serializer.first)
327+
when :author
328+
assert_instance_of(ResourceNamespace::AuthorSerializer, association.lazy_association.serializer)
329+
when :description
330+
assert_instance_of(ResourceNamespace::DescriptionSerializer, association.lazy_association.serializer)
331+
else
332+
flunk "Unknown association: #{key}"
333+
end
334+
end
335+
end
336+
end
337+
288338
class NestedSerializersTest < ActiveSupport::TestCase
289339
class Post < ::Model
290340
associations :comments, :author, :description

0 commit comments

Comments
 (0)