mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
parent
60fc55021c
commit
5fa4002039
@ -14,17 +14,18 @@ module ActiveModel
|
|||||||
|
|
||||||
def initialize(object, options={})
|
def initialize(object, options={})
|
||||||
@object = object
|
@object = object
|
||||||
|
@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]
|
||||||
@options = options.merge(root: nil)
|
@resource_name = options[:resource_name]
|
||||||
end
|
end
|
||||||
attr_accessor :object, :root, :meta_key, :meta, :options
|
attr_accessor :object, :scope, :root, :meta_key, :meta
|
||||||
|
|
||||||
def json_key
|
def json_key
|
||||||
if root.nil?
|
if root.nil?
|
||||||
@options[:resource_name]
|
@resource_name
|
||||||
else
|
else
|
||||||
root
|
root
|
||||||
end
|
end
|
||||||
@ -32,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, @options)
|
serializer_class.new(item, scope: scope)
|
||||||
end
|
end
|
||||||
|
|
||||||
def serializable_object
|
def serializable_object
|
||||||
|
|||||||
@ -102,14 +102,14 @@ end
|
|||||||
end
|
end
|
||||||
|
|
||||||
def initialize(object, options={})
|
def initialize(object, options={})
|
||||||
@object = object
|
@object = object
|
||||||
@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]
|
||||||
@options = options.reject{|k,v| [:scope, :root, :meta_key, :meta].include?(k) }
|
@wrap_in_array = options[:_wrap_in_array]
|
||||||
end
|
end
|
||||||
attr_accessor :object, :scope, :meta_key, :meta, :root, :options
|
attr_accessor :object, :scope, :root, :meta_key, :meta
|
||||||
|
|
||||||
def json_key
|
def json_key
|
||||||
if root == true || root.nil?
|
if root == true || root.nil?
|
||||||
@ -186,7 +186,7 @@ end
|
|||||||
return nil if object.nil?
|
return nil if object.nil?
|
||||||
hash = attributes
|
hash = attributes
|
||||||
hash.merge! associations
|
hash.merge! associations
|
||||||
@options[:_wrap_in_array] ? [hash] : hash
|
@wrap_in_array ? [hash] : hash
|
||||||
end
|
end
|
||||||
alias_method :serializable_hash, :serializable_object
|
alias_method :serializable_hash, :serializable_object
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,17 +0,0 @@
|
|||||||
require 'test_helper'
|
|
||||||
|
|
||||||
module ActiveModel
|
|
||||||
class Serializer
|
|
||||||
class OptionsTest < ActiveModel::TestCase
|
|
||||||
def setup
|
|
||||||
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_meta
|
|
||||||
profile_serializer = ProfileSerializer.new(@profile, root: 'profile', random_option: "This is an option")
|
|
||||||
|
|
||||||
assert_equal("This is an option", profile_serializer.options[:random_option])
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
Loading…
Reference in New Issue
Block a user