diff options
-rw-r--r-- | rubocop/cop/migration/add_reference.rb | 9 | ||||
-rw-r--r-- | spec/rubocop/cop/migration/add_reference_spec.rb | 12 |
2 files changed, 17 insertions, 4 deletions
diff --git a/rubocop/cop/migration/add_reference.rb b/rubocop/cop/migration/add_reference.rb index 4b67270c97a..1d471b9797e 100644 --- a/rubocop/cop/migration/add_reference.rb +++ b/rubocop/cop/migration/add_reference.rb @@ -8,7 +8,7 @@ module RuboCop class AddReference < RuboCop::Cop::Cop include MigrationHelpers - MSG = '`add_reference` requires `index: true`' + MSG = '`add_reference` requires `index: true` or `index: { options... }`' def on_send(node) return unless in_migration?(node) @@ -33,7 +33,12 @@ module RuboCop private def index_enabled?(pair) - hash_key_type(pair) == :sym && hash_key_name(pair) == :index && pair.children[1].true_type? + return unless hash_key_type(pair) == :sym + return unless hash_key_name(pair) == :index + + index = pair.children[1] + + index.true_type? || index.hash_type? end def hash_key_type(pair) diff --git a/spec/rubocop/cop/migration/add_reference_spec.rb b/spec/rubocop/cop/migration/add_reference_spec.rb index 8f795bb561e..c348fc0efac 100644 --- a/spec/rubocop/cop/migration/add_reference_spec.rb +++ b/spec/rubocop/cop/migration/add_reference_spec.rb @@ -29,7 +29,7 @@ describe RuboCop::Cop::Migration::AddReference do expect_offense(<<~RUBY) call do add_reference(:projects, :users) - ^^^^^^^^^^^^^ `add_reference` requires `index: true` + ^^^^^^^^^^^^^ `add_reference` requires `index: true` or `index: { options... }` end RUBY end @@ -38,7 +38,7 @@ describe RuboCop::Cop::Migration::AddReference do expect_offense(<<~RUBY) def up add_reference(:projects, :users, index: false) - ^^^^^^^^^^^^^ `add_reference` requires `index: true` + ^^^^^^^^^^^^^ `add_reference` requires `index: true` or `index: { options... }` end RUBY end @@ -50,5 +50,13 @@ describe RuboCop::Cop::Migration::AddReference do end RUBY end + + it 'does not register an offense when the index is unique' do + expect_no_offenses(<<~RUBY) + def up + add_reference(:projects, :users, index: { unique: true } ) + end + RUBY + end end end |