Sha256: f84b9dd6b4e60600ad7b705949d39821dbde3592d53c1db59d637cb3019ca295

Contents?: true

Size: 1.63 KB

Versions: 3

Compression:

Stored size: 1.63 KB

Contents

class Product < ActiveRecord::Base
  has_many :product_option_types, :dependent => :destroy
  has_many :option_types, :through => :product_option_types
  has_many :variations, :dependent => :destroy
  belongs_to :category
  has_and_belongs_to_many :tax_treatments
  has_many :images, :as => :viewable, :dependent => :destroy
  has_one :sku, :as => :stockable, :dependent => :destroy
  validates_presence_of :name
  validates_presence_of :description
  validates_presence_of :price
  
  alias :selected_options :product_option_types
  
  # if product has a new category then we may need to delete tax_treatments associated with the  
  # previous category
  def before_update
    return if self.category.nil?
    ar_tax_treatments.clear unless self.category.tax_treatments.empty? 
  end
  
  def apply_tax_treatment?(id)
    return true if self.tax_treatments.any? {|tt| tt.id == id} 
    return self.category.tax_treatments.any? {|tt| tt.id == id} unless self.category.nil?
  end

  # Serious Ruby hacking going on here.  We alias the original method for the association as added by 
  # ActiveRecord and then override it so we can return the categories treatments if they are present.
  alias :ar_tax_treatments :tax_treatments
  def tax_treatments
    tt = ar_tax_treatments
    return tt unless tt.empty?
    return tt if self.category.nil?
    if self.category.tax_treatments.empty?
      # return empty array (does not need to be frozen since category has none)
      return tt
    else
      # return a frozen copy of the cateogires variations
      return Array.new(self.category.tax_treatments).freeze   
    end
  end  
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
spree-0.0.5 starter-app/vendor/plugins/spree/app/models/product.rb
spree-0.0.6 starter-app/vendor/plugins/spree/app/models/product.rb
spree-0.0.7 starter-app/vendor/plugins/spree/app/models/product.rb