class ActsAsAccount::Account

Public Class Methods

create(attributes = nil) click to toggle source
Calls superclass method
# File lib/acts_as_account/account.rb, line 32
def create(attributes = nil)
  find_on_error(attributes) do
    super
  end
end
create!(attributes = nil) click to toggle source
Calls superclass method
# File lib/acts_as_account/account.rb, line 26
def create!(attributes = nil)
  find_on_error(attributes) do
    super
  end
end
delete_account(number) click to toggle source
# File lib/acts_as_account/account.rb, line 38
def delete_account(number)
  transaction do
    account = find(number)
    raise ActiveRecord::ActiveRecordError, "Cannot be deleted" unless account.deleteable?

    account.holder.destroy if [ManuallyCreatedAccount, GlobalAccount].include?(account.holder.class)
    account.destroy
  end
end
find_on_error(attributes) { || ... } click to toggle source
# 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].account(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) click to toggle source
# File lib/acts_as_account/account.rb, line 22
def for(name)
  GlobalAccount.find_or_create_by(name: name).account
end
recalculate_all_balances() click to toggle source
# 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_attributes() click to toggle source
# File lib/acts_as_account/account.rb, line 16
def recalculate_attributes
  find_each do |account|
    account.recalculate_attributes
  end
end

Public Instance Methods

balance() click to toggle source
Calls superclass method
# File lib/acts_as_account/account.rb, line 66
def balance
  if ActsAsAccount.configuration.persist_attributes_on_account
    super
  else
    postings.sum(:amount)
  end
end
deleteable?() click to toggle source
# File lib/acts_as_account/account.rb, line 100
def deleteable?
  postings.empty? && journals.empty?
end
last_valuta() click to toggle source
Calls superclass method
# File lib/acts_as_account/account.rb, line 82
def last_valuta
  if ActsAsAccount.configuration.persist_attributes_on_account
    super
  else
    postings.maximum(:valuta)
  end
end
postings_count() click to toggle source
Calls superclass method
# File lib/acts_as_account/account.rb, line 74
def postings_count
  if ActsAsAccount.configuration.persist_attributes_on_account
    super
  else
    postings.count
  end
end
recalculate_attributes() click to toggle source
# File lib/acts_as_account/account.rb, line 90
def recalculate_attributes
  return unless ActsAsAccount.configuration.persist_attributes_on_account

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