mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Revert "Merge branch 'context'"
This reverts commit1bd8180a94, reversing changes made to9bb32331f4.
This commit is contained in:
parent
1bd8180a94
commit
ed9a5288f9
@ -65,21 +65,11 @@ module ActionController
|
|||||||
options = default_serializer_options.merge(options)
|
options = default_serializer_options.merge(options)
|
||||||
|
|
||||||
if serializer = options.fetch(:serializer, ActiveModel::Serializer.serializer_for(resource))
|
if serializer = options.fetch(:serializer, ActiveModel::Serializer.serializer_for(resource))
|
||||||
options[:context] = build_context_from options[:context]
|
options[:scope] = serialization_scope unless options.has_key?(:scope)
|
||||||
options[:resource_name] = controller_name if resource.respond_to?(:to_ary)
|
options[:resource_name] = controller_name if resource.respond_to?(:to_ary)
|
||||||
|
|
||||||
serializer.new(resource, options)
|
serializer.new(resource, options)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def build_context_from(context_option)
|
|
||||||
default_context.tap do |context|
|
|
||||||
context.merge! context_option if context_option
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def default_context
|
|
||||||
{ scope: serialization_scope }
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -14,14 +14,14 @@ module ActiveModel
|
|||||||
|
|
||||||
def initialize(object, options={})
|
def initialize(object, options={})
|
||||||
@object = object
|
@object = object
|
||||||
@context = options[:context] || {}
|
@scope = options[:scope]
|
||||||
@root = options.fetch(:root, self.class._root)
|
@root = options.fetch(:root, self.class._root)
|
||||||
@meta_key = options[:meta_key] || :meta
|
@meta_key = options[:meta_key] || :meta
|
||||||
@meta = options[@meta_key]
|
@meta = options[@meta_key]
|
||||||
@each_serializer = options[:each_serializer]
|
@each_serializer = options[:each_serializer]
|
||||||
@resource_name = options[:resource_name]
|
@resource_name = options[:resource_name]
|
||||||
end
|
end
|
||||||
attr_accessor :object, :context, :root, :meta_key, :meta
|
attr_accessor :object, :scope, :root, :meta_key, :meta
|
||||||
|
|
||||||
def json_key
|
def json_key
|
||||||
if root.nil?
|
if root.nil?
|
||||||
@ -33,7 +33,7 @@ module ActiveModel
|
|||||||
|
|
||||||
def serializer_for(item)
|
def serializer_for(item)
|
||||||
serializer_class = @each_serializer || Serializer.serializer_for(item) || DefaultSerializer
|
serializer_class = @each_serializer || Serializer.serializer_for(item) || DefaultSerializer
|
||||||
serializer_class.new(item, context: context)
|
serializer_class.new(item, scope: scope)
|
||||||
end
|
end
|
||||||
|
|
||||||
def serializable_object
|
def serializable_object
|
||||||
|
|||||||
@ -103,13 +103,13 @@ end
|
|||||||
|
|
||||||
def initialize(object, options={})
|
def initialize(object, options={})
|
||||||
@object = object
|
@object = object
|
||||||
@context = options[:context] || {}
|
@scope = options[:scope]
|
||||||
@root = options.fetch(:root, self.class._root)
|
@root = options.fetch(:root, self.class._root)
|
||||||
@meta_key = options[:meta_key] || :meta
|
@meta_key = options[:meta_key] || :meta
|
||||||
@meta = options[@meta_key]
|
@meta = options[@meta_key]
|
||||||
@wrap_in_array = options[:_wrap_in_array]
|
@wrap_in_array = options[:_wrap_in_array]
|
||||||
end
|
end
|
||||||
attr_accessor :object, :context, :root, :meta_key, :meta
|
attr_accessor :object, :scope, :root, :meta_key, :meta
|
||||||
|
|
||||||
def json_key
|
def json_key
|
||||||
if root == true || root.nil?
|
if root == true || root.nil?
|
||||||
@ -166,7 +166,7 @@ end
|
|||||||
|
|
||||||
def build_serializer(association)
|
def build_serializer(association)
|
||||||
object = send(association.name)
|
object = send(association.name)
|
||||||
association.build_serializer(object, context: context)
|
association.build_serializer(object, scope: scope)
|
||||||
end
|
end
|
||||||
|
|
||||||
def serialize(association)
|
def serialize(association)
|
||||||
|
|||||||
2
test/fixtures/poro.rb
vendored
2
test/fixtures/poro.rb
vendored
@ -47,7 +47,7 @@ end
|
|||||||
class ProfileSerializer < ActiveModel::Serializer
|
class ProfileSerializer < ActiveModel::Serializer
|
||||||
def description
|
def description
|
||||||
description = object.read_attribute_for_serialization(:description)
|
description = object.read_attribute_for_serialization(:description)
|
||||||
context[:scope] ? "#{description} - #{context[:scope]}" : description
|
scope ? "#{description} - #{scope}" : description
|
||||||
end
|
end
|
||||||
|
|
||||||
attributes :name, :description
|
attributes :name, :description
|
||||||
|
|||||||
@ -43,7 +43,7 @@ module ActionController
|
|||||||
class DefaultOptionsForSerializerScopeTest < ActionController::TestCase
|
class DefaultOptionsForSerializerScopeTest < ActionController::TestCase
|
||||||
class MyController < ActionController::Base
|
class MyController < ActionController::Base
|
||||||
def default_serializer_options
|
def default_serializer_options
|
||||||
{ context: { scope: current_admin } }
|
{ scope: current_admin }
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_using_scope_set_in_default_serializer_options
|
def render_using_scope_set_in_default_serializer_options
|
||||||
@ -73,7 +73,7 @@ module ActionController
|
|||||||
class ExplicitSerializerScopeTest < ActionController::TestCase
|
class ExplicitSerializerScopeTest < ActionController::TestCase
|
||||||
class MyController < ActionController::Base
|
class MyController < ActionController::Base
|
||||||
def render_using_implicit_serializer_and_explicit_scope
|
def render_using_implicit_serializer_and_explicit_scope
|
||||||
render json: Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }), context: { scope: current_admin }
|
render json: Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }), scope: current_admin
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|||||||
@ -1,38 +0,0 @@
|
|||||||
require 'test_helper'
|
|
||||||
|
|
||||||
module ActiveModel
|
|
||||||
class ArraySerializer
|
|
||||||
class ContextTest < ActiveModel::TestCase
|
|
||||||
def test_context_using_a_hash
|
|
||||||
serializer = ArraySerializer.new(nil, context: { a: 1, b: 2 })
|
|
||||||
assert_equal(1, serializer.context[:a])
|
|
||||||
assert_equal(2, serializer.context[:b])
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_context_using_an_object
|
|
||||||
serializer = ArraySerializer.new(nil, context: Struct.new(:a, :b).new(1, 2))
|
|
||||||
assert_equal(1, serializer.context.a)
|
|
||||||
assert_equal(2, serializer.context.b)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class ScopeTest < ActiveModel::TestCase
|
|
||||||
def test_array_serializer_pass_context_to_item_serializers
|
|
||||||
array = [Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
|
|
||||||
Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })]
|
|
||||||
serializer = ArraySerializer.new(array, context: { scope: current_user })
|
|
||||||
|
|
||||||
expected = [{ name: 'Name 1', description: 'Description 1 - user' },
|
|
||||||
{ name: 'Name 2', description: 'Description 2 - user' }]
|
|
||||||
|
|
||||||
assert_equal expected, serializer.serializable_array
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def current_user
|
|
||||||
'user'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
24
test/unit/active_model/array_serializer/scope_test.rb
Normal file
24
test/unit/active_model/array_serializer/scope_test.rb
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
module ActiveModel
|
||||||
|
class ArraySerializer
|
||||||
|
class ScopeTest < ActiveModel::TestCase
|
||||||
|
def test_array_serializer_pass_options_to_items_serializers
|
||||||
|
array = [Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
|
||||||
|
Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })]
|
||||||
|
serializer = ArraySerializer.new(array, scope: current_user)
|
||||||
|
|
||||||
|
expected = [{ name: 'Name 1', description: 'Description 1 - user' },
|
||||||
|
{ name: 'Name 2', description: 'Description 2 - user' }]
|
||||||
|
|
||||||
|
assert_equal expected, serializer.serializable_array
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def current_user
|
||||||
|
'user'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -2,45 +2,48 @@ require 'test_helper'
|
|||||||
|
|
||||||
module ActiveModel
|
module ActiveModel
|
||||||
class Serializer
|
class Serializer
|
||||||
class ContextTest < ActiveModel::TestCase
|
class ScopeTest < ActiveModel::TestCase
|
||||||
def test_context_using_a_hash
|
def setup
|
||||||
serializer = UserSerializer.new(nil, context: { a: 1, b: 2 })
|
@serializer = ProfileSerializer.new(nil, scope: current_user)
|
||||||
assert_equal(1, serializer.context[:a])
|
|
||||||
assert_equal(2, serializer.context[:b])
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_context_using_an_object
|
def test_scope
|
||||||
serializer = UserSerializer.new(nil, context: Struct.new(:a, :b).new(1, 2))
|
assert_equal('user', @serializer.scope)
|
||||||
assert_equal(1, serializer.context.a)
|
end
|
||||||
assert_equal(2, serializer.context.b)
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def current_user
|
||||||
|
'user'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class ContextAssociationTest < ActiveModel::TestCase
|
class NestedScopeTest < ActiveModel::TestCase
|
||||||
def setup
|
def setup
|
||||||
@association = UserSerializer._associations[:profile]
|
@association = UserSerializer._associations[:profile]
|
||||||
@old_association = @association.dup
|
@old_association = @association.dup
|
||||||
@user = User.new({ name: 'Name 1', email: 'mail@server.com', gender: 'M' })
|
@user = User.new({ name: 'Name 1', email: 'mail@server.com', gender: 'M' })
|
||||||
@user_serializer = UserSerializer.new(@user, context: { admin: true })
|
@user_serializer = UserSerializer.new(@user, scope: 'user')
|
||||||
end
|
end
|
||||||
|
|
||||||
def teardown
|
def teardown
|
||||||
UserSerializer._associations[:profile] = @old_association
|
UserSerializer._associations[:profile] = @old_association
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_context_passed_through
|
def test_scope_passed_through
|
||||||
@association.serializer_class = Class.new(ActiveModel::Serializer) do
|
@association.serializer_class = Class.new(ActiveModel::Serializer) do
|
||||||
def name
|
def name
|
||||||
context[:admin] ? 'Admin' : 'User'
|
scope
|
||||||
end
|
end
|
||||||
|
|
||||||
attributes :name
|
attributes :name
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_equal({
|
assert_equal({
|
||||||
name: 'Name 1', email: 'mail@server.com', profile: { name: 'Admin' }
|
name: 'Name 1', email: 'mail@server.com', profile: { name: 'user' }
|
||||||
}, @user_serializer.serializable_hash)
|
}, @user_serializer.serializable_hash)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
Loading…
Reference in New Issue
Block a user