Sha256: f3a8c616497bf69d5db495abcdcfab2591131dfd4b52e58e40f8373c7cd7727c

Contents?: true

Size: 1.17 KB

Versions: 2

Compression:

Stored size: 1.17 KB

Contents

module Piggybak
  class TaxMethod < ActiveRecord::Base
    has_many :tax_method_values, :dependent => :destroy
    alias :metadata :tax_method_values

    validates_presence_of :description
    validates_presence_of :klass

    accepts_nested_attributes_for :tax_method_values, :allow_destroy => true

    validates_each :tax_method_values do |record, attr, value|
      if record.klass
        calculator = record.klass.constantize
        metadata_keys = value.collect { |v| v.key }.sort
        if calculator::KEYS.sort != metadata_keys
          record.errors.add attr, "You must define key values for #{calculator::KEYS.join(', ')} for this tax method."
        end
      end
    end

    def klass_enum 
      #TODO: Troubleshoot use of subclasses here instead
      [Piggybak::TaxCalculator::FlatRate]
    end

    def self.calculate_tax(object)
      total_tax = 0

      TaxMethod.all.each do |tax_method|
        calculator = tax_method.klass.constantize
        if calculator.available?(tax_method, object)
          total_tax += calculator.rate(tax_method, object)
        end 
      end
      
      total_tax
    end

    def admin_label
      self.description
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
piggybak-0.1.1 app/models/piggybak/tax_method.rb
piggybak-0.1.0 app/models/piggybak/tax_method.rb