Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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][])
3 changes: 3 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
Expand Down
6 changes: 5 additions & 1 deletion lib/rubocop/cop/rails/transaction_exit_statement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions spec/rubocop/cop/rails/transaction_exit_statement_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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