diff --git a/changelog/change_disable_transaction_exit_rails_7.2.md b/changelog/change_disable_transaction_exit_rails_7.2.md new file mode 100644 index 0000000000..c199c04e3f --- /dev/null +++ b/changelog/change_disable_transaction_exit_rails_7.2.md @@ -0,0 +1 @@ +* [#727](https://github.com/rubocop/rubocop-rails/issues/727): Disable `Rails/TransactionExitStatement` on Rails >= 7.2. ([@earlopain][]) diff --git a/lib/rubocop/cop/rails/transaction_exit_statement.rb b/lib/rubocop/cop/rails/transaction_exit_statement.rb index dc22555cfb..508e06697e 100644 --- a/lib/rubocop/cop/rails/transaction_exit_statement.rb +++ b/lib/rubocop/cop/rails/transaction_exit_statement.rb @@ -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 @@ -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| diff --git a/spec/rubocop/cop/rails/transaction_exit_statement_spec.rb b/spec/rubocop/cop/rails/transaction_exit_statement_spec.rb index 2db1d3ea8a..8cf2b76317 100644 --- a/spec/rubocop/cop/rails/transaction_exit_statement_spec.rb +++ b/spec/rubocop/cop/rails/transaction_exit_statement_spec.rb @@ -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 diff --git a/spec/support/shared_contexts.rb b/spec/support/shared_contexts.rb index bd659b6a0b..c9df13973b 100644 --- a/spec/support/shared_contexts.rb +++ b/spec/support/shared_contexts.rb @@ -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