class Keepr::Journal

Public Instance Methods

amount() click to toggle source
# File lib/keepr/journal.rb, line 28
def amount
  debit_postings.sum(&:amount)
end
credit_postings() click to toggle source
# File lib/keepr/journal.rb, line 20
def credit_postings
  existing_postings.select(&:credit?)
end
debit_postings() click to toggle source
# File lib/keepr/journal.rb, line 24
def debit_postings
  existing_postings.select(&:debit?)
end

Private Instance Methods

check_permanent() click to toggle source
# File lib/keepr/journal.rb, line 56
def check_permanent
  return unless permanent_was

  # If marked as permanent, no changes are allowed
  errors.add :base, :changes_not_allowed

  if ActiveRecord::VERSION::MAJOR < 5
    false
  else
    throw :abort
  end
end
existing_postings() click to toggle source
# File lib/keepr/journal.rb, line 38
def existing_postings
  keepr_postings.to_a.delete_if(&:marked_for_destruction?)
end
set_defaults() click to toggle source
# File lib/keepr/journal.rb, line 42
def set_defaults
  self.date ||= Date.today
end
validate_postings() click to toggle source
# File lib/keepr/journal.rb, line 46
def validate_postings
  if existing_postings.map(&:keepr_account_id).uniq.length < 2
    # At least two accounts have to be booked
    errors.add :base, :account_missing
  elsif existing_postings.map(&:raw_amount).compact.sum != 0
    # Debit does not match credit
    errors.add :base, :amount_mismatch
  end
end