Class: Effective::AfterCommit

Inherits:
Object
  • Object
show all
Defined in:
app/models/effective/after_commit.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection:, **handlers) ⇒ AfterCommit

Returns a new instance of AfterCommit.



4
5
6
7
# File 'app/models/effective/after_commit.rb', line 4

def initialize(connection:, **handlers)
  @connection = connection
  @handlers = handlers
end

Class Method Details

.register_callback(connection:, name:, no_tx_action:, callback:) ⇒ Object

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/models/effective/after_commit.rb', line 33

def self.register_callback(connection:, name:, no_tx_action:, callback:)
  raise ArgumentError, "#{name} expected a block" unless callback

  unless (connection.transaction_open? && connection.current_transaction.joinable?)
    case no_tx_action
    when :warn_and_execute
      warn "#{name}: No transaction open. Executing callback immediately."
      return callback.call
    when :execute
      return callback.call
    when :exception
      raise("#{name} is useless outside transaction")
    end
  end

  after_commit = Effective::AfterCommit.new(connection: connection, "#{name}": callback)
  connection.add_transaction_record(after_commit)
end

Instance Method Details

#add_to_transactionObject



29
30
31
# File 'app/models/effective/after_commit.rb', line 29

def add_to_transaction(*)
  @connection.add_transaction_record(self)
end

#before_committed!Object



17
18
19
# File 'app/models/effective/after_commit.rb', line 17

def before_committed!(*)
  @handlers[:before_commit]&.call
end

#committed!(args) ⇒ Object



21
22
23
# File 'app/models/effective/after_commit.rb', line 21

def committed!(args)
  @handlers[:after_commit]&.call
end

#has_transactional_callbacks?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'app/models/effective/after_commit.rb', line 9

def has_transactional_callbacks?
  true
end

#rolledback!Object



25
26
27
# File 'app/models/effective/after_commit.rb', line 25

def rolledback!(*)
  @handlers[:after_rollback]&.call
end

#trigger_transactional_callbacks?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'app/models/effective/after_commit.rb', line 13

def trigger_transactional_callbacks?
  true
end