class Shoulda::Matchers::ActiveRecord::AssociationMatchers::ModelReflection

@private

Attributes

reflection[R]
subject[R]

Public Class Methods

new(reflection) click to toggle source
Calls superclass method
# File lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb, line 9
def initialize(reflection)
  super(reflection)
  @reflection = reflection
  @subject = reflection.active_record
end

Public Instance Methods

associated_class() click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb, line 15
def associated_class
  if polymorphic?
    subject
  else
    reflection.klass
  end
end
association_foreign_key() click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb, line 58
def association_foreign_key
  if has_and_belongs_to_many_reflection
    join_model = has_and_belongs_to_many_reflection.options[:class]
    join_model.right_reflection.foreign_key
  else
    reflection.association_foreign_key
  end
end
association_relation(related_instance) click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb, line 37
def association_relation(related_instance)
  relation = associated_class.all

  if reflection.scope
    # Source: AR::Associations::AssociationScope#eval_scope
    relation.instance_exec(related_instance, &reflection.scope)
  else
    relation
  end
end
foreign_key() click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb, line 48
def foreign_key
  if has_and_belongs_to_many_reflection
    has_and_belongs_to_many_reflection.foreign_key
  elsif reflection.respond_to?(:foreign_key)
    reflection.foreign_key
  else
    reflection.primary_key_name
  end
end
has_and_belongs_to_many_name() click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb, line 73
def has_and_belongs_to_many_name
  reflection.options[:through]
end
join_table_name() click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb, line 31
def join_table_name
  join_table_name =
    has_and_belongs_to_many_name_table_name || reflection.join_table
  join_table_name.to_s
end
polymorphic?() click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb, line 23
def polymorphic?
  reflection.options[:polymorphic]
end
strict_loading?() click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb, line 77
def strict_loading?
  reflection.options.fetch(:strict_loading, subject.strict_loading_by_default)
end
through?() click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb, line 27
def through?
  reflection.options[:through]
end
validate_inverse_of_through_association!() click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb, line 67
def validate_inverse_of_through_association!
  if through?
    reflection.check_validity!
  end
end

Private Instance Methods

has_and_belongs_to_many_name_table_name() click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb, line 87
def has_and_belongs_to_many_name_table_name
  has_and_belongs_to_many_reflection&.table_name
end
has_and_belongs_to_many_reflection() click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb, line 91
def has_and_belongs_to_many_reflection
  @_has_and_belongs_to_many_reflection ||=
    if has_and_belongs_to_many_name
      @subject.reflect_on_association(has_and_belongs_to_many_name)
    end
end