From b297f17e532411db95bdf93469b38a07bcd155d7 Mon Sep 17 00:00:00 2001 From: Theodore Konukhov Date: Fri, 29 Aug 2014 06:11:40 +0200 Subject: [PATCH] test for namespaced associations + bug fixed --- lib/active_model/serializer.rb | 2 +- lib/active_model/serializer/associations.rb | 12 ++++++------ test/fixtures/poro.rb | 4 +--- .../associations/build_serializer_test.rb | 15 +++++++++++++++ 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/lib/active_model/serializer.rb b/lib/active_model/serializer.rb index 3913a5c3..48a34bf0 100644 --- a/lib/active_model/serializer.rb +++ b/lib/active_model/serializer.rb @@ -116,7 +116,7 @@ end def build_serializer_class(resource, options) "".tap do |klass_name| klass_name << "#{options[:namespace]}::" if options[:namespace] - klass_name << prefix.to_s.classify if options[:prefix] + klass_name << options[:prefix].to_s.classify if options[:prefix] klass_name << "#{resource.class.name}Serializer" end end diff --git a/lib/active_model/serializer/associations.rb b/lib/active_model/serializer/associations.rb index 44d7c84f..da6271a1 100644 --- a/lib/active_model/serializer/associations.rb +++ b/lib/active_model/serializer/associations.rb @@ -38,8 +38,8 @@ module ActiveModel @embed_objects = embed == :object || embed == :objects end - def serializer_from_object(object) - Serializer.serializer_for(object) + def serializer_from_object(object, options = {}) + Serializer.serializer_for(object, options) end def default_serializer @@ -47,7 +47,7 @@ module ActiveModel end def build_serializer(object, options = {}) - serializer_class(object).new(object, options.merge(self.options)) + serializer_class(object, options).new(object, options.merge(self.options)) end class HasOne < Association @@ -57,8 +57,8 @@ module ActiveModel @key ||= "#{name}_id" end - def serializer_class(object) - serializer_from_options || serializer_from_object(object) || default_serializer + def serializer_class(object, options = {}) + serializer_from_options || serializer_from_object(object, options) || default_serializer end def build_serializer(object, options = {}) @@ -74,7 +74,7 @@ module ActiveModel @key ||= "#{name.to_s.singularize}_ids" end - def serializer_class(object) + def serializer_class(object, _ = {}) if use_array_serializer? ArraySerializer else diff --git a/test/fixtures/poro.rb b/test/fixtures/poro.rb index de3c2037..5ad9dd82 100644 --- a/test/fixtures/poro.rb +++ b/test/fixtures/poro.rb @@ -74,9 +74,7 @@ class WebLogLowerCamelSerializer < WebLogSerializer format_keys :lower_camel end -class ShortUserSerializer < ActiveModel::Serializer - attributes :name -end +class ShortProfileSerializer < ::ProfileSerializer; end module TestNamespace class ProfileSerializer < ::ProfileSerializer; end diff --git a/test/unit/active_model/serializer/associations/build_serializer_test.rb b/test/unit/active_model/serializer/associations/build_serializer_test.rb index 2848badf..414a400f 100644 --- a/test/unit/active_model/serializer/associations/build_serializer_test.rb +++ b/test/unit/active_model/serializer/associations/build_serializer_test.rb @@ -7,6 +7,7 @@ module ActiveModel def setup @association = Association::HasOne.new('post', serializer: PostSerializer) @post = Post.new({ title: 'Title 1', body: 'Body 1', date: '1/1/2000' }) + @user = User.new end def test_build_serializer_for_array_called_twice @@ -15,6 +16,20 @@ module ActiveModel assert_instance_of(PostSerializer, serializer) end end + + def test_build_serializer_from_in_a_namespace + assoc = Association::HasOne.new('profile') + serializer = TestNamespace::UserSerializer.new(@user).build_serializer(assoc) + + assert_instance_of(TestNamespace::ProfileSerializer, serializer) + end + + def test_build_serializer_with_prefix + assoc = Association::HasOne.new('profile', prefix: :short) + serializer = UserSerializer.new(@user).build_serializer(assoc) + + assert_instance_of(ShortProfileSerializer, serializer) + end end end end