Class: CleanActions::TransactionRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/clean_actions/transaction_runner.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(action) ⇒ TransactionRunner

Returns a new instance of TransactionRunner.



16
17
18
# File 'lib/clean_actions/transaction_runner.rb', line 16

def initialize(action)
  @action = action
end

Class Method Details

.action_calls_restricted_byObject



11
12
13
# File 'lib/clean_actions/transaction_runner.rb', line 11

def action_calls_restricted_by
  Thread.current[:action_calls_restricted_by]
end

.restrict_action_calls_by(method) ⇒ Object



4
5
6
7
8
9
# File 'lib/clean_actions/transaction_runner.rb', line 4

def restrict_action_calls_by(method)
  Thread.current[:action_calls_restricted_by] = method
  yield
ensure
  Thread.current[:action_calls_restricted_by] = nil
end

Instance Method Details

#run(with_savepoint: false, &block) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/clean_actions/transaction_runner.rb', line 20

def run(with_savepoint: false, &block)
  performed_actions << @action

  if Thread.current[:transaction_started]
    unless IsolationLevelValidator.can_be_nested(action_isolation_level)
      ErrorReporter.report <<~MSG
        action #{@action.class.name} requires #{action_isolation_level}, run inside #{Thread.current[:root_isolation_level]}
      MSG
    end

    if with_savepoint
      return ActiveRecord::Base.transaction(requires_new: true) { block.call }
    else
      return block.call
    end
  end

  start_transaction(&block)
end