Skip to content

Commit

Permalink
Don't register BulkChangeTable offense if block intervenes
Browse files Browse the repository at this point in the history
  • Loading branch information
mvz committed Dec 6, 2019
1 parent 8d48d8d commit cd182c0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Bug fixes

* [#170](https://github.com/rubocop-hq/rubocop-rails/pull/170): Make `Rails/BulkChangeTable` not suggest combining methods with an intervening block. ([@mvz][])
* [#159](https://github.com/rubocop-hq/rubocop-rails/issues/159): Fix autocorrect for `Rails/EnumHash` when using % arrays notations. ([@ngouy][])

## 2.4.0 (2019-11-27)
Expand Down Expand Up @@ -113,3 +114,4 @@
[@gyfis]: https:/github.com/gyfis
[@DNA]: https://github.com/DNA
[@ngouy]: https://github.com/ngouy
[@mvz]: https://github.com/mvz
11 changes: 8 additions & 3 deletions lib/rubocop/cop/rails/bulk_change_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ def on_def(node)

recorder = AlterMethodsRecorder.new

node.body.each_child_node(:send) do |send_node|
if combinable_alter_methods.include?(send_node.method_name)
recorder.process(send_node)
node.body.child_nodes.each do |child_node|
if call_to_combinable_alter_method? child_node
recorder.process(child_node)
else
recorder.flush
end
Expand Down Expand Up @@ -218,6 +218,11 @@ def support_bulk_alter?
end
end

def call_to_combinable_alter_method?(child_node)
child_node.send_type? &&
combinable_alter_methods.include?(child_node.method_name)
end

def combinable_alter_methods
case database
when MYSQL
Expand Down
12 changes: 12 additions & 0 deletions spec/rubocop/cop/rails/bulk_change_table_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,18 @@ def change
RUBY
end

it 'does not register an offense when including a block between' do
expect_no_offenses(<<~RUBY)
def change
add_column :users, :name, :string, null: false
User.find_each do |user|
user.update(name: user.nickname)
end
remove_column :users, :nickname
end
RUBY
end

it 'does not register an offense' \
'when including a combinable alter method' do
expect_no_offenses(<<~RUBY)
Expand Down

0 comments on commit cd182c0

Please sign in to comment.