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.
83 lines
2.4 KiB
Ruby
83 lines
2.4 KiB
Ruby
require 'set'
|
|
module ActiveModel
|
|
class SerializableResource
|
|
ADAPTER_OPTION_KEYS = Set.new([:include, :fields, :adapter])
|
|
|
|
def initialize(resource, options = {})
|
|
@resource = resource
|
|
@adapter_opts, @serializer_opts =
|
|
options.partition { |k, _| ADAPTER_OPTION_KEYS.include? k }.map { |h| Hash[h] }
|
|
end
|
|
|
|
delegate :serializable_hash, :as_json, :to_json, to: :adapter
|
|
|
|
# Primary interface to building a serializer (with adapter)
|
|
# If no block is given,
|
|
# returns the serializable_resource, ready for #as_json/#to_json/#serializable_hash.
|
|
# Otherwise, yields the serializable_resource and
|
|
# returns the contents of the block
|
|
def self.serialize(resource, options = {})
|
|
serializable_resource = SerializableResource.new(resource, options)
|
|
if block_given?
|
|
yield serializable_resource
|
|
else
|
|
serializable_resource
|
|
end
|
|
end
|
|
|
|
def serialization_scope=(scope)
|
|
serializer_opts[:scope] = scope
|
|
end
|
|
|
|
def serialization_scope
|
|
serializer_opts[:scope]
|
|
end
|
|
|
|
def serialization_scope_name=(scope_name)
|
|
serializer_opts[:scope_name] = scope_name
|
|
end
|
|
|
|
def adapter
|
|
@adapter ||= ActiveModel::Serializer::Adapter.create(serializer_instance, adapter_opts)
|
|
end
|
|
alias_method :adapter_instance, :adapter
|
|
|
|
def serializer_instance
|
|
@serializer_instance ||= serializer.new(resource, serializer_opts)
|
|
end
|
|
|
|
# Get serializer either explicitly :serializer or implicitly from resource
|
|
# Remove :serializer key from serializer_opts
|
|
# Replace :serializer key with :each_serializer if present
|
|
def serializer
|
|
@serializer ||=
|
|
begin
|
|
@serializer = serializer_opts.delete(:serializer)
|
|
@serializer ||= ActiveModel::Serializer.serializer_for(resource)
|
|
|
|
if serializer_opts.key?(:each_serializer)
|
|
serializer_opts[:serializer] = serializer_opts.delete(:each_serializer)
|
|
end
|
|
@serializer
|
|
end
|
|
end
|
|
alias_method :serializer_class, :serializer
|
|
|
|
# True when no explicit adapter given, or explicit appear is truthy (non-nil)
|
|
# False when explicit adapter is falsy (nil or false)
|
|
def use_adapter?
|
|
!(adapter_opts.key?(:adapter) && !adapter_opts[:adapter])
|
|
end
|
|
|
|
def serializer?
|
|
use_adapter? && !!(serializer)
|
|
end
|
|
|
|
private
|
|
|
|
ActiveModelSerializers.silence_warnings do
|
|
attr_reader :resource, :adapter_opts, :serializer_opts
|
|
end
|
|
end
|
|
end
|