Sha256: 5f16908d8f1d3e48a17d87f83a907cb846332168b1f816093f65d96670127136

Contents?: true

Size: 943 Bytes

Versions: 13

Compression:

Stored size: 943 Bytes

Contents

class TaxRate < ActiveRecord::Base
  has_and_belongs_to_many :products
  belongs_to :country
  belongs_to :province

  validates_presence_of :title, :rate, :country
  validates_numericality_of :rate, :greater_than => 0.0
  validate :unique_country_and_province

  # the new ActiveAssociation stuff is teh bomb!
  scope :by_country_and_province, joins(:country).joins("LEFT OUTER JOIN provinces ON tax_rates.province_id = provinces.id").order('countries.title, provinces.title')
  
  protected

    def unique_country_and_province
      if self.id
        tax_rates = TaxRate.where(["country_id = ? and province_id = ? and id != ?", self.country_id, self.province_id, self.id]).all
      else
        tax_rates = TaxRate.where(:country_id => self.country_id, :province_id => self.province_id).all
      end
      if tax_rates.size > 0
          errors.add :base, "A tax rate already exists for this country and province"
      end
    end

end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
forge-cli-0.0.18 lib/forge/app/models/tax_rate.rb
forge-cli-0.0.17 lib/forge/app/models/tax_rate.rb
forge-cli-0.0.16 lib/forge/app/models/tax_rate.rb
forge-cli-0.0.15 lib/forge/app/models/tax_rate.rb
forge-cli-0.0.14 lib/forge/app/models/tax_rate.rb
forge-cli-0.0.13 lib/forge/app/models/tax_rate.rb
forge-cli-0.0.12 lib/forge/app/models/tax_rate.rb
forge-cli-0.0.11 lib/forge/app/models/tax_rate.rb
forge-cli-0.0.10 lib/forge/app/models/tax_rate.rb
forge-cli-0.0.9 lib/forge/app/models/tax_rate.rb
forge-cli-0.0.8 lib/forge/app/models/tax_rate.rb
forge-cli-0.0.7 lib/forge/app/models/tax_rate.rb
forge-cli-0.0.6 lib/forge/app/models/tax_rate.rb