Merge pull request #790 from engineyard/array-serializer-context

pass context to ArraySerializer
This commit is contained in:
Alexandre de Oliveira 2015-01-29 15:05:57 -02:00
commit f71ac766ef
2 changed files with 21 additions and 4 deletions

View File

@ -22,10 +22,11 @@ module ActiveModel
@resource_name = options[:resource_name]
@only = options[:only] ? Array(options[:only]) : nil
@except = options[:except] ? Array(options[:except]) : nil
@context = options[:context]
@namespace = options[:namespace]
@key_format = options[:key_format] || options[:each_serializer].try(:key_format)
end
attr_accessor :object, :scope, :root, :meta_key, :meta, :key_format
attr_accessor :object, :scope, :root, :meta_key, :meta, :key_format, :context
def json_key
key = root.nil? ? @resource_name : root
@ -35,7 +36,7 @@ module ActiveModel
def serializer_for(item)
serializer_class = @each_serializer || Serializer.serializer_for(item, namespace: @namespace) || DefaultSerializer
serializer_class.new(item, scope: scope, key_format: key_format, only: @only, except: @except, polymorphic: @polymorphic, namespace: @namespace)
serializer_class.new(item, scope: scope, key_format: key_format, context: @context, only: @only, except: @except, polymorphic: @polymorphic, namespace: @namespace)
end
def serializable_object(options={})
@ -66,7 +67,7 @@ module ActiveModel
private
def instrumentation_keys
[:object, :scope, :root, :meta_key, :meta, :each_serializer, :resource_name, :key_format]
[:object, :scope, :root, :meta_key, :meta, :each_serializer, :resource_name, :key_format, :context]
end
end
end

View File

@ -0,0 +1,16 @@
require 'test_helper'
module ActiveModel
class ArraySerializer
class OptionsTest < Minitest::Test
def test_custom_options_are_accessible_from_serializer
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, only: [:name], context: {foo: :bar})
assert_equal({foo: :bar}, serializer.context)
end
end
end
end