Skip to content

Commit

Permalink
Merge pull request #1366 from Earlopain/transaction-exit-rails-7.2
Browse files Browse the repository at this point in the history
[Fix #727] Disable `Rails/TransactionExitStatement` on Rails >= 7.2
  • Loading branch information
koic authored Sep 28, 2024
2 parents d1b7186 + a171638 commit 466f764
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 466f764

Please sign in to comment.