mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Merge branch 'association_in_root_array_serializer'
This commit is contained in:
commit
78cceb4113
@ -30,12 +30,25 @@ module ActiveModel
|
||||
end
|
||||
end
|
||||
|
||||
def serializer_for(item)
|
||||
serializer_class = @each_serializer || Serializer.serializer_for(item) || DefaultSerializer
|
||||
serializer_class.new(item, @options)
|
||||
end
|
||||
|
||||
def serializable_array
|
||||
@object.map do |item|
|
||||
serializer = @each_serializer || Serializer.serializer_for(item) || DefaultSerializer
|
||||
serializer.new(item, @options).serializable_object
|
||||
serializer_for(item).serializable_object
|
||||
end
|
||||
end
|
||||
alias_method :serializable_object, :serializable_array
|
||||
|
||||
def embedded_in_root_associations
|
||||
@object.each_with_object({}) do |item, hash|
|
||||
serializer = serializer_for(item)
|
||||
if serializer.respond_to?(:embedded_in_root_associations)
|
||||
hash.merge!(serializer.embedded_in_root_associations)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -11,11 +11,15 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def serializable_data
|
||||
if respond_to?(:meta) && meta
|
||||
{ meta_key => meta }
|
||||
else
|
||||
{}
|
||||
embedded_in_root_associations.tap do |hash|
|
||||
if respond_to?(:meta) && meta
|
||||
hash[meta_key] = meta
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def embedded_in_root_associations
|
||||
{}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -141,10 +141,6 @@ end
|
||||
keys
|
||||
end
|
||||
|
||||
def serializable_data
|
||||
embedded_in_root_associations.merge!(super)
|
||||
end
|
||||
|
||||
def embedded_in_root_associations
|
||||
associations = self.class._associations
|
||||
included_associations = filter(associations.keys)
|
||||
|
||||
@ -193,5 +193,42 @@ module ActionController
|
||||
assert_equal '{"my":[{"name":"Name 1","description":"Description 1"}]}', @response.body
|
||||
end
|
||||
end
|
||||
|
||||
class ArrayEmbedingSerializerTest < ActionController::TestCase
|
||||
def setup
|
||||
super
|
||||
@association = UserSerializer._associations[:profile]
|
||||
@old_association = @association.dup
|
||||
end
|
||||
|
||||
def teardown
|
||||
super
|
||||
UserSerializer._associations[:profile] = @old_association
|
||||
end
|
||||
|
||||
class MyController < ActionController::Base
|
||||
def initialize(*)
|
||||
super
|
||||
@user = User.new({ name: 'Name 1', email: 'mail@server.com', gender: 'M' })
|
||||
end
|
||||
attr_reader :user
|
||||
|
||||
def render_array_embeding_in_root
|
||||
render json: [@user]
|
||||
end
|
||||
end
|
||||
|
||||
tests MyController
|
||||
|
||||
def test_render_array_embeding_in_root
|
||||
@association.embed = :ids
|
||||
@association.embed_in_root = true
|
||||
|
||||
get :render_array_embeding_in_root
|
||||
assert_equal 'application/json', @response.content_type
|
||||
|
||||
assert_equal("{\"my\":[{\"name\":\"Name 1\",\"email\":\"mail@server.com\",\"profile_id\":#{@controller.user.profile.object_id}}],\"profiles\":[{\"name\":\"N1\",\"description\":\"D1\"}]}", @response.body)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user