class ActiveAny::Reflection::AssociationReflection

Constants

INVALID_AUTOMATIC_INVERSE_OPTIONS
JoinKeys
VALID_AUTOMATIC_INVERSE_MACROS

Attributes

name[R]
options[R]
record_class[R]
scope[R]

Public Class Methods

new(name, scope, options, record_class) click to toggle source
# File lib/active_any/reflection/association_reflection.rb, line 8
def initialize(name, scope, options, record_class)
  @name = name
  @record_class = record_class
  @options = options
  @scope = scope
end

Public Instance Methods

association_class() click to toggle source
# File lib/active_any/reflection/association_reflection.rb, line 15
def association_class
  raise NotImplementedError
end
belongs_to?() click to toggle source
# File lib/active_any/reflection/association_reflection.rb, line 39
def belongs_to?
  false
end
check_eager_loadable!()
Alias for: check_preloadable!
check_preloadable!() click to toggle source
# File lib/active_any/reflection/association_reflection.rb, line 61
      def check_preloadable!
        return unless scope

        if scope.arity.positive?
          raise ArgumentError, <<-MSG.squish
            The association scope '#{name}' is instance dependent (the scope
            block takes an argument). Preloading instance dependent scopes is
            not supported.
          MSG
        end
      end
Also aliased as: check_eager_loadable!
class_name() click to toggle source
# File lib/active_any/reflection/association_reflection.rb, line 23
def class_name
  @class_name ||= (options[:class_name] || derive_class_name).to_s
end
collection?() click to toggle source
# File lib/active_any/reflection/association_reflection.rb, line 35
def collection?
  false
end
compute_class(name) click to toggle source
# File lib/active_any/reflection/association_reflection.rb, line 31
def compute_class(name)
  name.constantize
end
foreign_key() click to toggle source
# File lib/active_any/reflection/association_reflection.rb, line 53
def foreign_key
  @foreign_key ||= options[:foreign_key] || derive_foreign_key.freeze
end
has_one?() click to toggle source
# File lib/active_any/reflection/association_reflection.rb, line 43
def has_one?
  false
end
inverse_of() click to toggle source
# File lib/active_any/reflection/association_reflection.rb, line 74
def inverse_of
  return unless inverse_name

  @inverse_of ||= klass._reflect_on_association inverse_name
end
join_keys() click to toggle source
# File lib/active_any/reflection/association_reflection.rb, line 49
def join_keys
  JoinKeys.new(join_pk, join_fk)
end
klass() click to toggle source
# File lib/active_any/reflection/association_reflection.rb, line 27
def klass
  @klass ||= compute_class(class_name)
end
marco() click to toggle source
# File lib/active_any/reflection/association_reflection.rb, line 19
def marco
  raise NotImplementedError
end
record_class_primary_key() click to toggle source
# File lib/active_any/reflection/association_reflection.rb, line 80
def record_class_primary_key
  @primary_key ||= options[:primary_key] || primary_key(record_class)
end
scope_for(klass) click to toggle source
# File lib/active_any/reflection/association_reflection.rb, line 57
def scope_for(klass)
  scope ? klass.unscoped.instance_exec(nil, &scope) : klass.unscoped
end

Private Instance Methods

automatic_inverse_of() click to toggle source
# File lib/active_any/reflection/association_reflection.rb, line 96
def automatic_inverse_of
  if can_find_inverse_of_automatically?(self)
    inverse_name = ActiveSupport::Inflector.underscore(options[:as] || record_class.name.demodulize).to_sym

    begin
      reflection = klass._reflect_on_association(inverse_name)
    rescue NameError
      # Give up: we couldn't compute the klass type so we won't be able
      # to find any associations either.
      reflection = false
    end

    return inverse_name if valid_inverse_reflection?(reflection)
  end

  false
end
can_find_inverse_of_automatically?(reflection) click to toggle source
# File lib/active_any/reflection/association_reflection.rb, line 117
def can_find_inverse_of_automatically?(reflection)
  reflection.options[:inverse_of] != false &&
    VALID_AUTOMATIC_INVERSE_MACROS.include?(reflection.macro) &&
    INVALID_AUTOMATIC_INVERSE_OPTIONS.none? { |opt| reflection.options[opt] } &&
    !reflection.scope
end
derive_class_name() click to toggle source
# File lib/active_any/reflection/association_reflection.rb, line 138
def derive_class_name
  class_name = name.to_s
  class_name = class_name.singularize if collection?
  class_name.camelize
end
derive_foreign_key() click to toggle source
# File lib/active_any/reflection/association_reflection.rb, line 144
def derive_foreign_key
  if belongs_to?
    "#{name}_id"
  elsif options[:as]
    "#{options[:as]}_id"
  else
    record_class.name.foreign_key
  end
end
inverse_name() click to toggle source
# File lib/active_any/reflection/association_reflection.rb, line 90
def inverse_name
  options.fetch(:inverse_of) do
    @automatic_inverse_of ||= automatic_inverse_of
  end
end
join_fk() click to toggle source
# File lib/active_any/reflection/association_reflection.rb, line 134
def join_fk
  raise NotImplementedError
end
join_pk() click to toggle source
# File lib/active_any/reflection/association_reflection.rb, line 130
def join_pk
  raise NotImplementedError
end
primary_key(record_class) click to toggle source
# File lib/active_any/reflection/association_reflection.rb, line 86
def primary_key(record_class)
  record_class.primary_key || (raise UnknownPrimaryKey.new, klass)
end
valid_inverse_reflection?(reflection) click to toggle source
# File lib/active_any/reflection/association_reflection.rb, line 124
def valid_inverse_reflection?(reflection)
  reflection &&
    klass.name == reflection.record_class.name &&
    can_find_inverse_of_automatically?(reflection)
end