Skip to content

Commit f9224f9

Browse files
authored
Merge pull request #170 from mvz/make-bulk-change-table-handle-blocks
Don't register BulkChangeTable offense if block intervenes
2 parents 1fc2d47 + cd182c0 commit f9224f9

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Bug fixes
66

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

910
## 2.4.0 (2019-11-27)
@@ -113,3 +114,4 @@
113114
[@gyfis]: https:/github.com/gyfis
114115
[@DNA]: https://github.com/DNA
115116
[@ngouy]: https://github.com/ngouy
117+
[@mvz]: https://github.com/mvz

lib/rubocop/cop/rails/bulk_change_table.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,9 @@ def on_def(node)
138138

139139
recorder = AlterMethodsRecorder.new
140140

141-
node.body.each_child_node(:send) do |send_node|
142-
if combinable_alter_methods.include?(send_node.method_name)
143-
recorder.process(send_node)
141+
node.body.child_nodes.each do |child_node|
142+
if call_to_combinable_alter_method? child_node
143+
recorder.process(child_node)
144144
else
145145
recorder.flush
146146
end
@@ -218,6 +218,11 @@ def support_bulk_alter?
218218
end
219219
end
220220

221+
def call_to_combinable_alter_method?(child_node)
222+
child_node.send_type? &&
223+
combinable_alter_methods.include?(child_node.method_name)
224+
end
225+
221226
def combinable_alter_methods
222227
case database
223228
when MYSQL

spec/rubocop/cop/rails/bulk_change_table_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,18 @@ def change
306306
RUBY
307307
end
308308

309+
it 'does not register an offense when including a block between' do
310+
expect_no_offenses(<<~RUBY)
311+
def change
312+
add_column :users, :name, :string, null: false
313+
User.find_each do |user|
314+
user.update(name: user.nickname)
315+
end
316+
remove_column :users, :nickname
317+
end
318+
RUBY
319+
end
320+
309321
it 'does not register an offense' \
310322
'when including a combinable alter method' do
311323
expect_no_offenses(<<~RUBY)

0 commit comments

Comments
 (0)