Sha256: 0fafbebae24254b73873e2bbddb9ed879fd4b5b37f412973d93ea480dccabf70

Contents?: true

Size: 1.38 KB

Versions: 5

Compression:

Stored size: 1.38 KB

Contents

module InvoiceBar
  class Account < ActiveRecord::Base
    attr_accessible :amount, :bank_account_number, :iban, :name, :swift

    validates :name,    presence: true
    validates :amount,  presence: true, numericality: true

    validates :iban,  length: { in: 15..34 }, allow_blank: true
    validates :swift, length: { in: 8..11 }, allow_blank: true

    validate :name_is_unique

    # Assosiations
    attr_accessible :user_id, :currency_id

    belongs_to :currency
    belongs_to :user
    has_many :invoices
    has_many :receipts

    validates :currency_id, presence: true
    validates :user_id, presence: true

    # Search
    include InvoiceBar::Searchable

    def self.searchable_fields
      %w( name iban swift bank_account_number )
    end

    def currency_name
      currency.name if currency
    end

    def currency_symbol
      currency.symbol if currency
    end

    def balance
      balance = amount

      receipts.expense.each do |receipt|
        balance -= receipt.amount
      end

      receipts.income.each do |receipt|
        balance += receipt.amount
      end

      balance
    end

    private

      # Validates uniqueness of a name for current user.
      def name_is_unique
        accounts = Account.where(name: name, user_id: user_id)

        if accounts.any? && !accounts.include?(self)
          errors.add(:name, :uniqueness)
        end
      end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
invoice_bar-0.0.11 app/models/invoice_bar/account.rb
invoice_bar-0.0.10 app/models/invoice_bar/account.rb
invoice_bar-0.0.9 app/models/invoice_bar/account.rb
invoice_bar-0.0.8 app/models/invoice_bar/account.rb
invoice_bar-0.0.7 app/models/invoice_bar/account.rb