Sha256: 12e8754533183f61fef85bf8ed07892c88cba64c2f8bf63251d17bb8a20c5843

Contents?: true

Size: 1.16 KB

Versions: 6

Compression:

Stored size: 1.16 KB

Contents

# encoding: utf-8

module InvoiceBar
  class Invoice < ActiveRecord::Base
    before_validation :update_amount
  
    attr_accessible :number, :sent, :paid
  
    validates :number, presence: true
    validate :number_is_unique
  
    validates :due_date, presence: true

    include InvoiceBar::Billable::Base
    include InvoiceBar::Billable::StrictValidations
    include InvoiceBar::Billable::Invoicing
    
    include InvoiceBar::Billable::Associations::Base
    include InvoiceBar::Billable::Associations::StrictValidations
    
    belongs_to :receipt
    
    include InvoiceBar::Billable::Filters
    
    # Search
    include InvoiceBar::Searchable
    
    def self.searchable_fields
      ['number', 'contact_name', 'contact_ic']
    end
    
    def mark_as_paid
      self.paid = true
    end
  
    def mark_as_sent
      self.sent = true
    end
    
    private
    
      # Validates uniqueness of a number for current user.
      def number_is_unique
        invoices = Invoice.where(:number => self.number, :user_id => self.user_id)

        if invoices.any?
          errors.add(:number, :uniqueness) unless invoices.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/invoice.rb
invoice_bar-0.0.5 app/models/invoice_bar/invoice.rb
invoice_bar-0.0.4 app/models/invoice_bar/invoice.rb
invoice_bar-0.0.3 app/models/invoice_bar/invoice.rb
invoice_bar-0.0.2 app/models/invoice_bar/invoice.rb
invoice_bar-0.0.1 app/models/invoice_bar/invoice.rb