Class: Product
- Includes:
- ProductScopes
- Defined in:
- app/models/product.rb
Overview
PRODUCTS Products represent an entity for sale in a store.
Products can have variations, called variants Products properties include description, permalink, availability,
shipping category, etc. that do not change by variant.
MASTER VARIANT Every product has one master variant, which stores master price and sku, size and weight, etc. The master variant does not have option values associated with it. Price, SKU, size, weight, etc. are all delegated to the master variant. Contains on_hand inventory levels only when there are no variants for the product.
VARIANTS All variants can access the product properties directly (via reverse delegation). Inventory units are tied to Variant. The master variant can have inventory units, but not option values. All other variants have option values and may have inventory units. Sum of on_hand each variant’s inventory level determine “on_hand” level for the product.
Instance Attribute Summary collapse
-
#prototype_id ⇒ Object
Adding properties and option types on creation based on a chosen prototype.
Instance Method Summary collapse
- #add_properties_and_option_types_from_prototype ⇒ Object
-
#has_stock? ⇒ Boolean
Returns true if there are inventory units (any variant) with “on_hand” state for this product.
-
#has_variants? ⇒ Boolean
returns true if the product has any variants (the master variant is not a member of the variants array).
-
#master_price ⇒ Object
———————————————————————————————————-.
- #master_price=(value) ⇒ Object
-
#on_hand ⇒ Object
returns the number of inventory units “on_hand” for this product.
-
#on_hand=(new_level) ⇒ Object
adjusts the “on_hand” inventory level for the product up or down to match the given new_level.
-
#to_param ⇒ Object
———————————————————————————————————- end deprecation region ———————————————————————————————————-.
- #variant ⇒ Object
- #variants? ⇒ Boolean
Instance Attribute Details
#prototype_id ⇒ Object
Adding properties and option types on creation based on a chosen prototype
122 123 124 |
# File 'app/models/product.rb', line 122 def prototype_id @prototype_id end |
Instance Method Details
#add_properties_and_option_types_from_prototype ⇒ Object
127 128 129 130 131 132 133 134 |
# File 'app/models/product.rb', line 127 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 true if there are inventory units (any variant) with “on_hand” state for this product
117 118 119 |
# File 'app/models/product.rb', line 117 def has_stock? master.in_stock? || !!variants.detect{|v| v.in_stock?} end |
#has_variants? ⇒ Boolean
returns true if the product has any variants (the master variant is not a member of the variants array)
101 102 103 |
# File 'app/models/product.rb', line 101 def has_variants? !variants.empty? end |
#master_price ⇒ Object
The following methods are deprecated and will be removed in a future version of Spree
71 72 73 74 |
# File 'app/models/product.rb', line 71 def master_price warn "[DEPRECATION] `Product.master_price` is deprecated. Please use `Product.price` instead. (called from #{caller[0]}" self.price end |
#master_price=(value) ⇒ Object
76 77 78 79 |
# File 'app/models/product.rb', line 76 def master_price=(value) warn "[DEPRECATION] `Product.master_price=` is deprecated. Please use `Product.price=` instead. (called from #{caller[0]}" self.price = value end |
#on_hand ⇒ Object
returns the number of inventory units “on_hand” for this product
106 107 108 |
# File 'app/models/product.rb', line 106 def on_hand has_variants? ? variants.inject(0){|sum, v| sum + v.on_hand} : master.on_hand end |
#on_hand=(new_level) ⇒ Object
adjusts the “on_hand” inventory level for the product up or down to match the given new_level
111 112 113 114 |
# File 'app/models/product.rb', line 111 def on_hand=(new_level) raise "cannot set on_hand of product with variants" if has_variants? master.on_hand = new_level end |
#to_param ⇒ Object
end deprecation region
95 96 97 98 |
# File 'app/models/product.rb', line 95 def to_param return permalink unless permalink.blank? name.to_url end |
#variant ⇒ Object
86 87 88 89 |
# File 'app/models/product.rb', line 86 def variant warn "[DEPRECATION] `Product.variant` is deprecated. Please use `Product.master` instead. (called from #{caller[0]})" self.master end |
#variants? ⇒ Boolean
81 82 83 84 |
# File 'app/models/product.rb', line 81 def variants? warn "[DEPRECATION] `Product.variants?` is deprecated. Please use `Product.has_variants?` instead. (called from #{caller[0]})" self.has_variants? end |