Class: DraftApprove::Transaction Private

Inherits:
Object
  • Object
show all
Defined in:
lib/draft_approve/transaction.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Logic for handling ActiveRecord database transactions, and creating DraftTransaction records.

Class Method Summary collapse

Class Method Details

.current_draft_transaction!DraftTransaction

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get the current DraftTransaction for this thread / fiber, or raise an error if there is no current DraftTransaction



71
72
73
74
75
# File 'lib/draft_approve/transaction.rb', line 71

def self.current_draft_transaction!
  raise DraftApprove::Errors::NoDraftTransactionError unless current_draft_transaction.present?

  current_draft_transaction
end

.ensure_in_draft_transaction(created_by: nil, extra_data: nil) { ... } ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Ensure the block is running in a database transaction and a DraftTransaction - if there’s not one already, create one.

Parameters:

  • created_by (String) (defaults to: nil)

    the user or process which created this DraftTransaction and the draft changes within it. Ignored if a DraftTransaction already exists within the scope of the current thread / fiber.

  • extra_data (Hash) (defaults to: nil)

    any extra metadata to be stored with this DraftTransaction. Ignored if a DraftTransaction already exists within the scope of the current thread / fiber.

Yields:

  • invokes the block, during which methods should be called to save Draft objects

Returns:

  • (Object)

    the result of yielding to the given block



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/draft_approve/transaction.rb', line 48

def self.ensure_in_draft_transaction(created_by: nil, extra_data: nil)
  draft_transaction = current_draft_transaction

  if draft_transaction
    # There's an existing draft_transaction, just yield to the block
    yield
  else
    # There's no transaction - start one and yield to the block inside the
    # new transaction
    (draft_transaction, yield_return) = in_new_draft_transaction_helper(created_by: created_by, extra_data: extra_data) do
      yield
    end

    # ensure_in_draft_transaction is used in model.draft_save! method calls
    # so we want to return the result of the yield (a draft object) to the caller
    return yield_return
  end
end

.in_new_draft_transaction(created_by: nil, extra_data: nil) { ... } ⇒ DraftTransaction?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Start a new database transaction, and create a new DraftTransaction to wrap the commands in the block.

Parameters:

  • created_by (String) (defaults to: nil)

    the user or process which created this DraftTransaction and the draft changes within it

  • extra_data (Hash) (defaults to: nil)

    any extra metadata to be stored with this DraftTransaction

Yields:

  • invokes the block, during which methods should be called to save Draft objects

Returns:

  • (DraftTransaction, nil)

    the resulting DraftTransaction, or nil if no Draft changes were saved within the given block



24
25
26
27
28
29
30
31
32
# File 'lib/draft_approve/transaction.rb', line 24

def self.in_new_draft_transaction(created_by: nil, extra_data: nil)
  (draft_transaction, yield_return) = in_new_draft_transaction_helper(created_by: created_by, extra_data: extra_data) do
    yield
  end

  # in_new_draft_transaction is used in Model.draft_transaction do ... blocks
  # so we want to return the transaction itself to the caller
  return draft_transaction
end