mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
s/side_load/embed_in_root
This commit is contained in:
parent
2b70503896
commit
860acad9af
@ -19,14 +19,14 @@ module ActiveModel
|
||||
|
||||
def embed(type, options={})
|
||||
CONFIG.embed = type
|
||||
CONFIG.side_load = true if options[:side_load] || options[:include]
|
||||
CONFIG.embed_in_root = true if options[:embed_in_root] || options[:include]
|
||||
ActiveSupport::Deprecation.warn <<-WARN
|
||||
** Notice: embed is deprecated. **
|
||||
The use of .embed method on a Serializer will be soon removed, as this should have a global scope and not a class scope.
|
||||
Please use the global .setup method instead:
|
||||
ActiveModel::Serializer.setup do |config|
|
||||
config.embed = :#{type}
|
||||
config.side_load = #{CONFIG.side_load || false}
|
||||
config.embed_in_root = #{CONFIG.embed_in_root || false}
|
||||
end
|
||||
WARN
|
||||
end
|
||||
@ -147,7 +147,7 @@ end
|
||||
included_associations = filter(associations.keys)
|
||||
associations.each_with_object({}) do |(name, association), hash|
|
||||
if included_associations.include? name
|
||||
if association.side_load?
|
||||
if association.embed_in_root?
|
||||
hash[association.embedded_key] = serialize association
|
||||
end
|
||||
end
|
||||
|
||||
@ -7,14 +7,14 @@ module ActiveModel
|
||||
def initialize(name, options={})
|
||||
if options.has_key?(:include)
|
||||
ActiveSupport::Deprecation.warn <<-WARN
|
||||
** Notice: include was renamed to side_load. **
|
||||
** Notice: include was renamed to embed_in_root. **
|
||||
WARN
|
||||
end
|
||||
|
||||
@name = name.to_s
|
||||
@options = options
|
||||
self.embed = options.fetch(:embed) { CONFIG.embed }
|
||||
@side_load = options.fetch(:side_load) { options.fetch(:include) { CONFIG.side_load } }
|
||||
@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
|
||||
@ -23,10 +23,10 @@ module ActiveModel
|
||||
end
|
||||
|
||||
attr_reader :name, :embed_ids, :embed_objects, :serializer_class
|
||||
attr_accessor :side_load, :embed_key, :key, :embedded_key, :options
|
||||
attr_accessor :embed_in_root, :embed_key, :key, :embedded_key, :options
|
||||
alias embed_ids? embed_ids
|
||||
alias embed_objects? embed_objects
|
||||
alias side_load? side_load
|
||||
alias embed_in_root? embed_in_root
|
||||
|
||||
def serializer_class=(serializer)
|
||||
@serializer_class = serializer.is_a?(String) ? serializer.constantize : serializer
|
||||
|
||||
@ -40,7 +40,7 @@ module ActiveModel
|
||||
def test_serialization_embedding_ids_including_in_root
|
||||
post_serializer = ARPostSerializer.new(@post)
|
||||
|
||||
embed(ARPostSerializer, embed: :ids, side_load: true) do
|
||||
embed(ARPostSerializer, embed: :ids, embed_in_root: true) do
|
||||
assert_equal({
|
||||
'ar_post' => {
|
||||
title: 'New post', body: 'A body!!!',
|
||||
@ -63,7 +63,7 @@ module ActiveModel
|
||||
|
||||
ARPostSerializer._associations.each_value do |association|
|
||||
association.embed = options[:embed]
|
||||
association.side_load = options[:side_load]
|
||||
association.embed_in_root = options[:embed_in_root]
|
||||
end
|
||||
|
||||
yield
|
||||
|
||||
@ -66,7 +66,7 @@ module ActiveModel
|
||||
class ApplyConfigTest < ActiveModel::TestCase
|
||||
def test_apply_config_to_associations
|
||||
CONFIG.embed = :ids
|
||||
CONFIG.side_load = true
|
||||
CONFIG.embed_in_root = true
|
||||
|
||||
association = PostSerializer._associations[:comments]
|
||||
old_association = association.dup
|
||||
@ -75,7 +75,7 @@ module ActiveModel
|
||||
|
||||
assert association.embed_ids?
|
||||
assert !association.embed_objects?
|
||||
assert association.side_load
|
||||
assert association.embed_in_root
|
||||
ensure
|
||||
PostSerializer._associations[:comments] = old_association
|
||||
CONFIG.clear
|
||||
|
||||
@ -25,7 +25,7 @@ module ActiveModel
|
||||
@association = PostSerializer._associations[:comments]
|
||||
@old_association = @association.dup
|
||||
@association.embed = :ids
|
||||
@association.side_load = true
|
||||
@association.embed_in_root = true
|
||||
@post = Post.new({ title: 'Title 1', body: 'Body 1', date: '1/1/2000' })
|
||||
@post_serializer = PostSerializer.new(@post)
|
||||
@post_serializer.instance_eval do
|
||||
|
||||
@ -86,7 +86,7 @@ module ActiveModel
|
||||
|
||||
def test_associations_embedding_ids_including_objects_serialization_using_serializable_hash
|
||||
@association.embed = :ids
|
||||
@association.side_load = true
|
||||
@association.embed_in_root = true
|
||||
|
||||
assert_equal({
|
||||
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
|
||||
@association.embed = :ids
|
||||
@association.side_load = true
|
||||
@association.embed_in_root = true
|
||||
|
||||
assert_equal({
|
||||
'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
|
||||
@association.embed = nil
|
||||
@association.side_load = true
|
||||
@association.embed_in_root = true
|
||||
|
||||
assert_equal({
|
||||
'post' => { title: 'Title 1', body: 'Body 1' },
|
||||
@ -115,7 +115,7 @@ module ActiveModel
|
||||
|
||||
def test_associations_using_a_given_serializer
|
||||
@association.embed = :ids
|
||||
@association.side_load = true
|
||||
@association.embed_in_root = true
|
||||
@association.serializer_class = Class.new(ActiveModel::Serializer) do
|
||||
def content
|
||||
'fake'
|
||||
|
||||
@ -99,7 +99,7 @@ module ActiveModel
|
||||
|
||||
def test_associations_embedding_ids_including_objects_serialization_using_serializable_hash
|
||||
@association.embed = :ids
|
||||
@association.side_load = true
|
||||
@association.embed_in_root = true
|
||||
|
||||
assert_equal({
|
||||
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
|
||||
@association.embed = :ids
|
||||
@association.side_load = true
|
||||
@association.embed_in_root = true
|
||||
|
||||
assert_equal({
|
||||
'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
|
||||
@association.embed = :ids
|
||||
@association.side_load = true
|
||||
@association.embed_in_root = true
|
||||
@association.serializer_class = Class.new(ActiveModel::Serializer) do
|
||||
def name
|
||||
'fake'
|
||||
|
||||
Loading…
Reference in New Issue
Block a user