mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 15:23:06 +00:00
Pull in upstream
This commit is contained in:
@@ -29,6 +29,7 @@ module ActionController
|
||||
|
||||
included do
|
||||
class_attribute :_serialization_scope
|
||||
self._serialization_scope = :current_user
|
||||
end
|
||||
|
||||
def serialization_scope
|
||||
@@ -37,7 +38,7 @@ module ActionController
|
||||
|
||||
def _render_option_json(json, options)
|
||||
if json.respond_to?(:active_model_serializer) && (serializer = json.active_model_serializer)
|
||||
json = serializer.new(json, serialization_scope)
|
||||
json = serializer.new(json, serialization_scope, options)
|
||||
end
|
||||
super
|
||||
end
|
||||
|
||||
@@ -11,14 +11,15 @@ module ActiveModel
|
||||
class ArraySerializer
|
||||
attr_reader :object, :scope
|
||||
|
||||
def initialize(object, scope)
|
||||
@object, @scope = object, scope
|
||||
def initialize(object, scope, options={})
|
||||
@object, @scope, @options = object, scope, options
|
||||
@hash = options[:hash]
|
||||
end
|
||||
|
||||
def serializable_array
|
||||
@object.map do |item|
|
||||
if item.respond_to?(:active_model_serializer) && (serializer = item.active_model_serializer)
|
||||
serializer.new(item, scope)
|
||||
serializer.new(item, scope, :hash => @hash)
|
||||
else
|
||||
item
|
||||
end
|
||||
@@ -26,7 +27,14 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def as_json(*args)
|
||||
serializable_array.as_json(*args)
|
||||
@hash = {}
|
||||
array = serializable_array.as_json(*args)
|
||||
|
||||
if root = @options[:root]
|
||||
@hash.merge!(root => array)
|
||||
else
|
||||
array
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -79,9 +87,9 @@ module ActiveModel
|
||||
end
|
||||
|
||||
class HasMany < Config #:nodoc:
|
||||
def serialize(collection, scope)
|
||||
def serialize(collection, scope, options)
|
||||
collection.map do |item|
|
||||
serializer.new(item, scope).serializable_hash
|
||||
serializer.new(item, scope, options).serializable_hash
|
||||
end
|
||||
end
|
||||
|
||||
@@ -96,18 +104,18 @@ module ActiveModel
|
||||
end
|
||||
|
||||
class HasOne < Config #:nodoc:
|
||||
def serialize(object, scope)
|
||||
def serialize(object, scope, options)
|
||||
return unless object
|
||||
|
||||
if polymorphic?
|
||||
polymorphic_type = object.class.to_s.demodulize
|
||||
serializer_class = "#{object.class.to_s}Serializer".constantize
|
||||
|
||||
serializer_class.new(object, scope).serializable_hash.merge({
|
||||
serializer_class.new(object, scope, options).serializable_hash.merge({
|
||||
"#{name}_type".to_sym => polymorphic_type
|
||||
})
|
||||
else
|
||||
serializer.new(object, scope).serializable_hash
|
||||
serializer.new(object, scope, options).serializable_hash
|
||||
end
|
||||
end
|
||||
|
||||
@@ -222,8 +230,8 @@ module ActiveModel
|
||||
columns = klass.columns_hash
|
||||
|
||||
attrs = _attributes.inject({}) do |hash, (name,key)|
|
||||
column = columns[name]
|
||||
hash.merge key => column[:type]
|
||||
column = columns[name.to_s]
|
||||
hash.merge key => column.type
|
||||
end
|
||||
|
||||
associations = _associations.inject({}) do |hash, association|
|
||||
@@ -236,7 +244,7 @@ module ActiveModel
|
||||
|
||||
# The model class associated with this serializer.
|
||||
def model_class
|
||||
name.sub(/Serializer$/, '')
|
||||
name.sub(/Serializer$/, '').constantize
|
||||
end
|
||||
|
||||
# Define how associations should be embedded.
|
||||
@@ -269,19 +277,20 @@ module ActiveModel
|
||||
|
||||
attr_reader :object, :scope
|
||||
|
||||
def initialize(object, scope)
|
||||
@object, @scope = object, scope
|
||||
def initialize(object, scope, options={})
|
||||
@object, @scope, @options = object, scope, options
|
||||
@hash = options[:hash]
|
||||
end
|
||||
|
||||
# Returns a json representation of the serializable
|
||||
# object including the root.
|
||||
def as_json(*)
|
||||
if _root
|
||||
hash = { _root => serializable_hash }
|
||||
hash.merge!(associations) if _root_embed
|
||||
if root = @options[:root] || _root
|
||||
@hash = hash = {}
|
||||
hash.merge!(root => serializable_hash)
|
||||
hash
|
||||
else
|
||||
serializable_hash
|
||||
@hash = serializable_hash
|
||||
end
|
||||
end
|
||||
|
||||
@@ -289,6 +298,7 @@ module ActiveModel
|
||||
# object without the root.
|
||||
def serializable_hash
|
||||
if _embed == :ids
|
||||
merge_associations(@hash, associations) if _root_embed
|
||||
attributes.merge(association_ids)
|
||||
elsif _embed == :objects
|
||||
attributes.merge(associations)
|
||||
@@ -297,6 +307,16 @@ module ActiveModel
|
||||
end
|
||||
end
|
||||
|
||||
def merge_associations(hash, associations)
|
||||
associations.each do |key, value|
|
||||
if hash[key]
|
||||
hash[key] |= value
|
||||
elsif value
|
||||
hash[key] = value
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Returns a hash representation of the serializable
|
||||
# object associations.
|
||||
def associations
|
||||
@@ -304,10 +324,7 @@ module ActiveModel
|
||||
|
||||
_associations.each do |association|
|
||||
associated_object = send(association.name)
|
||||
hash[association.key] = association.serialize(associated_object, scope)
|
||||
|
||||
if association.polymorphic? && associated_object
|
||||
end
|
||||
hash[association.key] = association.serialize(associated_object, scope, :hash => @hash)
|
||||
end
|
||||
|
||||
hash
|
||||
|
||||
Reference in New Issue
Block a user