Sha256: 9f975b957105e08e2bdefb57c380c7400fb7c48cf5585ce824f9029d85b8d27c

Contents?: true

Size: 1.22 KB

Versions: 6

Compression:

Stored size: 1.22 KB

Contents

# encoding: utf-8

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
  
    validates :currency_id, :presence => true
    validates :user_id, :presence => true
    
    # Search
    include InvoiceBar::Searchable
    
    def self.searchable_fields
      ['name', 'iban', 'swift', 'bank_account_number']
    end
    
    def currency_name
      currency.name if currency
    end
    
    def currency_symbol
      currency.symbol if currency
    end
  
    private
      
      # Validates uniqueness of a name for current user.
      def name_is_unique
        accounts = Account.where(:name => self.name, :user_id => self.user_id)
    
        if accounts.any?
          errors.add(:name, :uniqueness) unless accounts.include? self
        end
      end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
invoice_bar-0.0.6 app/models/invoice_bar/account.rb
invoice_bar-0.0.5 app/models/invoice_bar/account.rb
invoice_bar-0.0.4 app/models/invoice_bar/account.rb
invoice_bar-0.0.3 app/models/invoice_bar/account.rb
invoice_bar-0.0.2 app/models/invoice_bar/account.rb
invoice_bar-0.0.1 app/models/invoice_bar/account.rb