Skip to content

Commit ebd652f

Browse files
authored
Merge pull request #1335 from Earlopain/error-bulk-change-table
Fix an error for `Rails/BulkChangeTable` when the block for `change_table` is empty
2 parents 75be327 + 83b185f commit ebd652f

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1335](https://github.com/rubocop/rubocop-rails/pull/1335): Fix an error for `Rails/BulkChangeTable` when the block for `change_table` is empty. ([@earlopain][])

lib/rubocop/cop/rails/bulk_change_table.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@ def on_send(node)
140140
return unless support_bulk_alter?
141141
return unless node.command?(:change_table)
142142
return if include_bulk_options?(node)
143-
return unless node.block_node
143+
return unless (body = node.block_node&.body)
144144

145-
send_nodes = send_nodes_from_change_table_block(node.block_node.body)
145+
send_nodes = send_nodes_from_change_table_block(body)
146146

147147
add_offense_for_change_table(node) if count_transformations(send_nodes) > 1
148148
end

spec/rubocop/cop/rails/bulk_change_table_spec.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,25 @@ def change
2525
end
2626
end
2727

28+
shared_examples 'wrong arguments' do
29+
it 'does not register an offense for `change_table` with no block' do
30+
expect_no_offenses(<<~RUBY)
31+
def up
32+
change_table(:users)
33+
end
34+
RUBY
35+
end
36+
37+
it 'does not register an offense for `change_table` with empty block' do
38+
expect_no_offenses(<<~RUBY)
39+
def up
40+
change_table(:users) do
41+
end
42+
end
43+
RUBY
44+
end
45+
end
46+
2847
shared_examples 'no offense' do
2948
it 'does not register an offense when including combinable transformations' do
3049
expect_no_offenses(<<~RUBY)
@@ -45,6 +64,8 @@ def change
4564
end
4665
RUBY
4766
end
67+
68+
it_behaves_like 'wrong arguments'
4869
end
4970

5071
shared_examples 'offense for mysql' do
@@ -91,6 +112,8 @@ def change
91112
end
92113
RUBY
93114
end
115+
116+
it_behaves_like 'wrong arguments'
94117
end
95118

96119
shared_examples 'offense for postgresql' do
@@ -153,6 +176,8 @@ def change
153176
end
154177
RUBY
155178
end
179+
180+
it_behaves_like 'wrong arguments'
156181
end
157182

158183
it_behaves_like 'no offense'

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy