mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Breaking change: - Adapters now inherit Adapter::Base - 'Adapter' is now a module, no longer a class Why? - using a class as a namespace that you also inherit from is complicated and circular at time i.e. buggy (see https://github.com/rails-api/active_model_serializers/pull/1177) - The class methods on Adapter aren't necessarily related to the instance methods, they're more Adapter functions - named `Base` because it's a Rails-ism - It helps to isolate and highlight what the Adapter interface actually is
57 lines
1.7 KiB
Ruby
57 lines
1.7 KiB
Ruby
module ActiveModel
|
|
class Serializer
|
|
module Adapter
|
|
class Attributes < Base
|
|
def serializable_hash(options = nil)
|
|
options ||= {}
|
|
if serializer.respond_to?(:each)
|
|
result = serializer.map { |s| Attributes.new(s).serializable_hash(options) }
|
|
else
|
|
hash = {}
|
|
|
|
core = cache_check(serializer) do
|
|
serializer.attributes(options)
|
|
end
|
|
|
|
serializer.associations.each do |association|
|
|
serializer = association.serializer
|
|
association_options = association.options
|
|
|
|
if serializer.respond_to?(:each)
|
|
array_serializer = serializer
|
|
hash[association.key] = array_serializer.map do |item|
|
|
cache_check(item) do
|
|
item.attributes(association_options)
|
|
end
|
|
end
|
|
else
|
|
hash[association.key] =
|
|
if serializer && serializer.object
|
|
cache_check(serializer) do
|
|
serializer.attributes(options)
|
|
end
|
|
elsif association_options[:virtual_value]
|
|
association_options[:virtual_value]
|
|
end
|
|
end
|
|
end
|
|
result = core.merge hash
|
|
end
|
|
result
|
|
end
|
|
|
|
def fragment_cache(cached_hash, non_cached_hash)
|
|
Json::FragmentCache.new.fragment_cache(cached_hash, non_cached_hash)
|
|
end
|
|
|
|
private
|
|
|
|
# no-op: Attributes adapter does not include meta data, because it does not support root.
|
|
def include_meta(json)
|
|
json
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|