class Shoulda::Matchers::ActiveRecord::HaveDbIndexMatcher

@private

Public Class Methods

new(columns) click to toggle source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 61
def initialize(columns)
  @columns = normalize_columns_to_array(columns)
  @options = {}
end

Public Instance Methods

description() click to toggle source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 86
def description
  if @options.key?(:unique)
    "have a #{index_type} index on columns #{@columns.join(' and ')}"
  else
    "have an index on columns #{@columns.join(' and ')}"
  end
end
failure_message() click to toggle source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 76
def failure_message
  "Expected #{expectation} (#{@missing})"
end
Also aliased as: failure_message_for_should
failure_message_for_should()
Alias for: failure_message
failure_message_for_should_not()
failure_message_when_negated() click to toggle source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 81
def failure_message_when_negated
  "Did not expect #{expectation}"
end
matches?(subject) click to toggle source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 71
def matches?(subject)
  @subject = subject
  index_exists? && correct_unique?
end
unique(unique) click to toggle source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 66
def unique(unique)
  @options[:unique] = unique
  self
end

Protected Instance Methods

correct_unique?() click to toggle source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 100
def correct_unique?
  return true unless @options.key?(:unique)

  is_unique = matched_index.unique

  is_unique = !is_unique unless @options[:unique]

  unless is_unique
    @missing = "#{table_name} has an index named #{matched_index.name} " <<
    "of unique #{matched_index.unique}, not #{@options[:unique]}."
  end

  is_unique
end
expectation() click to toggle source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 131
def expectation
  "#{model_class.name} to #{description}"
end
index_exists?() click to toggle source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 96
def index_exists?
  ! matched_index.nil?
end
index_type() click to toggle source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 135
def index_type
  if @options[:unique]
    'unique'
  else
    'non-unique'
  end
end
indexes() click to toggle source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 127
def indexes
  ::ActiveRecord::Base.connection.indexes(table_name)
end
matched_index() click to toggle source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 115
def matched_index
  indexes.detect { |each| each.columns == @columns }
end
model_class() click to toggle source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 119
def model_class
  @subject.class
end
normalize_columns_to_array(columns) click to toggle source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 143
def normalize_columns_to_array(columns)
  Array.wrap(columns).map(&:to_s)
end
table_name() click to toggle source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 123
def table_name
  model_class.table_name
end