class ActiveModel::Serializer

def self.adapter

Other tags:
    See: ActiveModelSerializers::Adapter.lookup -
def self.adapter
  ActiveModelSerializers::Adapter.lookup(config.adapter)
end

def self.get_serializer_for(klass)

Other tags:
    Api: - private
def self.get_serializer_for(klass)
  return nil unless config.serializer_lookup_enabled
  serializers_cache.fetch_or_store(klass) do
    # NOTE(beauby): When we drop 1.9.3 support we can lazify the map for perfs.
    serializer_class = serializer_lookup_chain_for(klass).map(&:safe_constantize).find { |x| x && x < ActiveModel::Serializer }
    if serializer_class
      serializer_class
    elsif klass.superclass
      get_serializer_for(klass.superclass)
    end
  end
end

def self.include_directive_from_options(options)

Other tags:
    Api: - private
def self.include_directive_from_options(options)
  if options[:include_directive]
    options[:include_directive]
  elsif options[:include]
    JSONAPI::IncludeDirective.new(options[:include], allow_wildcard: true)
  else
    ActiveModelSerializers.default_include_directive
  end
end

def self.serialization_adapter_instance

Other tags:
    Api: - private
def self.serialization_adapter_instance
  @serialization_adapter_instance ||= ActiveModelSerializers::Adapter::Attributes
end

def self.serializer_for(resource, options = {})

Returns:
  • (ActiveModel::Serializer) -

Parameters:
  • resource (ActiveRecord::Base, ActiveModelSerializers::Model) --
def self.serializer_for(resource, options = {})
  if resource.respond_to?(:serializer_class)
    resource.serializer_class
  elsif resource.respond_to?(:to_ary)
    config.collection_serializer
  else
    options.fetch(:serializer) { get_serializer_for(resource.class) }
  end
end

def self.serializer_lookup_chain_for(klass)

Other tags:
    Api: - private
def self.serializer_lookup_chain_for(klass)
  chain = []
  resource_class_name = klass.name.demodulize
  resource_namespace = klass.name.deconstantize
  serializer_class_name = "#{resource_class_name}Serializer"
  chain.push("#{name}::#{serializer_class_name}") if self != ActiveModel::Serializer
  chain.push("#{resource_namespace}::#{serializer_class_name}")
  chain
end

def self.serializers_cache

when looked up by Serializer.get_serializer_for.
Used to cache serializer name => serializer class
def self.serializers_cache
  @serializers_cache ||= ThreadSafe::Cache.new
end

def as_json(adapter_opts = nil)

Parameters:
  • options (nil, Hash) -- The same valid options passed to `as_json`

Other tags:
    See: #serializable_hash -
def as_json(adapter_opts = nil)
  serializable_hash(adapter_opts)
end

def initialize(object, options = {})

defines the method so that it calls the +scope+.
If the instance does not have a method named `scope_name`, it
`scope_name` is set as :current_user by default in the controller.
def initialize(object, options = {})
  self.object = object
  self.instance_options = options
  self.root = instance_options[:root]
  self.scope = instance_options[:scope]
  scope_name = instance_options[:scope_name]
  if scope_name && !respond_to?(scope_name)
    define_singleton_method scope_name, lambda { scope }
  end
end

def json_key

Used by adapter as resource root.
def json_key
  root || _type || object.class.model_name.to_s.underscore
end

def read_attribute_for_serialization(attr)

def read_attribute_for_serialization(attr)
  if respond_to?(attr)
    send(attr)
  else
    object.read_attribute_for_serialization(attr)
  end
end

def relationship_value_for(association, adapter_options, adapter_instance)

Other tags:
    Api: - private
def relationship_value_for(association, adapter_options, adapter_instance)
  return association.options[:virtual_value] if association.options[:virtual_value]
  association_serializer = association.serializer
  association_object = association_serializer && association_serializer.object
  return unless association_object
  relationship_value = association_serializer.serializable_hash(adapter_options, {}, adapter_instance)
  if association.options[:polymorphic] && relationship_value
    polymorphic_type = association_object.class.name.underscore
    relationship_value = { type: polymorphic_type, polymorphic_type.to_sym => relationship_value }
  end
  relationship_value
end

def resource_relationships(adapter_options, options, adapter_instance)

Other tags:
    Api: - private
def resource_relationships(adapter_options, options, adapter_instance)
  relationships = {}
  include_directive = options.fetch(:include_directive)
  associations(include_directive).each do |association|
    adapter_opts = adapter_options.merge(include_directive: include_directive[association.key])
    relationships[association.key] ||= relationship_value_for(association, adapter_opts, adapter_instance)
  end
  relationships
end

def serializable_hash(adapter_options = nil, options = {}, adapter_instance = self.class.serialization_adapter_instance)

Parameters:
  • options (nil, Hash) -- The same valid options passed to `serializable_hash`

Returns:
  • (Hash) - containing the attributes and first level
def serializable_hash(adapter_options = nil, options = {}, adapter_instance = self.class.serialization_adapter_instance)
  adapter_options ||= {}
  options[:include_directive] ||= ActiveModel::Serializer.include_directive_from_options(adapter_options)
  cached_attributes = adapter_options[:cached_attributes] ||= {}
  resource = fetch_attributes(options[:fields], cached_attributes, adapter_instance)
  relationships = resource_relationships(adapter_options, options, adapter_instance)
  resource.merge(relationships)
end

def success?

def success?
  true
end