Class: ActsAsAccount::Account

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create(attributes = nil) ⇒ Object



32
33
34
35
36
# File 'lib/acts_as_account/account.rb', line 32

def create(attributes = nil)
  find_on_error(attributes) do
    super
  end
end

.create!(attributes = nil) ⇒ Object



26
27
28
29
30
# File 'lib/acts_as_account/account.rb', line 26

def create!(attributes = nil)
  find_on_error(attributes) do
    super
  end
end

.delete_account(number) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/acts_as_account/account.rb', line 38

def (number)
  transaction do
     = find(number)
    raise ActiveRecord::ActiveRecordError, "Cannot be deleted" unless .deleteable?

    .holder.destroy if [ManuallyCreatedAccount, GlobalAccount].include?(.holder.class)
    .destroy
  end
end

.find_on_error(attributes) ⇒ Object



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

def find_on_error(attributes)
  yield

# Trying to create a duplicate key on a unique index raises StatementInvalid
rescue ActiveRecord::StatementInvalid
  record = if attributes[:holder]
    attributes[:holder].(attributes[:name])
  else
    where(
      :holder_type => attributes[:holder_type],
      :holder_id   => attributes[:holder_id],
      :name        => attributes[:name]
    ).first
  end
  record || raise("Cannot find or create account with attributes #{attributes.inspect}")
end

.for(name) ⇒ Object



22
23
24
# File 'lib/acts_as_account/account.rb', line 22

def for(name)
  GlobalAccount.find_or_create_by(name: name).
end

.recalculate_all_balancesObject



10
11
12
13
14
# File 'lib/acts_as_account/account.rb', line 10

def recalculate_all_balances
  warn "[DEPRECATION] `recalculate_all_balances` is deprecated and will be removed in a future version. Please use `recalculate_attributes` instead."

  recalculate_attributes
end

.recalculate_attributesObject



16
17
18
19
20
# File 'lib/acts_as_account/account.rb', line 16

def recalculate_attributes
  find_each do ||
    .recalculate_attributes
  end
end

Instance Method Details

#balanceObject



66
67
68
69
70
71
72
# File 'lib/acts_as_account/account.rb', line 66

def balance
  if ActsAsAccount.configuration.
    super
  else
    postings.sum(:amount)
  end
end

#deleteable?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/acts_as_account/account.rb', line 100

def deleteable?
  postings.empty? && journals.empty?
end

#last_valutaObject



82
83
84
85
86
87
88
# File 'lib/acts_as_account/account.rb', line 82

def last_valuta
  if ActsAsAccount.configuration.
    super
  else
    postings.maximum(:valuta)
  end
end

#postings_countObject



74
75
76
77
78
79
80
# File 'lib/acts_as_account/account.rb', line 74

def postings_count
  if ActsAsAccount.configuration.
    super
  else
    postings.count
  end
end

#recalculate_attributesObject



90
91
92
93
94
95
96
97
98
# File 'lib/acts_as_account/account.rb', line 90

def recalculate_attributes
  return unless ActsAsAccount.configuration.

  update_columns(
    last_valuta:    postings.maximum(:valuta),
    balance:        postings.sum(:amount),
    postings_count: postings.count
  )
end