From 99515d047c6773e863ef7c1fa499a4df88750497 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Tue, 15 Sep 2020 22:51:46 +0900 Subject: [PATCH] [Fix #357] Fix a false positive for `Rails/ReversibleMigration` Fixes #357. This PR fixes a false positive for `Rails/ReversibleMigration` when keyword arguments of `change_column_default` are in the order of `to`, `from`. --- CHANGELOG.md | 1 + lib/rubocop/cop/rails/reversible_migration.rb | 2 +- spec/rubocop/cop/rails/reversible_migration_spec.rb | 5 +++++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e036e8717..51e77977d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ * [#351](https://github.com/rubocop-hq/rubocop-rails/pull/351): Add `<>` operator to `Rails/WhereNot` cop. ([@Tietew][]) * [#352](https://github.com/rubocop-hq/rubocop-rails/pull/352): Do not register offense if given a splatted hash. ([@dvandersluis][]) * [#346](https://github.com/rubocop-hq/rubocop-rails/pull/346): Fix a false positive for `Rails/DynamicFindBy` when any of the arguments are splat argument. ([@koic][]) +* [#357](https://github.com/rubocop-hq/rubocop-rails/issues/357): Fix a false positive for `Rails/ReversibleMigration` when keyword arguments of `change_column_default` are in the order of `to`, `from`. ([@koic][]) ## 2.8.0 (2020-09-04) diff --git a/lib/rubocop/cop/rails/reversible_migration.rb b/lib/rubocop/cop/rails/reversible_migration.rb index 04f39aef11..948f65da75 100644 --- a/lib/rubocop/cop/rails/reversible_migration.rb +++ b/lib/rubocop/cop/rails/reversible_migration.rb @@ -360,7 +360,7 @@ def all_hash_key?(args, *keys) key.children.first.to_sym end - hash_keys & keys == keys + (hash_keys & keys).sort == keys end end end diff --git a/spec/rubocop/cop/rails/reversible_migration_spec.rb b/spec/rubocop/cop/rails/reversible_migration_spec.rb index e42a6644ff..2b36341511 100644 --- a/spec/rubocop/cop/rails/reversible_migration_spec.rb +++ b/spec/rubocop/cop/rails/reversible_migration_spec.rb @@ -114,6 +114,11 @@ def change change_column_default(:posts, :state, from: nil, to: "draft") RUBY + it_behaves_like 'accepts', + 'change_column_default(with :to and :from)', <<-RUBY + change_column_default(:posts, :state, to: "draft", from: nil) + RUBY + it_behaves_like 'accepts', 'change_column_default(*column :from and :to)', <<-RUBY columns = [:foo, :bar]