active_model_serializers/lib/active_model/serializer/adapter/json.rb
Benjamin Fleischer 2d24dded14 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
2015-06-24 11:46:29 -05:00

52 lines
1.5 KiB
Ruby

require 'active_model/serializer/adapter/json/fragment_cache'
module ActiveModel
class Serializer
class Adapter
class Json < Adapter
def serializable_hash(options = nil)
options ||= {}
if serializer.respond_to?(:each)
@result = serializer.map{|s| FlattenJson.new(s).serializable_hash(options) }
else
@hash = {}
@core = cache_check(serializer) do
serializer.attributes(options)
end
serializer.each_association do |name, association, opts|
if association.respond_to?(:each)
array_serializer = association
@hash[name] = array_serializer.map do |item|
cache_check(item) do
item.attributes(opts)
end
end
else
if association && association.object
@hash[name] = cache_check(association) do
association.attributes(options)
end
elsif opts[:virtual_value]
@hash[name] = opts[:virtual_value]
else
@hash[name] = nil
end
end
end
@result = @core.merge @hash
end
{ root => @result }
end
def fragment_cache(cached_hash, non_cached_hash)
Json::FragmentCache.new().fragment_cache(cached_hash, non_cached_hash)
end
end
end
end
end