From a17163895bc137e8625bc3056e3d9925e349ba5e Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Thu, 19 Sep 2024 09:58:31 +0200 Subject: [PATCH] [Fix #727] Disable `Rails/TransactionExitStatement` on Rails >= 7.2 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`. https://github.com/rails/rails/commit/eccc6061f4f3abdfdeb9a363987a898418d9498f https://github.com/rails/rails/pull/48600 --- changelog/change_disable_transaction_exit_rails_7.2.md | 1 + lib/rubocop/cop/rails/transaction_exit_statement.rb | 5 +++++ .../cop/rails/transaction_exit_statement_spec.rb | 10 ++++++++++ spec/support/shared_contexts.rb | 4 ++++ 4 files changed, 20 insertions(+) create mode 100644 changelog/change_disable_transaction_exit_rails_7.2.md 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