From fb77a6c310cdcbf3a15ac4e00ef59be5883342bb Mon Sep 17 00:00:00 2001 From: Daniel Morris Date: Tue, 4 Mar 2014 12:47:16 +0000 Subject: [PATCH] Make optional the required argument in have_db_index(...).unique --- NEWS.md | 3 +++ lib/shoulda/matchers/active_record/have_db_index_matcher.rb | 2 +- .../matchers/active_record/have_db_index_matcher_spec.rb | 4 ++++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 88915679..3b8de566 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # HEAD +* Change `have_db_index(...).unique(unique)` so that the `unique` argument + is optional, allowing for the shorter `have_db_index(...).unique` syntax. + * Association matchers now test that the model being referred to (either implicitly or explicitly, using `:class_name`) actually exists. diff --git a/lib/shoulda/matchers/active_record/have_db_index_matcher.rb b/lib/shoulda/matchers/active_record/have_db_index_matcher.rb index e718a1b4..a08aafd0 100644 --- a/lib/shoulda/matchers/active_record/have_db_index_matcher.rb +++ b/lib/shoulda/matchers/active_record/have_db_index_matcher.rb @@ -27,7 +27,7 @@ module Shoulda # :nodoc: @options = {} end - def unique(unique) + def unique(unique = true) @options[:unique] = unique self end diff --git a/spec/shoulda/matchers/active_record/have_db_index_matcher_spec.rb b/spec/shoulda/matchers/active_record/have_db_index_matcher_spec.rb index a05e491e..2ea98575 100644 --- a/spec/shoulda/matchers/active_record/have_db_index_matcher_spec.rb +++ b/spec/shoulda/matchers/active_record/have_db_index_matcher_spec.rb @@ -52,6 +52,10 @@ describe Shoulda::Matchers::ActiveRecord::HaveDbIndexMatcher do expect(have_db_index(:user_id).unique(true).description).to match(/a unique index/) end + it 'describes a unique index as unique when no argument is given' do + expect(have_db_index(:user_id).unique.description).to match(/a unique index/) + end + it 'describes a non-unique index as non-unique' do expect(have_db_index(:user_id).unique(false).description).to match(/a non-unique index/) end