mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
has_one returns arrays with roots pluralized
This commit is contained in:
parent
1eea008336
commit
4d4b820cbe
@ -164,7 +164,12 @@ end
|
||||
association.serializer_class <= ArraySerializer)
|
||||
associated_data.map { |elem| association.build_serializer(elem).serializable_hash }
|
||||
else
|
||||
association.build_serializer(associated_data).serializable_object
|
||||
serializable_obj = association.build_serializer(associated_data).serializable_object
|
||||
if !(association.serializer_class && association.serializer_class <= ArraySerializer)
|
||||
serializable_obj = [serializable_obj]
|
||||
serializable_obj.compact!
|
||||
end
|
||||
serializable_obj
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@ -17,7 +17,6 @@ module ActiveModel
|
||||
@embed_in_root = options.fetch(:embed_in_root) { options.fetch(:include) { CONFIG.embed_in_root } }
|
||||
@embed_key = options[:embed_key] || :id
|
||||
@key = options[:key]
|
||||
@embedded_key = options[:root] || name
|
||||
|
||||
self.serializer_class = @options[:serializer]
|
||||
end
|
||||
@ -43,16 +42,18 @@ module ActiveModel
|
||||
end
|
||||
|
||||
class HasOne < Association
|
||||
def initialize(*args)
|
||||
def initialize(name, *args)
|
||||
super
|
||||
@key ||= "#{name}_id"
|
||||
@embedded_key = "#{@options[:root] || name}".pluralize
|
||||
@key ||= "#{name}_id"
|
||||
end
|
||||
end
|
||||
|
||||
class HasMany < Association
|
||||
def initialize(*args)
|
||||
def initialize(name, *args)
|
||||
super
|
||||
@key ||= "#{name.singularize}_ids"
|
||||
@embedded_key = @options[:root] || name
|
||||
@key ||= "#{name.to_s.singularize}_ids"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -17,7 +17,7 @@ module ActiveModel
|
||||
ar_comments: [{ body: 'what a dumb post', ar_tags: [{ name: 'short' }, { name: 'whiny' }] },
|
||||
{ body: 'i liked it', ar_tags: [{:name=>"short"}, {:name=>"happy"}] }],
|
||||
ar_tags: [{ name: 'short' }, { name: 'whiny' }, { name: 'happy' }],
|
||||
ar_section: { 'name' => 'ruby' }
|
||||
'ar_sections' => [{ 'name' => 'ruby' }]
|
||||
}
|
||||
}, post_serializer.as_json)
|
||||
end
|
||||
@ -51,7 +51,7 @@ module ActiveModel
|
||||
ar_comments: [{ body: 'what a dumb post', ar_tags: [{ name: 'short' }, { name: 'whiny' }] },
|
||||
{ body: 'i liked it', ar_tags: [{:name=>"short"}, {:name=>"happy"}] }],
|
||||
ar_tags: [{ name: 'short' }, { name: 'whiny' }, { name: 'happy' }],
|
||||
ar_section: { 'name' => 'ruby' }
|
||||
'ar_sections' => [{ 'name' => 'ruby' }]
|
||||
}, post_serializer.as_json)
|
||||
end
|
||||
end
|
||||
|
||||
@ -50,7 +50,7 @@ module ActiveModel
|
||||
@association.embed = :objects
|
||||
|
||||
assert_equal({
|
||||
name: 'Name 1', email: 'mail@server.com', profile: { name: 'N1', description: 'D1' }
|
||||
name: 'Name 1', email: 'mail@server.com', 'profiles' => [{ name: 'N1', description: 'D1' }]
|
||||
}, @user_serializer.serializable_hash)
|
||||
end
|
||||
|
||||
@ -58,7 +58,7 @@ module ActiveModel
|
||||
@association.embed = :objects
|
||||
|
||||
assert_equal({
|
||||
'user' => { name: 'Name 1', email: 'mail@server.com', profile: { name: 'N1', description: 'D1' } }
|
||||
'user' => { name: 'Name 1', email: 'mail@server.com', 'profiles' => [{ name: 'N1', description: 'D1' }] }
|
||||
}, @user_serializer.as_json)
|
||||
end
|
||||
|
||||
@ -84,7 +84,7 @@ module ActiveModel
|
||||
end
|
||||
|
||||
assert_equal({
|
||||
'user' => { name: 'Name 1', email: 'mail@server.com', profile: nil }
|
||||
'user' => { name: 'Name 1', email: 'mail@server.com', 'profiles' => [] }
|
||||
}, @user_serializer.as_json)
|
||||
end
|
||||
|
||||
@ -93,7 +93,7 @@ module ActiveModel
|
||||
@association.embedded_key = 'root'
|
||||
|
||||
assert_equal({
|
||||
name: 'Name 1', email: 'mail@server.com', 'root' => { name: 'N1', description: 'D1' }
|
||||
name: 'Name 1', email: 'mail@server.com', 'root' => [{ name: 'N1', description: 'D1' }]
|
||||
}, @user_serializer.serializable_hash)
|
||||
end
|
||||
|
||||
@ -112,7 +112,7 @@ module ActiveModel
|
||||
|
||||
assert_equal({
|
||||
'user' => { name: 'Name 1', email: 'mail@server.com', 'profile_id' => @user.profile.object_id },
|
||||
profile: { name: 'N1', description: 'D1' }
|
||||
'profiles' => [{ name: 'N1', description: 'D1' }]
|
||||
}, @user_serializer.as_json)
|
||||
end
|
||||
|
||||
@ -129,7 +129,7 @@ module ActiveModel
|
||||
|
||||
assert_equal({
|
||||
'user' => { name: 'Name 1', email: 'mail@server.com', 'profile_id' => @user.profile.object_id },
|
||||
profile: { name: 'fake' }
|
||||
'profiles' => [{ name: 'fake' }]
|
||||
}, @user_serializer.as_json)
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user