class Shoulda::Matchers::ActiveRecord::HaveDbIndexMatcher

@private

Attributes

expected_columns[R]
qualifiers[R]
reason[R]
subject[R]

Public Class Methods

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

Public Instance Methods

description() click to toggle source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 154
def description
  description = 'have '

  description <<
    if qualifiers.include?(:unique)
      "#{Shoulda::Matchers::Util.a_or_an(index_type)} "
    else
      'an '
    end

  description << 'index on '

  description << inspected_expected_columns
end
failure_message() click to toggle source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 131
def failure_message
  message =
    "Expected #{described_table_name} to #{positive_expectation}"

  message <<
    if index_exists?
      ". The index does exist, but #{reason}."
    elsif reason
      ", but #{reason}."
    else
      ', but it does not.'
    end

  Shoulda::Matchers.word_wrap(message)
end
failure_message_when_negated() click to toggle source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 147
def failure_message_when_negated
  Shoulda::Matchers.word_wrap(
    "Expected #{described_table_name} not to " +
    "#{negative_expectation}, but it does.",
  )
end
matches?(subject) click to toggle source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 126
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 121
def unique(unique = true)
  @qualifiers[:unique] = unique
  self
end

Private Instance Methods

actual_indexes() click to toggle source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 210
def actual_indexes
  model.connection.indexes(table_name)
end
correct_unique?() click to toggle source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 181
def correct_unique?
  if qualifiers.include?(:unique)
    if qualifiers[:unique] && !matched_index.unique
      @reason = 'it is not unique'
      false
    elsif !qualifiers[:unique] && matched_index.unique
      @reason = 'it is unique'
      false
    else
      true
    end
  else
    true
  end
end
described_table_name() click to toggle source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 214
def described_table_name
  if model
    "the #{table_name} table"
  else
    'a table'
  end
end
formatted_expected_columns() click to toggle source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 260
def formatted_expected_columns
  expected_columns.map do |column|
    if column.match?(/^\w+$/)
      column.to_sym
    else
      column
    end
  end
end
index_exists?() click to toggle source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 177
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 252
def index_type
  if qualifiers[:unique]
    'unique'
  else
    'non-unique'
  end
end
inspected_expected_columns() click to toggle source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 244
def inspected_expected_columns
  if formatted_expected_columns.one?
    formatted_expected_columns.first.inspect
  else
    formatted_expected_columns.inspect
  end
end
matched_index() click to toggle source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 197
def matched_index
  @_matched_index ||=
    if expected_columns.one?
      actual_indexes.detect do |index|
        Array.wrap(index.columns) == expected_columns
      end
    else
      actual_indexes.detect do |index|
        index.columns == expected_columns
      end
    end
end
model() click to toggle source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 270
def model
  subject&.class
end
negative_expectation() click to toggle source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 240
def negative_expectation
  description
end
normalize_columns_to_array(columns) click to toggle source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 173
def normalize_columns_to_array(columns)
  Array.wrap(columns).map(&:to_s)
end
positive_expectation() click to toggle source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 226
def positive_expectation
  if index_exists?
    expectation = "have an index on #{inspected_expected_columns}"

    if qualifiers.include?(:unique)
      expectation << " and for it to be #{index_type}"
    end

    expectation
  else
    description
  end
end
table_name() click to toggle source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 222
def table_name
  model.table_name
end