Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix an error for Rails/BulkChangeTable when the block for change_table is empty #1335

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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