class ActiveRecord::Associations::AssociationReflection

Attributes

association_foreign_key[R]
attribute[R]
macro[R]
owner_class[R]
source[R]

Public Class Methods

new(owner_class, macro, name, options = {}) click to toggle source
# File lib/reactive_record/active_record/associations.rb, line 31
def initialize(owner_class, macro, name, options = {})
  owner_class.reflect_on_all_associations << self
  @owner_class = owner_class
  @macro =       macro
  @options =     options
  @klass_name =  options[:class_name] || (collection? && name.camelize.gsub(/s$/, '')) || name.camelize
  if @klass_name < ActiveRecord::Base
    @klass = @klass_name
    @klass_name = @klass_name.name
  end rescue nil
  @association_foreign_key = options[:foreign_key] || (macro == :belongs_to && "#{name}_id") || "#{@owner_class.name.underscore}_id"
  @source = options[:source] || @klass_name.underscore if options[:through]
  @attribute = name
end

Public Instance Methods

collection?() click to toggle source
# File lib/reactive_record/active_record/associations.rb, line 102
def collection?
  [:has_many].include? @macro
end
find_inverse() click to toggle source
# File lib/reactive_record/active_record/associations.rb, line 86
def find_inverse
  klass.reflect_on_all_associations.each do |association|
    next if association.association_foreign_key != @association_foreign_key
    next if association.klass != @owner_class
    next if association.attribute == attribute
    return association if klass == association.owner_class
  end
  raise "Association #{@owner_class}.#{attribute} "\
        "(foreign_key: #{@association_foreign_key}) "\
        "has no inverse in #{@klass_name}"
end
inverse() click to toggle source
# File lib/reactive_record/active_record/associations.rb, line 77
def inverse
  @inverse ||=
    through_association ? through_association.inverse : find_inverse
end
inverse_of() click to toggle source
# File lib/reactive_record/active_record/associations.rb, line 82
def inverse_of
  @inverse_of ||= inverse.attribute
end
klass() click to toggle source
# File lib/reactive_record/active_record/associations.rb, line 98
def klass
  @klass ||= Object.const_get(@klass_name)
end
source_associations() click to toggle source
# File lib/reactive_record/active_record/associations.rb, line 66
def source_associations
  # find all associations that use this association as the source
  # that is final all associations that are using this association as the source in a
  # through relationship
  @source_associations ||= owner_class.reflect_on_all_associations.collect do |sibling|
    sibling.klass.reflect_on_all_associations.select do |assoc|
      assoc.source == attribute
    end
  end.flatten
end
through_association() click to toggle source
# File lib/reactive_record/active_record/associations.rb, line 46
def through_association
  return unless @options[:through]
  @through_association ||= @owner_class.reflect_on_all_associations.detect do |association|
    association.attribute == @options[:through]
  end
  raise "Through association #{@options[:through]} for "\
        "#{@owner_class}.#{attribute} not found." unless @through_association
  @through_association
end
Also aliased as: through_association?
through_association?()
Alias for: through_association
through_associations() click to toggle source
# File lib/reactive_record/active_record/associations.rb, line 58
def through_associations
  # find all associations that use the inverse association as the through association
  # that is find all associations that are using this association in a through relationship
  @through_associations ||= klass.reflect_on_all_associations.select do |assoc|
    assoc.through_association && assoc.inverse == self
  end
end