Sha256: eafb37c5678fcfd51b0179b57a0a488f2742ffc6678576435f236568ee96f754

Contents?: true

Size: 1.46 KB

Versions: 5

Compression:

Stored size: 1.46 KB

Contents

module Shoppe
  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, -> { order(:default => :desc, :name => :asc) }, :class_name => 'Shoppe::Product', :foreign_key => 'parent_id', :dependent => :destroy

    # The parent product (only applies to variants)
    belongs_to :parent, :class_name => 'Shoppe::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 [Shoppe::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

5 entries across 5 versions & 2 rubygems

Version Path
shoppe-1.0.7 app/models/shoppe/product/variants.rb
shoppe-1.0.6 app/models/shoppe/product/variants.rb
shoppe-paypal-1.1.0 vendor/bundle/ruby/2.1.0/gems/shoppe-1.0.5/app/models/shoppe/product/variants.rb
shoppe-1.0.5 app/models/shoppe/product/variants.rb
shoppe-1.0.3 app/models/shoppe/product/variants.rb