Sha256: e36ab98ff31c1b6b63ad326b6d334982eb45be3e36e71e3536c4c52725ffbab0

Contents?: true

Size: 821 Bytes

Versions: 5

Compression:

Stored size: 821 Bytes

Contents

module InvoiceBar
  class InvoiceTemplate < ActiveRecord::Base
    before_validation :update_amount

    attr_accessible :name

    validates :name, presence: true
    validate :name_is_unique

    include InvoiceBar::Billable::Base
    include InvoiceBar::Billable::Invoicing

    include InvoiceBar::Billable::Associations::Base
    delegate :name, :tax_id, :address, to: :user, prefix: true

    # Search
    include InvoiceBar::Searchable

    def self.searchable_fields
      %w( name )
    end

    private

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

        if invoice_templates.any? && !invoice_templates.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/invoice_template.rb
invoice_bar-0.0.10 app/models/invoice_bar/invoice_template.rb
invoice_bar-0.0.9 app/models/invoice_bar/invoice_template.rb
invoice_bar-0.0.8 app/models/invoice_bar/invoice_template.rb
invoice_bar-0.0.7 app/models/invoice_bar/invoice_template.rb