s/side_load/embed_in_root

This commit is contained in:
Arthur Neves
2013-10-24 14:11:58 -04:00
parent 2b70503896
commit 860acad9af
7 changed files with 19 additions and 19 deletions

View File

@@ -19,14 +19,14 @@ module ActiveModel
def embed(type, options={}) def embed(type, options={})
CONFIG.embed = type 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 ActiveSupport::Deprecation.warn <<-WARN
** Notice: embed is deprecated. ** ** 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. 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: Please use the global .setup method instead:
ActiveModel::Serializer.setup do |config| ActiveModel::Serializer.setup do |config|
config.embed = :#{type} config.embed = :#{type}
config.side_load = #{CONFIG.side_load || false} config.embed_in_root = #{CONFIG.embed_in_root || false}
end end
WARN WARN
end end
@@ -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.side_load? if association.embed_in_root?
hash[association.embedded_key] = serialize association hash[association.embedded_key] = serialize association
end end
end end

View File

@@ -7,14 +7,14 @@ module ActiveModel
def initialize(name, options={}) def initialize(name, options={})
if options.has_key?(:include) if options.has_key?(:include)
ActiveSupport::Deprecation.warn <<-WARN ActiveSupport::Deprecation.warn <<-WARN
** Notice: include was renamed to side_load. ** ** Notice: include was renamed to embed_in_root. **
WARN WARN
end end
@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 }
@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 @embed_key = options[:embed_key] || :id
@key = options[:key] @key = options[:key]
@embedded_key = options[:root] || name @embedded_key = options[:root] || name
@@ -23,10 +23,10 @@ module ActiveModel
end end
attr_reader :name, :embed_ids, :embed_objects, :serializer_class 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_ids? embed_ids
alias embed_objects? embed_objects alias embed_objects? embed_objects
alias side_load? side_load alias embed_in_root? embed_in_root
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

View File

@@ -40,7 +40,7 @@ module ActiveModel
def test_serialization_embedding_ids_including_in_root def test_serialization_embedding_ids_including_in_root
post_serializer = ARPostSerializer.new(@post) post_serializer = ARPostSerializer.new(@post)
embed(ARPostSerializer, embed: :ids, side_load: true) do embed(ARPostSerializer, embed: :ids, embed_in_root: true) do
assert_equal({ assert_equal({
'ar_post' => { 'ar_post' => {
title: 'New post', body: 'A body!!!', title: 'New post', body: 'A body!!!',
@@ -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.side_load = options[:side_load] association.embed_in_root = options[:embed_in_root]
end end
yield yield

View File

@@ -66,7 +66,7 @@ module ActiveModel
class ApplyConfigTest < ActiveModel::TestCase class ApplyConfigTest < ActiveModel::TestCase
def test_apply_config_to_associations def test_apply_config_to_associations
CONFIG.embed = :ids CONFIG.embed = :ids
CONFIG.side_load = true CONFIG.embed_in_root = true
association = PostSerializer._associations[:comments] association = PostSerializer._associations[:comments]
old_association = association.dup old_association = association.dup
@@ -75,7 +75,7 @@ module ActiveModel
assert association.embed_ids? assert association.embed_ids?
assert !association.embed_objects? assert !association.embed_objects?
assert association.side_load assert association.embed_in_root
ensure ensure
PostSerializer._associations[:comments] = old_association PostSerializer._associations[:comments] = old_association
CONFIG.clear CONFIG.clear

View File

@@ -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.side_load = true @association.embed_in_root = 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

View File

@@ -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.side_load = true @association.embed_in_root = 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.side_load = true @association.embed_in_root = 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.side_load = true @association.embed_in_root = 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.side_load = true @association.embed_in_root = true
@association.serializer_class = Class.new(ActiveModel::Serializer) do @association.serializer_class = Class.new(ActiveModel::Serializer) do
def content def content
'fake' 'fake'

View File

@@ -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.side_load = true @association.embed_in_root = 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.side_load = true @association.embed_in_root = 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.side_load = true @association.embed_in_root = true
@association.serializer_class = Class.new(ActiveModel::Serializer) do @association.serializer_class = Class.new(ActiveModel::Serializer) do
def name def name
'fake' 'fake'