mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Merge branch 'has_one_arrays_pluralized'
This commit is contained in:
commit
5a92e00b51
@ -64,15 +64,12 @@ module ActionController
|
|||||||
def build_json_serializer(resource, options)
|
def build_json_serializer(resource, options)
|
||||||
options = default_serializer_options.merge(options || {})
|
options = default_serializer_options.merge(options || {})
|
||||||
|
|
||||||
serializer = options.delete(:serializer)
|
if serializer = options.fetch(:serializer, ActiveModel::Serializer.serializer_for(resource))
|
||||||
serializer = ActiveModel::Serializer.serializer_for(resource) if serializer.nil?
|
options[:scope] = serialization_scope unless options.has_key?(:scope)
|
||||||
|
options[:resource_name] = self.controller_name if resource.respond_to?(:to_ary)
|
||||||
|
|
||||||
return unless serializer
|
serializer.new(resource, options)
|
||||||
|
end
|
||||||
options[:scope] = serialization_scope unless options.has_key?(:scope)
|
|
||||||
options[:resource_name] = self.controller_name if resource.respond_to?(:to_ary)
|
|
||||||
|
|
||||||
serializer.new(resource, options)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -159,13 +159,7 @@ end
|
|||||||
|
|
||||||
def serialize(association)
|
def serialize(association)
|
||||||
associated_data = send(association.name)
|
associated_data = send(association.name)
|
||||||
if associated_data.respond_to?(:to_ary) &&
|
association.build_serializer(associated_data).serializable_object
|
||||||
!(association.serializer_class &&
|
|
||||||
association.serializer_class <= ArraySerializer)
|
|
||||||
associated_data.map { |elem| association.build_serializer(elem).serializable_hash }
|
|
||||||
else
|
|
||||||
association.build_serializer(associated_data).serializable_object
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def serialize_ids(association)
|
def serialize_ids(association)
|
||||||
|
|||||||
@ -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
|
||||||
@ -30,6 +29,13 @@ module ActiveModel
|
|||||||
|
|
||||||
def serializer_class=(serializer)
|
def serializer_class=(serializer)
|
||||||
@serializer_class = serializer.is_a?(String) ? serializer.constantize : serializer
|
@serializer_class = serializer.is_a?(String) ? serializer.constantize : serializer
|
||||||
|
|
||||||
|
if @serializer_class && !(@serializer_class <= ArraySerializer)
|
||||||
|
@options.merge!(each_serializer: @serializer_class)
|
||||||
|
@serializer_class = ArraySerializer
|
||||||
|
else
|
||||||
|
@serializer_class ||= ArraySerializer
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def embed=(embed)
|
def embed=(embed)
|
||||||
@ -38,21 +44,22 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def build_serializer(object)
|
def build_serializer(object)
|
||||||
@serializer_class ||= Serializer.serializer_for(object) || DefaultSerializer
|
@serializer_class.new(Array(object), @options)
|
||||||
@serializer_class.new(object, @options)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
class HasOne < Association
|
class HasOne < Association
|
||||||
def initialize(*args)
|
def initialize(name, *args)
|
||||||
super
|
super
|
||||||
@key ||= "#{name}_id"
|
@embedded_key = "#{@options[:root] || name}".pluralize
|
||||||
|
@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
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user