mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
parent
60b5901af8
commit
99677c0c58
@ -156,7 +156,7 @@ end
|
||||
end
|
||||
|
||||
def serialize(association, object)
|
||||
association.build_serializer(object).serializable_object
|
||||
association.build_serializer(object, scope: scope).serializable_object
|
||||
end
|
||||
|
||||
def serialize_ids(association)
|
||||
|
||||
@ -34,6 +34,17 @@ module ActiveModel
|
||||
@embed_objects = embed == :object || embed == :objects
|
||||
end
|
||||
|
||||
def build_serializer(object, options = {})
|
||||
@serializer_class.new(object, options.merge(@options))
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def use_array_serializer!
|
||||
@options.merge!(each_serializer: @serializer_class)
|
||||
@serializer_class = ArraySerializer
|
||||
end
|
||||
|
||||
class HasOne < Association
|
||||
def initialize(name, *args)
|
||||
super
|
||||
@ -41,13 +52,14 @@ module ActiveModel
|
||||
@key ||= "#{name}_id"
|
||||
end
|
||||
|
||||
def build_serializer(object)
|
||||
def build_serializer(object, options = {})
|
||||
if object.respond_to?(:to_ary)
|
||||
@options.merge!(each_serializer: @serializer_class)
|
||||
@serializer_class = ArraySerializer
|
||||
use_array_serializer!
|
||||
else
|
||||
@serializer_class ||= Serializer.serializer_for(object) || DefaultSerializer
|
||||
end
|
||||
@serializer_class ||= Serializer.serializer_for(object) || DefaultSerializer
|
||||
@serializer_class.new(object, @options)
|
||||
|
||||
super
|
||||
end
|
||||
end
|
||||
|
||||
@ -58,13 +70,14 @@ module ActiveModel
|
||||
@key ||= "#{name.to_s.singularize}_ids"
|
||||
end
|
||||
|
||||
def build_serializer(object)
|
||||
def build_serializer(object, options = {})
|
||||
if @serializer_class && !(@serializer_class <= ArraySerializer)
|
||||
@options.merge!(each_serializer: @serializer_class)
|
||||
@serializer_class = ArraySerializer
|
||||
use_array_serializer!
|
||||
else
|
||||
@serializer_class ||= ArraySerializer
|
||||
end
|
||||
@serializer_class ||= ArraySerializer
|
||||
@serializer_class.new(object, @options)
|
||||
|
||||
super
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -17,5 +17,33 @@ module ActiveModel
|
||||
'user'
|
||||
end
|
||||
end
|
||||
|
||||
class NestedScopeTest < ActiveModel::TestCase
|
||||
def setup
|
||||
@association = UserSerializer._associations[:profile]
|
||||
@old_association = @association.dup
|
||||
@user = User.new({ name: 'Name 1', email: 'mail@server.com', gender: 'M' })
|
||||
@user_serializer = UserSerializer.new(@user, scope: 'user')
|
||||
end
|
||||
|
||||
def teardown
|
||||
UserSerializer._associations[:profile] = @old_association
|
||||
end
|
||||
|
||||
def test_scope_passed_through
|
||||
@association.serializer_class = Class.new(ActiveModel::Serializer) do
|
||||
def name
|
||||
scope
|
||||
end
|
||||
|
||||
attributes :name
|
||||
end
|
||||
|
||||
assert_equal({
|
||||
name: 'Name 1', email: 'mail@server.com', profile: { name: 'user' }
|
||||
}, @user_serializer.serializable_hash)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user