Sha256: d88cde050b21980eeca13bd09f51ced230567384e612eb5233ff3037a2942a97

Contents?: true

Size: 1009 Bytes

Versions: 11

Compression:

Stored size: 1009 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') }

  # open up everything for mass assignment
  attr_protected
  
  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

11 entries across 11 versions & 1 rubygems

Version Path
forge-cli-0.1.10 lib/forge/app/models/tax_rate.rb
forge-cli-0.1.9 lib/forge/app/models/tax_rate.rb
forge-cli-0.1.8 lib/forge/app/models/tax_rate.rb
forge-cli-0.1.7 lib/forge/app/models/tax_rate.rb
forge-cli-0.1.6 lib/forge/app/models/tax_rate.rb
forge-cli-0.1.5 lib/forge/app/models/tax_rate.rb
forge-cli-0.1.4 lib/forge/app/models/tax_rate.rb
forge-cli-0.1.3 lib/forge/app/models/tax_rate.rb
forge-cli-0.1.2 lib/forge/app/models/tax_rate.rb
forge-cli-0.1.1 lib/forge/app/models/tax_rate.rb
forge-cli-0.1.0 lib/forge/app/models/tax_rate.rb