class Shoulda::Matchers::ActiveRecord::HaveDbIndexMatcher
@private
Attributes
Public Class Methods
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
Source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 154 def description String.new('have ').tap do |description| description << if qualifiers.include?(:unique) "#{Shoulda::Matchers::Util.a_or_an(index_type)} " else 'an ' end description << 'index on ' description << inspected_expected_columns end end
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
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
Source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 126 def matches?(subject) @subject = subject index_exists? && correct_unique? end
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
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
Source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 225 def described_table_name if model "the #{table_name} table" else 'a table' end end
Source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 271 def formatted_expected_columns expected_columns.map do |column| if column.match?(/^\w+$/) column.to_sym else column end end end
Source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 177 def index_exists? !matched_index.nil? end
Source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 263 def index_type if qualifiers[:unique] 'unique' else 'non-unique' end end
Source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 255 def inspected_expected_columns if formatted_expected_columns.one? formatted_expected_columns.first.inspect else formatted_expected_columns.inspect end end
Source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 197 def matched_index @_matched_index ||= if expected_columns.one? sorted_indexes.detect do |index| Array.wrap(index.columns) == expected_columns end else sorted_indexes.detect do |index| index.columns == expected_columns end end end
Source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 281 def model subject&.class end
Source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 251 def negative_expectation description end
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
Source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 237 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
Source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 210 def sorted_indexes if qualifiers.include?(:unique) # return indexes with unique matching the qualifier first unsorted_indexes.sort_by do |index| index.unique == qualifiers[:unique] ? 0 : 1 end else unsorted_indexes end end
Source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 233 def table_name model.table_name end
Source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 221 def unsorted_indexes model.connection.indexes(table_name) end