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 44
def join_table_exists?
  if connection.data_sources.
      include?(join_table_name.to_s)
    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 54
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].inspect} 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 81
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 94
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 77
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 71
def missing_columns
  @_missing_columns ||= expected_join_table_columns.reject 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 89
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 85
def missing_table_message
  "join table #{join_table_name} doesn't exist"
end