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 74
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 97
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 89
def failure_message
  "Expected #{expectation} (#{@missing})"
end
failure_message_when_negated() click to toggle source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 93
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 84
def matches?(subject)
  @subject = subject
  index_exists? && correct_unique?
end
unique(unique = true) click to toggle source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 79
def unique(unique = true)
  @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 111
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 142
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 107
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 146
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 138
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 126
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 130
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 154
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 134
def table_name
  model_class.table_name
end