Skip to content

Commit

Permalink
Merge pull request #1335 from Earlopain/error-bulk-change-table
Browse files Browse the repository at this point in the history
Fix an error for `Rails/BulkChangeTable` when the block for `change_table` is empty
  • Loading branch information
koic authored Aug 22, 2024
2 parents 75be327 + 83b185f commit ebd652f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog/fix_error_rails_bulk_change_table.md
Original file line number Diff line number Diff line change
@@ -0,0 +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][])
4 changes: 2 additions & 2 deletions lib/rubocop/cop/rails/bulk_change_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ def on_send(node)
return unless support_bulk_alter?
return unless node.command?(:change_table)
return if include_bulk_options?(node)
return unless node.block_node
return unless (body = node.block_node&.body)

send_nodes = send_nodes_from_change_table_block(node.block_node.body)
send_nodes = send_nodes_from_change_table_block(body)

add_offense_for_change_table(node) if count_transformations(send_nodes) > 1
end
Expand Down
25 changes: 25 additions & 0 deletions spec/rubocop/cop/rails/bulk_change_table_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,25 @@ def change
end
end

shared_examples 'wrong arguments' do
it 'does not register an offense for `change_table` with no block' do
expect_no_offenses(<<~RUBY)
def up
change_table(:users)
end
RUBY
end

it 'does not register an offense for `change_table` with empty block' do
expect_no_offenses(<<~RUBY)
def up
change_table(:users) do
end
end
RUBY
end
end

shared_examples 'no offense' do
it 'does not register an offense when including combinable transformations' do
expect_no_offenses(<<~RUBY)
Expand All @@ -45,6 +64,8 @@ def change
end
RUBY
end

it_behaves_like 'wrong arguments'
end

shared_examples 'offense for mysql' do
Expand Down Expand Up @@ -91,6 +112,8 @@ def change
end
RUBY
end

it_behaves_like 'wrong arguments'
end

shared_examples 'offense for postgresql' do
Expand Down Expand Up @@ -153,6 +176,8 @@ def change
end
RUBY
end

it_behaves_like 'wrong arguments'
end

it_behaves_like 'no offense'
Expand Down

0 comments on commit ebd652f

Please sign in to comment.