mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
test for namespaced associations + bug fixed
This commit is contained in:
parent
dde1492934
commit
b297f17e53
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
4
test/fixtures/poro.rb
vendored
4
test/fixtures/poro.rb
vendored
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user