mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 23:06:50 +00:00
Allow to pass context around serializers
This commit is contained in:
parent
9bb32331f4
commit
8bd2542ed2
@ -14,6 +14,7 @@ module ActiveModel
|
|||||||
|
|
||||||
def initialize(object, options={})
|
def initialize(object, options={})
|
||||||
@object = object
|
@object = object
|
||||||
|
@context = options[:context]
|
||||||
@scope = options[:scope]
|
@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
|
||||||
@ -21,7 +22,7 @@ module ActiveModel
|
|||||||
@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, :scope, :root, :meta_key, :meta
|
attr_accessor :object, :context, :scope, :root, :meta_key, :meta
|
||||||
|
|
||||||
def json_key
|
def json_key
|
||||||
if root.nil?
|
if root.nil?
|
||||||
@ -33,7 +34,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, scope: scope)
|
serializer_class.new(item, context: context, scope: scope)
|
||||||
end
|
end
|
||||||
|
|
||||||
def serializable_object
|
def serializable_object
|
||||||
|
|||||||
@ -103,13 +103,14 @@ end
|
|||||||
|
|
||||||
def initialize(object, options={})
|
def initialize(object, options={})
|
||||||
@object = object
|
@object = object
|
||||||
|
@context = options[:context]
|
||||||
@scope = options[:scope]
|
@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, :scope, :root, :meta_key, :meta
|
attr_accessor :object, :context, :scope, :root, :meta_key, :meta
|
||||||
|
|
||||||
def json_key
|
def json_key
|
||||||
if root == true || root.nil?
|
if root == true || root.nil?
|
||||||
@ -166,7 +167,7 @@ end
|
|||||||
|
|
||||||
def build_serializer(association)
|
def build_serializer(association)
|
||||||
object = send(association.name)
|
object = send(association.name)
|
||||||
association.build_serializer(object, scope: scope)
|
association.build_serializer(object, context: context, scope: scope)
|
||||||
end
|
end
|
||||||
|
|
||||||
def serialize(association)
|
def serialize(association)
|
||||||
|
|||||||
19
test/unit/active_model/array_serializer/context_test.rb
Normal file
19
test/unit/active_model/array_serializer/context_test.rb
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
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
|
||||||
|
end
|
||||||
|
end
|
||||||
19
test/unit/active_model/serializer/context_test.rb
Normal file
19
test/unit/active_model/serializer/context_test.rb
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
module ActiveModel
|
||||||
|
class Serializer
|
||||||
|
class ContextTest < ActiveModel::TestCase
|
||||||
|
def test_context_using_a_hash
|
||||||
|
serializer = UserSerializer.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 = UserSerializer.new(nil, context: Struct.new(:a, :b).new(1, 2))
|
||||||
|
assert_equal(1, serializer.context.a)
|
||||||
|
assert_equal(2, serializer.context.b)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Reference in New Issue
Block a user