Module: Granite::Action::TransactionManager

Defined in:
lib/granite/action/transaction_manager.rb,
lib/granite/action/transaction_manager/transactions_stack.rb

Overview

:nodoc:

Defined Under Namespace

Classes: TransactionsStack

Class Method Summary collapse

Class Method Details

.after_commit(listener = nil, &block) ⇒ Object

Adds a block or listener object to be executed after finishing the current transaction. Callbacks are reset after each transaction.

Parameters:

  • listener (Object) (defaults to: nil)

    an object which will receive ‘run_callbacks(:commit)` after transaction committed

  • block (Proc)

    a block which will be called after transaction committed



23
24
25
26
27
28
29
# File 'lib/granite/action/transaction_manager.rb', line 23

def after_commit(listener = nil, &block)
  callback = listener || block

  raise 'Block or object is required to register after_commit hook!' unless callback

  transactions_stack.add_callback callback
end

.transaction(&block) ⇒ Object

Runs a block in a transaction It will open a new transaction or append a block to the current one if it exists

Returns:

  • (Object)

    result of a block



13
14
15
16
17
# File 'lib/granite/action/transaction_manager.rb', line 13

def transaction(&block)
  run_in_transaction(&block) || false
ensure
  finish_root_transaction if transactions_stack.depth.zero?
end