Class: SimpleWallet::AccountDebitingService

Inherits:
Service
  • Object
show all
Defined in:
app/services/simple_wallet/account_debiting_service.rb

Instance Method Summary collapse

Methods inherited from Service

#errors_sentence, #t

Constructor Details

#initialize(account:, amount: nil, source: nil, note: nil, up_to_account_balance: false) ⇒ AccountDebitingService

Returns a new instance of AccountDebitingService.



7
8
9
10
11
12
13
# File 'app/services/simple_wallet/account_debiting_service.rb', line 7

def initialize(account:, amount: nil, source: nil, note: nil, up_to_account_balance: false)
  @account = 
  @amount = amount
  @source = source
  @up_to_account_balance = 
  @note = note
end

Instance Method Details

#debit(set_transaction_isolation_level: true) ⇒ Object

Use set_transaction_isolation_level: false to avoid getting this exception:

ActiveRecord::TransactionIsolationError (cannot set isolation when joining a transaction)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/services/simple_wallet/account_debiting_service.rb', line 17

def debit(set_transaction_isolation_level: true)
  # Checking account's balance should be wrapped in a transaction:
  # Setting transaction isolation level in tests is problematic due to nested transactions :|
  isolation_level = (Rails.env.test? || !set_transaction_isolation_level) ? nil : :serializable

  ActiveRecord::Base.transaction(isolation: isolation_level) do
    return false unless valid?
    unless debiting_transaction.valid?
      copy_errors_from(debiting_transaction)
      return false
    end

    debiting_transaction.save!
    after_operation
  end

  true
end