class JsonApiReflection

Wraps what we know about a reflection. Includes the ActiveRecord::Reflection, the ActiveModel::Serializer::Reflection, and the JsonApiReflectionReceiver results (i.e. the contents of a has_many block from the serializer definition).

Attributes

ar_class[R]
ar_reflection[R]
include_data[R]
klass[R]
name[R]
original_name[R]
reflection_sql[R]
serializer_reflection[R]

Public Class Methods

new(name, ar_class, serializer_class) click to toggle source

`ar_class` should be the source ActiveRecord class, so that `ar_class.name` is one or more things of `klass`.

# File lib/active_model_serializers/adapter/json_api_pg.rb, line 265
def initialize(name, ar_class, serializer_class)
  @name = name
  @ar_class = ar_class
  @original_name = @ar_class.instance_method(name).original_name

  @serializer_reflection = serializer_class._reflections[name.to_sym]

  @ar_reflection = ar_class.reflections[name.to_s]
  @reflection_sql = nil
  if @ar_reflection.nil?
    # See if it's an alias:
    @ar_reflection = ar_class.reflections[@original_name.to_s]
  end
  if @ar_reflection.nil?
    m = "#{name}__sql".to_sym
    if ar_class.respond_to? m
      rel = ar_class.send(m)
      # Must be an ActiveRecord::Relation (or ActiveModel::Base) so we can determine klass
      @reflection_sql = rel
      @klass = ActiveRecord::Relation === rel ? rel.klass : rel
    else
      raise "Can't find an association named #{name} for class #{ar_class.name}"
    end
  else
    @klass = @ar_reflection.klass
  end
  @include_data = true
  @links = {}

  if serializer_reflection.try(:block).present?
    x = JsonApiReflectionReceiver.new(serializer_class)
    x.instance_eval &serializer_reflection.block
    @include_data = x.result_include_data
    @links = x.result_links
  end
end

Public Instance Methods

belongs_to?() click to toggle source
# File lib/active_model_serializers/adapter/json_api_pg.rb, line 302
def belongs_to?
  ar_reflection.is_a? ActiveRecord::Reflection::BelongsToReflection
  # TODO: fall back to AMS reflection
end
has_many?() click to toggle source
# File lib/active_model_serializers/adapter/json_api_pg.rb, line 307
def has_many?
  ar_reflection.try(:is_a?, ActiveRecord::Reflection::HasManyReflection) ||
    serializer_reflection.is_a?(ActiveModel::Serializer::HasManyReflection)
end
has_one?() click to toggle source
# File lib/active_model_serializers/adapter/json_api_pg.rb, line 312
def has_one?
  ar_reflection.is_a? ActiveRecord::Reflection::HasOneReflection
  # TODO: fall back to AMS reflection
end