Skip to content

Commit

Permalink
[Fix #727] Disable Rails/TransactionExitStatement on Rails >= 7.2
Browse files Browse the repository at this point in the history
On Rails 7.2, the behavior is exactly like it was in earlier Rails.
On Rails 7.1, it is controlled by `active_record.commit_transaction_on_non_local_return`.

rails/rails@eccc606
rails/rails#48600
  • Loading branch information
Earlopain committed Sep 19, 2024
1 parent f935a0b commit a171638
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/change_disable_transaction_exit_rails_7.2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#727](https://github.com/rubocop/rubocop-rails/issues/727): Disable `Rails/TransactionExitStatement` on Rails >= 7.2. ([@earlopain][])
5 changes: 5 additions & 0 deletions lib/rubocop/cop/rails/transaction_exit_statement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ module Rails
#
# If you are defining custom transaction methods, you can configure it with `TransactionMethods`.
#
# NOTE: This cop is disabled on Rails >= 7.2 because transactions were restored
# to their historical behavior. In Rails 7.1, the behavior is controlled with
# the config `active_record.commit_transaction_on_non_local_return`.
#
# @example
# # bad
# ApplicationRecord.transaction do
Expand Down Expand Up @@ -76,6 +80,7 @@ class TransactionExitStatement < Base
PATTERN

def on_send(node)
return if target_rails_version >= 7.2
return unless in_transaction_block?(node)

exit_statements(node.parent.body).each do |statement_node|
Expand Down
10 changes: 10 additions & 0 deletions spec/rubocop/cop/rails/transaction_exit_statement_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,14 @@

it_behaves_like 'flags transaction exit statements', :writable_transaction
end

context 'Rails >= 7.2', :rails72 do
it 'registers no offense' do
expect_no_offenses(<<~RUBY)
ApplicationRecord.transaction do
return if user.active?
end
RUBY
end
end
end
4 changes: 4 additions & 0 deletions spec/support/shared_contexts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@
RSpec.shared_context 'with Rails 7.1', :rails71 do
let(:rails_version) { 7.1 }
end

RSpec.shared_context 'with Rails 7.2', :rails72 do
let(:rails_version) { 7.2 }
end

0 comments on commit a171638

Please sign in to comment.