has_one returns arrays with roots pluralized

This commit is contained in:
Santiago Pastorino 2013-10-30 15:17:26 -02:00
parent 1eea008336
commit 4d4b820cbe
4 changed files with 20 additions and 14 deletions

View File

@ -164,7 +164,12 @@ end
association.serializer_class <= ArraySerializer) association.serializer_class <= ArraySerializer)
associated_data.map { |elem| association.build_serializer(elem).serializable_hash } associated_data.map { |elem| association.build_serializer(elem).serializable_hash }
else 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
end end

View File

@ -17,7 +17,6 @@ module ActiveModel
@embed_in_root = options.fetch(:embed_in_root) { options.fetch(:include) { CONFIG.embed_in_root } } @embed_in_root = options.fetch(:embed_in_root) { options.fetch(:include) { CONFIG.embed_in_root } }
@embed_key = options[:embed_key] || :id @embed_key = options[:embed_key] || :id
@key = options[:key] @key = options[:key]
@embedded_key = options[:root] || name
self.serializer_class = @options[:serializer] self.serializer_class = @options[:serializer]
end end
@ -43,16 +42,18 @@ module ActiveModel
end end
class HasOne < Association class HasOne < Association
def initialize(*args) def initialize(name, *args)
super super
@embedded_key = "#{@options[:root] || name}".pluralize
@key ||= "#{name}_id" @key ||= "#{name}_id"
end end
end end
class HasMany < Association class HasMany < Association
def initialize(*args) def initialize(name, *args)
super super
@key ||= "#{name.singularize}_ids" @embedded_key = @options[:root] || name
@key ||= "#{name.to_s.singularize}_ids"
end end
end end
end end

View File

@ -17,7 +17,7 @@ module ActiveModel
ar_comments: [{ body: 'what a dumb post', ar_tags: [{ name: 'short' }, { name: 'whiny' }] }, ar_comments: [{ body: 'what a dumb post', ar_tags: [{ name: 'short' }, { name: 'whiny' }] },
{ body: 'i liked it', ar_tags: [{:name=>"short"}, {:name=>"happy"}] }], { body: 'i liked it', ar_tags: [{:name=>"short"}, {:name=>"happy"}] }],
ar_tags: [{ name: 'short' }, { name: 'whiny' }, { name: 'happy' }], ar_tags: [{ name: 'short' }, { name: 'whiny' }, { name: 'happy' }],
ar_section: { 'name' => 'ruby' } 'ar_sections' => [{ 'name' => 'ruby' }]
} }
}, post_serializer.as_json) }, post_serializer.as_json)
end end
@ -51,7 +51,7 @@ module ActiveModel
ar_comments: [{ body: 'what a dumb post', ar_tags: [{ name: 'short' }, { name: 'whiny' }] }, ar_comments: [{ body: 'what a dumb post', ar_tags: [{ name: 'short' }, { name: 'whiny' }] },
{ body: 'i liked it', ar_tags: [{:name=>"short"}, {:name=>"happy"}] }], { body: 'i liked it', ar_tags: [{:name=>"short"}, {:name=>"happy"}] }],
ar_tags: [{ name: 'short' }, { name: 'whiny' }, { name: 'happy' }], ar_tags: [{ name: 'short' }, { name: 'whiny' }, { name: 'happy' }],
ar_section: { 'name' => 'ruby' } 'ar_sections' => [{ 'name' => 'ruby' }]
}, post_serializer.as_json) }, post_serializer.as_json)
end end
end end

View File

@ -50,7 +50,7 @@ module ActiveModel
@association.embed = :objects @association.embed = :objects
assert_equal({ 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) }, @user_serializer.serializable_hash)
end end
@ -58,7 +58,7 @@ module ActiveModel
@association.embed = :objects @association.embed = :objects
assert_equal({ 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) }, @user_serializer.as_json)
end end
@ -84,7 +84,7 @@ module ActiveModel
end end
assert_equal({ 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) }, @user_serializer.as_json)
end end
@ -93,7 +93,7 @@ module ActiveModel
@association.embedded_key = 'root' @association.embedded_key = 'root'
assert_equal({ 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) }, @user_serializer.serializable_hash)
end end
@ -112,7 +112,7 @@ module ActiveModel
assert_equal({ assert_equal({
'user' => { name: 'Name 1', email: 'mail@server.com', 'profile_id' => @user.profile.object_id }, '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) }, @user_serializer.as_json)
end end
@ -129,7 +129,7 @@ module ActiveModel
assert_equal({ assert_equal({
'user' => { name: 'Name 1', email: 'mail@server.com', 'profile_id' => @user.profile.object_id }, 'user' => { name: 'Name 1', email: 'mail@server.com', 'profile_id' => @user.profile.object_id },
profile: { name: 'fake' } 'profiles' => [{ name: 'fake' }]
}, @user_serializer.as_json) }, @user_serializer.as_json)
end end
end end