diff --git a/lib/shoulda/matchers/active_record/have_secure_token_matcher.rb b/lib/shoulda/matchers/active_record/have_secure_token_matcher.rb index 693c7d8be..bfce629f3 100644 --- a/lib/shoulda/matchers/active_record/have_secure_token_matcher.rb +++ b/lib/shoulda/matchers/active_record/have_secure_token_matcher.rb @@ -41,6 +41,7 @@ class HaveSecureTokenMatcher def initialize(token_attribute) @token_attribute = token_attribute + @options = { ignore_check_for_db_index: false } end def description @@ -65,6 +66,11 @@ def matches?(subject) @errors.empty? end + def ignoring_check_for_db_index + @options[:ignore_check_for_db_index] = true + self + end + private def run_checks @@ -75,7 +81,7 @@ def run_checks if !has_expected_db_column? @errors << "missing correct column #{token_attribute}:string" end - if !has_expected_db_index? + if !@options[:ignore_check_for_db_index] && !has_expected_db_index? @errors << "missing unique index for #{table_and_column}" end @errors diff --git a/spec/unit/shoulda/matchers/active_record/have_secure_token_matcher_spec.rb b/spec/unit/shoulda/matchers/active_record/have_secure_token_matcher_spec.rb index bb2ac9ee8..cc2b203af 100644 --- a/spec/unit/shoulda/matchers/active_record/have_secure_token_matcher_spec.rb +++ b/spec/unit/shoulda/matchers/active_record/have_secure_token_matcher_spec.rb @@ -58,6 +58,16 @@ end end + it 'matches when called with ignoring_check_for_db_index without db index' do + create_table(:users) do |t| + t.string :token + end + + valid_model = define_model_class(:User) { has_secure_token } + expect(valid_model.new). + to have_secure_token.ignoring_check_for_db_index + end + it 'does not match when missing a token column' do create_table(:users) invalid_model = define_model_class(:User) { has_secure_token }