From 341bbfd417c37f787689ba929b014f50aed06cd7 Mon Sep 17 00:00:00 2001 From: Ian Pearce Date: Thu, 25 Sep 2025 14:20:55 -0700 Subject: [PATCH 1/3] Allow TransactionExitStatement to be configured to be enabled for all Rails versions --- config/default.yml | 3 +++ lib/rubocop/cop/rails/transaction_exit_statement.rb | 6 +++++- .../cop/rails/transaction_exit_statement_spec.rb | 10 ++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/config/default.yml b/config/default.yml index 74bebd3142..0f4570c903 100644 --- a/config/default.yml +++ b/config/default.yml @@ -1202,6 +1202,9 @@ Rails/TransactionExitStatement: Enabled: pending VersionAdded: '2.14' TransactionMethods: [] + # This rule is disabled by default in Rails 7.2 and above + # Set EnableForAllRailsVersions to true to enable this rule regardless of Rails version + EnableForAllRailsVersions: false Rails/UniqBeforePluck: Description: 'Prefer the use of uniq or distinct before pluck.' diff --git a/lib/rubocop/cop/rails/transaction_exit_statement.rb b/lib/rubocop/cop/rails/transaction_exit_statement.rb index 108fb67957..b2505ee780 100644 --- a/lib/rubocop/cop/rails/transaction_exit_statement.rb +++ b/lib/rubocop/cop/rails/transaction_exit_statement.rb @@ -80,7 +80,7 @@ class TransactionExitStatement < Base PATTERN def on_send(node) - return if target_rails_version >= 7.2 + return unless enable_for_all_rails_versions? || target_rails_version < 7.2 return unless in_transaction_block?(node) exit_statements(node.parent.body).each do |statement_node| @@ -127,6 +127,10 @@ def transaction_method_name?(method_name) def transaction_method?(method_name) cop_config.fetch('TransactionMethods', []).include?(method_name.to_s) end + + def enable_for_all_rails_versions? + cop_config.fetch('EnableForAllRailsVersions', false) + end end end end diff --git a/spec/rubocop/cop/rails/transaction_exit_statement_spec.rb b/spec/rubocop/cop/rails/transaction_exit_statement_spec.rb index 775b0f4c31..f463374dbe 100644 --- a/spec/rubocop/cop/rails/transaction_exit_statement_spec.rb +++ b/spec/rubocop/cop/rails/transaction_exit_statement_spec.rb @@ -220,5 +220,15 @@ end RUBY end + + context 'when EnableForAllRailsVersions is true' do + let(:cop_config) { { 'EnableForAllRailsVersions' => true } } + it_behaves_like 'flags transaction exit statements', :transaction + it_behaves_like 'flags transaction exit statements', :with_lock + context 'when `TransactionMethods: [writable_transaction]`' do + let(:cop_config) { super().merge({ 'TransactionMethods' => %w[writable_transaction] }) } + it_behaves_like 'flags transaction exit statements', :writable_transaction + end + end end end From 003d6c60da08e2ac0d7c24cb744c0d2cfe7644c1 Mon Sep 17 00:00:00 2001 From: Ian Pearce Date: Thu, 25 Sep 2025 19:34:11 -0700 Subject: [PATCH 2/3] Add whitespace --- spec/rubocop/cop/rails/transaction_exit_statement_spec.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/spec/rubocop/cop/rails/transaction_exit_statement_spec.rb b/spec/rubocop/cop/rails/transaction_exit_statement_spec.rb index f463374dbe..16232da763 100644 --- a/spec/rubocop/cop/rails/transaction_exit_statement_spec.rb +++ b/spec/rubocop/cop/rails/transaction_exit_statement_spec.rb @@ -223,10 +223,13 @@ context 'when EnableForAllRailsVersions is true' do let(:cop_config) { { 'EnableForAllRailsVersions' => true } } + it_behaves_like 'flags transaction exit statements', :transaction it_behaves_like 'flags transaction exit statements', :with_lock + context 'when `TransactionMethods: [writable_transaction]`' do let(:cop_config) { super().merge({ 'TransactionMethods' => %w[writable_transaction] }) } + it_behaves_like 'flags transaction exit statements', :writable_transaction end end From 82f9b25bbd931914610a9a15006f7bc0731a684c Mon Sep 17 00:00:00 2001 From: Ian Pearce Date: Wed, 1 Oct 2025 08:22:49 -0700 Subject: [PATCH 3/3] Add changelog entry --- .../new_opt_in_to_transaction_exit_statement_after_rails_7_1.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/new_opt_in_to_transaction_exit_statement_after_rails_7_1.md diff --git a/changelog/new_opt_in_to_transaction_exit_statement_after_rails_7_1.md b/changelog/new_opt_in_to_transaction_exit_statement_after_rails_7_1.md new file mode 100644 index 0000000000..74cc333766 --- /dev/null +++ b/changelog/new_opt_in_to_transaction_exit_statement_after_rails_7_1.md @@ -0,0 +1 @@ +* [#1535](https://github.com/rubocop/rubocop-rails/pull/1535): Allow TransactionExitStatement to be configured to be enabled for all Rails versions ([@tony-pizza][])