Class: ActsAsAccount::Journal

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/acts_as_account/journal.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.clear_currentObject



17
18
19
# File 'lib/acts_as_account/journal.rb', line 17

def clear_current
  Thread.current[:acts_as_account_current] = nil
end

.currentObject



13
14
15
# File 'lib/acts_as_account/journal.rb', line 13

def current
  Thread.current[:acts_as_account_current] ||= create!
end

Instance Method Details

#transfer(amount, from_account, to_account, reference = nil, valuta = Time.now) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/acts_as_account/journal.rb', line 28

def transfer(amount, , , reference = nil, valuta = Time.now)
  transaction do
    if (amount < 0)
      # change order if amount is negative
      amount, ,  = -amount, , 
    end

    logger.debug { "ActsAsAccount::Journal.transfer amount: #{amount} from:#{.id} to:#{.id} reference:#{reference.class.name}(#{reference.id}) valuta:#{valuta}" } if logger

    # to avoid possible deadlocks we need to ensure that the locking order is always
    # the same therfore the sort by id.
    [, ].sort_by(&:id).map(&:lock!)

    add_posting(-amount,  ,   , reference, valuta)
    add_posting( amount,    , , reference, valuta)
  end
end

#transfersObject



22
23
24
25
26
# File 'lib/acts_as_account/journal.rb', line 22

def transfers
  [].tap do |transfers|
    postings.each_slice(2) { |postings| transfers << Transfer.new(*postings) }
  end
end