mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 23:06:50 +00:00
serializable_hash and as_json should take options = nil
per ActiveModel::Serialization#serializable_hash
96bb004fc6/activemodel/lib/active_model/serialization.rb
def serializable_hash(options = nil)
options ||= {}
Otherwise, passing in nil to `as_json` or `serializable_hash`
makes things blow up when passing nil into attributes
This commit is contained in:
parent
f67fd976ec
commit
2d24dded14
@ -16,11 +16,11 @@ module ActiveModel
|
|||||||
@options = options
|
@options = options
|
||||||
end
|
end
|
||||||
|
|
||||||
def serializable_hash(options = {})
|
def serializable_hash(options = nil)
|
||||||
raise NotImplementedError, 'This is an abstract method. Should be implemented at the concrete adapter.'
|
raise NotImplementedError, 'This is an abstract method. Should be implemented at the concrete adapter.'
|
||||||
end
|
end
|
||||||
|
|
||||||
def as_json(options = {})
|
def as_json(options = nil)
|
||||||
hash = serializable_hash(options)
|
hash = serializable_hash(options)
|
||||||
include_meta(hash) unless self.class == FlattenJson
|
include_meta(hash) unless self.class == FlattenJson
|
||||||
hash
|
hash
|
||||||
|
|||||||
@ -4,9 +4,10 @@ module ActiveModel
|
|||||||
class Serializer
|
class Serializer
|
||||||
class Adapter
|
class Adapter
|
||||||
class Json < Adapter
|
class Json < Adapter
|
||||||
def serializable_hash(options = {})
|
def serializable_hash(options = nil)
|
||||||
|
options ||= {}
|
||||||
if serializer.respond_to?(:each)
|
if serializer.respond_to?(:each)
|
||||||
@result = serializer.map{|s| FlattenJson.new(s).serializable_hash }
|
@result = serializer.map{|s| FlattenJson.new(s).serializable_hash(options) }
|
||||||
else
|
else
|
||||||
@hash = {}
|
@hash = {}
|
||||||
|
|
||||||
|
|||||||
@ -15,10 +15,11 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def serializable_hash(options = {})
|
def serializable_hash(options = nil)
|
||||||
|
options = {}
|
||||||
if serializer.respond_to?(:each)
|
if serializer.respond_to?(:each)
|
||||||
serializer.each do |s|
|
serializer.each do |s|
|
||||||
result = self.class.new(s, @options.merge(fieldset: @fieldset)).serializable_hash
|
result = self.class.new(s, @options.merge(fieldset: @fieldset)).serializable_hash(options)
|
||||||
@hash[:data] << result[:data]
|
@hash[:data] << result[:data]
|
||||||
|
|
||||||
if result[:included]
|
if result[:included]
|
||||||
@ -27,7 +28,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
@hash[:data] = attributes_for_serializer(serializer, @options)
|
@hash[:data] = attributes_for_serializer(serializer, options)
|
||||||
add_resource_relationships(@hash[:data], serializer)
|
add_resource_relationships(@hash[:data], serializer)
|
||||||
end
|
end
|
||||||
@hash
|
@hash
|
||||||
|
|||||||
@ -2,7 +2,7 @@ module ActiveModel
|
|||||||
class Serializer
|
class Serializer
|
||||||
class Adapter
|
class Adapter
|
||||||
class Null < Adapter
|
class Null < Adapter
|
||||||
def serializable_hash(options = {})
|
def serializable_hash(options = nil)
|
||||||
{}
|
{}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user