mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
embed_in_root is now side_load
This commit is contained in:
parent
d26b7df158
commit
48db253765
@ -147,7 +147,7 @@ end
|
|||||||
included_associations = filter(associations.keys)
|
included_associations = filter(associations.keys)
|
||||||
associations.each_with_object({}) do |(name, association), hash|
|
associations.each_with_object({}) do |(name, association), hash|
|
||||||
if included_associations.include? name
|
if included_associations.include? name
|
||||||
if association.embed_in_root?
|
if association.side_load?
|
||||||
hash[association.embedded_key] = serialize association
|
hash[association.embedded_key] = serialize association
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -5,22 +5,22 @@ module ActiveModel
|
|||||||
class Serializer
|
class Serializer
|
||||||
class Association
|
class Association
|
||||||
def initialize(name, options={})
|
def initialize(name, options={})
|
||||||
@name = name.to_s
|
@name = name.to_s
|
||||||
@options = options
|
@options = options
|
||||||
self.embed = options.fetch(:embed) { CONFIG.embed }
|
self.embed = options.fetch(:embed) { CONFIG.embed }
|
||||||
@embed_in_root = options.fetch(:include) { CONFIG.include }
|
@side_load = options.fetch(:include) { CONFIG.include }
|
||||||
@embed_key = options[:embed_key] || :id
|
@embed_key = options[:embed_key] || :id
|
||||||
@key = options[:key]
|
@key = options[:key]
|
||||||
@embedded_key = options[:root] || name
|
@embedded_key = options[:root] || name
|
||||||
|
|
||||||
self.serializer_class = @options[:serializer]
|
self.serializer_class = @options[:serializer]
|
||||||
end
|
end
|
||||||
|
|
||||||
attr_reader :name, :embed_ids, :embed_objects, :serializer_class
|
attr_reader :name, :embed_ids, :embed_objects, :serializer_class
|
||||||
attr_accessor :embed_in_root, :embed_key, :key, :embedded_key, :options
|
attr_accessor :side_load, :embed_key, :key, :embedded_key, :options
|
||||||
alias embed_ids? embed_ids
|
alias embed_ids? embed_ids
|
||||||
alias embed_objects? embed_objects
|
alias embed_objects? embed_objects
|
||||||
alias embed_in_root? embed_in_root
|
alias side_load? side_load
|
||||||
|
|
||||||
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
|
||||||
|
|||||||
@ -63,7 +63,7 @@ module ActiveModel
|
|||||||
|
|
||||||
ARPostSerializer._associations.each_value do |association|
|
ARPostSerializer._associations.each_value do |association|
|
||||||
association.embed = options[:embed]
|
association.embed = options[:embed]
|
||||||
association.embed_in_root = options[:include]
|
association.side_load = options[:include]
|
||||||
end
|
end
|
||||||
|
|
||||||
yield
|
yield
|
||||||
|
|||||||
@ -75,7 +75,7 @@ module ActiveModel
|
|||||||
|
|
||||||
assert association.embed_ids?
|
assert association.embed_ids?
|
||||||
assert !association.embed_objects?
|
assert !association.embed_objects?
|
||||||
assert association.embed_in_root
|
assert association.side_load
|
||||||
ensure
|
ensure
|
||||||
PostSerializer._associations[:comments] = old_association
|
PostSerializer._associations[:comments] = old_association
|
||||||
CONFIG.clear
|
CONFIG.clear
|
||||||
|
|||||||
@ -25,7 +25,7 @@ module ActiveModel
|
|||||||
@association = PostSerializer._associations[:comments]
|
@association = PostSerializer._associations[:comments]
|
||||||
@old_association = @association.dup
|
@old_association = @association.dup
|
||||||
@association.embed = :ids
|
@association.embed = :ids
|
||||||
@association.embed_in_root = true
|
@association.side_load = true
|
||||||
@post = Post.new({ title: 'Title 1', body: 'Body 1', date: '1/1/2000' })
|
@post = Post.new({ title: 'Title 1', body: 'Body 1', date: '1/1/2000' })
|
||||||
@post_serializer = PostSerializer.new(@post)
|
@post_serializer = PostSerializer.new(@post)
|
||||||
@post_serializer.instance_eval do
|
@post_serializer.instance_eval do
|
||||||
|
|||||||
@ -86,7 +86,7 @@ module ActiveModel
|
|||||||
|
|
||||||
def test_associations_embedding_ids_including_objects_serialization_using_serializable_hash
|
def test_associations_embedding_ids_including_objects_serialization_using_serializable_hash
|
||||||
@association.embed = :ids
|
@association.embed = :ids
|
||||||
@association.embed_in_root = true
|
@association.side_load = true
|
||||||
|
|
||||||
assert_equal({
|
assert_equal({
|
||||||
title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id }
|
title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id }
|
||||||
@ -95,7 +95,7 @@ module ActiveModel
|
|||||||
|
|
||||||
def test_associations_embedding_ids_including_objects_serialization_using_as_json
|
def test_associations_embedding_ids_including_objects_serialization_using_as_json
|
||||||
@association.embed = :ids
|
@association.embed = :ids
|
||||||
@association.embed_in_root = true
|
@association.side_load = true
|
||||||
|
|
||||||
assert_equal({
|
assert_equal({
|
||||||
'post' => { title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } },
|
'post' => { title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } },
|
||||||
@ -105,7 +105,7 @@ module ActiveModel
|
|||||||
|
|
||||||
def test_associations_embedding_nothing_including_objects_serialization_using_as_json
|
def test_associations_embedding_nothing_including_objects_serialization_using_as_json
|
||||||
@association.embed = nil
|
@association.embed = nil
|
||||||
@association.embed_in_root = true
|
@association.side_load = true
|
||||||
|
|
||||||
assert_equal({
|
assert_equal({
|
||||||
'post' => { title: 'Title 1', body: 'Body 1' },
|
'post' => { title: 'Title 1', body: 'Body 1' },
|
||||||
@ -115,7 +115,7 @@ module ActiveModel
|
|||||||
|
|
||||||
def test_associations_using_a_given_serializer
|
def test_associations_using_a_given_serializer
|
||||||
@association.embed = :ids
|
@association.embed = :ids
|
||||||
@association.embed_in_root = true
|
@association.side_load = true
|
||||||
@association.serializer_class = Class.new(ActiveModel::Serializer) do
|
@association.serializer_class = Class.new(ActiveModel::Serializer) do
|
||||||
def content
|
def content
|
||||||
'fake'
|
'fake'
|
||||||
|
|||||||
@ -99,7 +99,7 @@ module ActiveModel
|
|||||||
|
|
||||||
def test_associations_embedding_ids_including_objects_serialization_using_serializable_hash
|
def test_associations_embedding_ids_including_objects_serialization_using_serializable_hash
|
||||||
@association.embed = :ids
|
@association.embed = :ids
|
||||||
@association.embed_in_root = true
|
@association.side_load = true
|
||||||
|
|
||||||
assert_equal({
|
assert_equal({
|
||||||
name: 'Name 1', email: 'mail@server.com', 'profile_id' => @user.profile.object_id
|
name: 'Name 1', email: 'mail@server.com', 'profile_id' => @user.profile.object_id
|
||||||
@ -108,7 +108,7 @@ module ActiveModel
|
|||||||
|
|
||||||
def test_associations_embedding_ids_including_objects_serialization_using_as_json
|
def test_associations_embedding_ids_including_objects_serialization_using_as_json
|
||||||
@association.embed = :ids
|
@association.embed = :ids
|
||||||
@association.embed_in_root = true
|
@association.side_load = true
|
||||||
|
|
||||||
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 },
|
||||||
@ -118,7 +118,7 @@ module ActiveModel
|
|||||||
|
|
||||||
def test_associations_using_a_given_serializer
|
def test_associations_using_a_given_serializer
|
||||||
@association.embed = :ids
|
@association.embed = :ids
|
||||||
@association.embed_in_root = true
|
@association.side_load = true
|
||||||
@association.serializer_class = Class.new(ActiveModel::Serializer) do
|
@association.serializer_class = Class.new(ActiveModel::Serializer) do
|
||||||
def name
|
def name
|
||||||
'fake'
|
'fake'
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user