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 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
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 210 def actual_indexes model.connection.indexes(table_name) end
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 214 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 260 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 252 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 244 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? 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
Source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 270 def model subject&.class end
Source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 240 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 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
Source
# File lib/shoulda/matchers/active_record/have_db_index_matcher.rb, line 222 def table_name model.table_name end