Sha256: 61cf53465ef548387b8241b7dfd4ab92992d0b7664de62827fefc40d841e1b62
Contents?: true
Size: 1.42 KB
Versions: 9
Compression:
Stored size: 1.42 KB
Contents
module KktShoppe class Product < ActiveRecord::Base # Validations validate { errors.add :base, :can_belong_to_root if self.parent && self.parent.parent } # Variants of the product has_many :variants, :class_name => 'KktShoppe::Product', :foreign_key => 'parent_id', :dependent => :destroy # The parent product (only applies to variants) belongs_to :parent, :class_name => 'KktShoppe::Product', :foreign_key => 'parent_id' # All products which are not variants scope :root, -> { where(:parent_id => nil) } # If a variant is created, the base product should be updated so that it doesn't have stock control enabled after_save do if self.parent self.parent.price = 0 self.parent.cost_price = 0 self.parent.tax_rate = nil self.parent.weight = 0 self.parent.stock_control = false self.parent.save if self.parent.changed? end end # Does this product have any variants? # # @return [Boolean] def has_variants? !variants.empty? end # Returns the default variant for the product or nil if none exists. # # @return [KktShoppe::Product] def default_variant return nil if self.parent @default_variant ||= self.variants.select { |v| v.default? }.first end # Is this product a variant of another? # # @return [Boolean] def variant? !self.parent_id.blank? end end end
Version data entries
9 entries across 9 versions & 1 rubygems