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 99
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
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 94
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 113
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 144
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 109
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 148
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 140
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 128
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 132
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 156
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 136
def table_name
  model_class.table_name
end