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][]) 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..16232da763 100644 --- a/spec/rubocop/cop/rails/transaction_exit_statement_spec.rb +++ b/spec/rubocop/cop/rails/transaction_exit_statement_spec.rb @@ -220,5 +220,18 @@ 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