Class: RuboCop::Cop::Rails::TransactionExitStatement
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Rails::TransactionExitStatement
- Defined in:
- lib/rubocop/cop/rails/transaction_exit_statement.rb
Overview
This cop checks for the use of exit statements (namely `return`, `break` and `throw`) in transactions. This is due to the eventual unexpected behavior when using ActiveRecord >= 7, where transactions exitted using these statements are being rollbacked rather than committed (pre ActiveRecord 7 behavior).
As alternatives, it would be more intuitive to explicitly raise an error when rollback is desired, and to use `next` when commit is desired.
Constant Summary collapse
- MSG =
<<~MSG.chomp Exit statement `%<statement>s` is not allowed. Use `raise` (rollback) or `next` (commit). MSG
- RESTRICT_ON_SEND =
%i[transaction].freeze
Instance Method Summary collapse
Instance Method Details
#on_send(node) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/rubocop/cop/rails/transaction_exit_statement.rb', line 56 def on_send(node) parent = node.parent return unless parent&.block_type? exit_statements(parent.body).each do |statement_node| statement = if statement_node.return_type? 'return' elsif statement_node.break_type? 'break' else statement_node.method_name end = format(MSG, statement: statement) add_offense(statement_node, message: ) end end |