class Shoulda::Matchers::ActiveRecord::AssociationMatchers::JoinTableMatcher

@private

Attributes

association_matcher[R]
failure_message[R]
missing_option[R]
reflector[R]

Public Class Methods

new(association_matcher, reflector) click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/join_table_matcher.rb, line 16
def initialize(association_matcher, reflector)
  @association_matcher = association_matcher
  @reflector = reflector
end

Public Instance Methods

join_table_exists?() click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/join_table_matcher.rb, line 40
def join_table_exists?
  if connection.tables.include?(join_table_name)
    true
  else
    @failure_message = missing_table_message
    false
  end
end
join_table_has_correct_columns?() click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/join_table_matcher.rb, line 49
def join_table_has_correct_columns?
  if missing_columns.empty?
    true
  else
    @failure_message = missing_columns_message
    false
  end
end
join_table_option_correct?() click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/join_table_matcher.rb, line 27
def join_table_option_correct?
  if options.key?(:join_table_name)
    if option_verifier.correct_for_string?(:join_table, options[:join_table_name])
      true
    else
      @failure_message = "#{name} should use '#{options[:join_table_name]}' for :join_table option"
      false
    end
  else
    true
  end
end
matches?(subject) click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/join_table_matcher.rb, line 21
def matches?(subject)
  join_table_option_correct? &&
    join_table_exists? &&
    join_table_has_correct_columns?
end

Private Instance Methods

actual_join_table_columns() click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/join_table_matcher.rb, line 76
def actual_join_table_columns
  connection.columns(join_table_name).map(&:name)
end
column_label() click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/join_table_matcher.rb, line 89
def column_label
  if missing_columns.count > 1
    'columns'
  else
    'column'
  end
end
expected_join_table_columns() click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/join_table_matcher.rb, line 72
def expected_join_table_columns
  [foreign_key, association_foreign_key]
end
missing_columns() click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/join_table_matcher.rb, line 66
def missing_columns
  @missing_columns ||= expected_join_table_columns.select do |key|
    !actual_join_table_columns.include?(key.to_s)
  end
end
missing_columns_message() click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/join_table_matcher.rb, line 84
def missing_columns_message
  missing = missing_columns.join(', ')
  "join table #{join_table_name} missing #{column_label}: #{missing}"
end
missing_table_message() click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/join_table_matcher.rb, line 80
def missing_table_message
  "join table #{join_table_name} doesn't exist"
end