Class: Product

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/product.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#prototype_idObject

Adding properties and option types on creation based on a chosen prototype



81
82
83
# File 'app/models/product.rb', line 81

def prototype_id
  @prototype_id
end

Instance Method Details

#add_properties_and_option_types_from_prototypeObject



87
88
89
90
91
92
93
94
# File 'app/models/product.rb', line 87

def add_properties_and_option_types_from_prototype
  if prototype_id and prototype = Prototype.find_by_id(prototype_id)
    prototype.properties.each do |property|
      product_properties.create(:property => property)
    end
    self.option_types = prototype.option_types
  end
end

#has_stock?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'app/models/product.rb', line 74

def has_stock?
  variants.inject(false){ |tf, v| tf ||= v.in_stock }
end

#on_handObject

Pseduo Attribute. Products don’t really have inventory - variants do. We want to make the variant stuff transparent in the simple cases, however, so we pretend like we’re setting the inventory of the product when in fact, we’re really changing the inventory of the so-called “empty variant.”



57
58
59
# File 'app/models/product.rb', line 57

def on_hand
  variant.on_hand
end

#on_hand=(quantity) ⇒ Object



61
62
63
# File 'app/models/product.rb', line 61

def on_hand=(quantity)
  @quantity = quantity
end

#skuObject

Pseduo attribute for SKU, similiar as on_hand above.



66
67
68
# File 'app/models/product.rb', line 66

def sku
  variant.sku if variant
end

#sku=(sku) ⇒ Object



70
71
72
# File 'app/models/product.rb', line 70

def sku=(sku)
  variant.sku = sku if variant
end

#to_paramObject



35
36
37
38
# File 'app/models/product.rb', line 35

def to_param       
  return permalink unless permalink.blank?
  name.parameterize.to_s
end

#variantObject

special method that returns the single empty variant (but only if there are no meaningful variants)



49
50
51
52
# File 'app/models/product.rb', line 49

def variant
  return nil if variants?
  variants.first
end

#variants?Boolean

checks is there are any meaningful variants (ie. variants with at least one option value)

Returns:

  • (Boolean)


41
42
43
44
45
46
# File 'app/models/product.rb', line 41

def variants?
  self.variants.each do |v|
    return true unless v.option_values.empty?
  end
  false
end