mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
I was seeing transient failures where adapters may not be registered. e.g. https://travis-ci.org/rails-api/active_model_serializers/builds/77735382 Since we're using the Adapter, JsonApi, and Json classes as namespaces, some of the conventions we use for modules don't apply. Basically, we don't want to define the class anywhere besides itself. Otherwise, the inherited hooks may not run, and some adapters may not be registered. For example: If we have a class Api `class Api; end` And Api is also used as a namespace for `Api::Product` And the classes are defined in different files. In one file: ```ruby class Api autoload :Product def self.inherited(subclass) puts p [:inherited, subclass.name] puts end end ``` And in another: ```ruby class Api class Product < Api def sell_sell_sell! # TODO: sell end end end ``` If we load the Api class file first, the inherited hook will be defined on the class so that when we load the Api::Product class, we'll see the output: ```plain [ :inherited, Api::Product] ``` However, if we load the Api::Product class first, since it defines the `Api` class and then inherited from it, the Api file was never loaded, the hook never defined, and thus never run. By defining the class as `class Api::Product < Api` We ensure the the Api class MUST be defined, and thus, the hook will be defined and run and so sunshine and unicorns. Appendix: The below would work, but triggers a circular reference warning. It's also not recommended to mix require with autoload. ```ruby require 'api' class Api class Product < Api def sell_sell_sell! # TODO: sell end end end ``` This failure scenario was introduced by removing the circular reference warnings in https://github.com/rails-api/active_model_serializers/pull/1067 Style note: To make diffs on the adapters smalleer and easier to read, I've maintained the same identention that was in the original file. I've decided to prefer ease of reading the diff over style, esp. since we may later return to the preferred class declaration style. with '#' will be ignored, and an empty message aborts the commit.
164 lines
5.2 KiB
Ruby
164 lines
5.2 KiB
Ruby
module ActiveModel
|
|
class Serializer
|
|
class Adapter
|
|
UnknownAdapterError = Class.new(ArgumentError)
|
|
ADAPTER_MAP = {}
|
|
private_constant :ADAPTER_MAP if defined?(private_constant)
|
|
extend ActiveSupport::Autoload
|
|
autoload :FragmentCache
|
|
autoload :Json
|
|
autoload :JsonApi
|
|
autoload :Null
|
|
autoload :FlattenJson
|
|
|
|
def self.create(resource, options = {})
|
|
override = options.delete(:adapter)
|
|
klass = override ? adapter_class(override) : ActiveModel::Serializer.adapter
|
|
klass.new(resource, options)
|
|
end
|
|
|
|
# @see ActiveModel::Serializer::Adapter.get
|
|
def self.adapter_class(adapter)
|
|
ActiveModel::Serializer::Adapter.get(adapter)
|
|
end
|
|
|
|
# Only the Adapter class has these methods.
|
|
# None of the sublasses have them.
|
|
class << ActiveModel::Serializer::Adapter
|
|
# @return Hash<adapter_name, adapter_class>
|
|
def adapter_map
|
|
ADAPTER_MAP
|
|
end
|
|
|
|
# @return [Array<Symbol>] list of adapter names
|
|
def adapters
|
|
adapter_map.keys.sort
|
|
end
|
|
|
|
# Adds an adapter 'klass' with 'name' to the 'adapter_map'
|
|
# Names are stringified and underscored
|
|
# @param [Symbol, String] name of the registered adapter
|
|
# @param [Class] klass - adapter class itself
|
|
# @example
|
|
# AMS::Adapter.register(:my_adapter, MyAdapter)
|
|
def register(name, klass)
|
|
adapter_map.update(name.to_s.underscore => klass)
|
|
self
|
|
end
|
|
|
|
# @param adapter [String, Symbol, Class] name to fetch adapter by
|
|
# @return [ActiveModel::Serializer::Adapter] subclass of Adapter
|
|
# @raise [UnknownAdapterError]
|
|
def get(adapter)
|
|
# 1. return if is a class
|
|
return adapter if adapter.is_a?(Class)
|
|
adapter_name = adapter.to_s.underscore
|
|
# 2. return if registered
|
|
adapter_map.fetch(adapter_name) {
|
|
# 3. try to find adapter class from environment
|
|
adapter_class = find_by_name(adapter_name)
|
|
register(adapter_name, adapter_class)
|
|
adapter_class
|
|
}
|
|
rescue ArgumentError => e
|
|
failure_message =
|
|
"Unknown adapter: #{adapter.inspect}. Valid adapters are: #{adapters}"
|
|
raise UnknownAdapterError, failure_message, e.backtrace
|
|
rescue NameError => e
|
|
failure_message =
|
|
"NameError: #{e.message}. Unknown adapter: #{adapter.inspect}. Valid adapters are: #{adapters}"
|
|
raise UnknownAdapterError, failure_message, e.backtrace
|
|
end
|
|
|
|
# @api private
|
|
def find_by_name(adapter_name)
|
|
adapter_name = adapter_name.to_s.classify.tr('API', 'Api')
|
|
"ActiveModel::Serializer::Adapter::#{adapter_name}".safe_constantize or # rubocop:disable Style/AndOr
|
|
fail UnknownAdapterError
|
|
end
|
|
private :find_by_name
|
|
end
|
|
|
|
# Automatically register adapters when subclassing
|
|
def self.inherited(subclass)
|
|
ActiveModel::Serializer::Adapter.register(subclass.to_s.demodulize, subclass)
|
|
end
|
|
|
|
attr_reader :serializer
|
|
|
|
def initialize(serializer, options = {})
|
|
@serializer = serializer
|
|
@options = options
|
|
end
|
|
|
|
def serializable_hash(options = nil)
|
|
raise NotImplementedError, 'This is an abstract method. Should be implemented at the concrete adapter.'
|
|
end
|
|
|
|
def as_json(options = nil)
|
|
hash = serializable_hash(options)
|
|
include_meta(hash)
|
|
hash
|
|
end
|
|
|
|
def fragment_cache(*args)
|
|
raise NotImplementedError, 'This is an abstract method. Should be implemented at the concrete adapter.'
|
|
end
|
|
|
|
private
|
|
|
|
def cache_check(serializer)
|
|
@cached_serializer = serializer
|
|
@klass = @cached_serializer.class
|
|
if is_cached?
|
|
@klass._cache.fetch(cache_key, @klass._cache_options) do
|
|
yield
|
|
end
|
|
elsif is_fragment_cached?
|
|
FragmentCache.new(self, @cached_serializer, @options).fetch
|
|
else
|
|
yield
|
|
end
|
|
end
|
|
|
|
def is_cached?
|
|
@klass._cache && !@klass._cache_only && !@klass._cache_except
|
|
end
|
|
|
|
def is_fragment_cached?
|
|
@klass._cache_only && !@klass._cache_except || !@klass._cache_only && @klass._cache_except
|
|
end
|
|
|
|
def cache_key
|
|
parts = []
|
|
parts << object_cache_key
|
|
parts << @klass._cache_digest unless @klass._cache_options && @klass._cache_options[:skip_digest]
|
|
parts.join('/')
|
|
end
|
|
|
|
def object_cache_key
|
|
object_time_safe = @cached_serializer.object.updated_at
|
|
object_time_safe = object_time_safe.strftime('%Y%m%d%H%M%S%9N') if object_time_safe.respond_to?(:strftime)
|
|
(@klass._cache_key) ? "#{@klass._cache_key}/#{@cached_serializer.object.id}-#{object_time_safe}" : @cached_serializer.object.cache_key
|
|
end
|
|
|
|
def meta
|
|
serializer.meta if serializer.respond_to?(:meta)
|
|
end
|
|
|
|
def meta_key
|
|
serializer.meta_key || 'meta'
|
|
end
|
|
|
|
def root
|
|
serializer.json_key.to_sym if serializer.json_key
|
|
end
|
|
|
|
def include_meta(json)
|
|
json[meta_key] = meta if meta
|
|
json
|
|
end
|
|
end
|
|
end
|
|
end
|