diff --git a/CHANGELOG.md b/CHANGELOG.md index 80d975bffa..4e95ce8914 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ * [#180](https://github.com/rubocop-hq/rubocop-rails/issues/180): Fix a false positive for `HttpPositionalArguments` when using `get` method with `:to` option. ([@koic][]) * [#193](https://github.com/rubocop-hq/rubocop-rails/issues/193): Make `Rails/EnvironmentComparison` aware of `Rails.env` is used in RHS or when `!=` is used for comparison. ([@koic][]) +* [#205](https://github.com/rubocop-hq/rubocop-rails/pull/205): Make `Rails/ReversibleMigration` aware of `:to_table` option of `remove_foreign_key`. ([@joshpencheon][]) ## 2.4.2 (2020-01-26) diff --git a/lib/rubocop/cop/rails/reversible_migration.rb b/lib/rubocop/cop/rails/reversible_migration.rb index dde1924211..a221a40c51 100644 --- a/lib/rubocop/cop/rails/reversible_migration.rb +++ b/lib/rubocop/cop/rails/reversible_migration.rb @@ -90,6 +90,11 @@ module Rails # remove_foreign_key :accounts, :branches # end # + # # good + # def change + # remove_foreign_key :accounts, to_table: :branches + # end + # # @example # # change_table # @@ -210,7 +215,7 @@ def check_remove_column_node(node) def check_remove_foreign_key_node(node) remove_foreign_key_call(node) do |arg| - if arg.hash_type? + if arg.hash_type? && !all_hash_key?(arg, :to_table) add_offense( node, message: format(MSG, diff --git a/spec/rubocop/cop/rails/reversible_migration_spec.rb b/spec/rubocop/cop/rails/reversible_migration_spec.rb index 20dcd6280f..18f7a8696d 100644 --- a/spec/rubocop/cop/rails/reversible_migration_spec.rb +++ b/spec/rubocop/cop/rails/reversible_migration_spec.rb @@ -169,6 +169,11 @@ def change remove_foreign_key :accounts, :branches RUBY + it_behaves_like 'accepts', + 'remove_foreign_key(with to_table option)', <<-RUBY + remove_foreign_key :accounts, to_table: :branches + RUBY + it_behaves_like 'offense', 'remove_foreign_key(without table)', <<~RUBY remove_foreign_key :accounts, column: :owner_id RUBY