Class: Spree::Variant
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Spree::Variant
- Defined in:
- app/models/spree/variant.rb,
app/models/spree/variant/scopes.rb
Class Method Summary collapse
- .additional_fields ⇒ Object
- .additional_fields=(new_fields) ⇒ Object
-
.has_option(option_type, *option_values) ⇒ Object
(also: has_options)
Returns variants that match a given option value.
Instance Method Summary collapse
-
#available? ⇒ Boolean
returns true if this variant is allowed to be placed on a new order.
-
#deleted? ⇒ Boolean
use deleted? rather than checking the attribute directly.
- #gross_profit ⇒ Object
-
#in_stock? ⇒ Boolean
(also: #in_stock)
returns true if at least one inventory unit of this variant is “on_hand”.
-
#on_backorder ⇒ Object
returns number of units currently on backorder for this variant.
-
#on_hand ⇒ Object
Returns number of inventory units for this variant (new records haven’t been saved to database, yet).
-
#on_hand=(new_level) ⇒ Object
Adjusts the inventory units to match the given new level.
- #options_text ⇒ Object
Class Method Details
.additional_fields ⇒ Object
64 65 66 |
# File 'app/models/spree/variant.rb', line 64 def self.additional_fields @fields end |
.additional_fields=(new_fields) ⇒ Object
68 69 70 |
# File 'app/models/spree/variant.rb', line 68 def self.additional_fields=(new_fields) @fields = new_fields end |
.has_option(option_type, *option_values) ⇒ Object Also known as: has_options
Returns variants that match a given option value
Example:
product.variants_including_master.has_option(OptionType.find_by_name(“shoe-size”),OptionValue.find_by_name(“8”))
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'app/models/spree/variant/scopes.rb', line 12 def has_option(option_type, *option_values) option_types = OptionType.table_name option_type_conditions = case option_type when OptionType then { "#{option_types}.name" => option_type.name } when String then { "#{option_types}.name" => option_type } else { "#{option_types}.id" => option_type } end relation = joins(:option_values => :option_type).where(option_type_conditions) option_values_conditions = option_values.each do |option_value| option_value_conditions = case option_value when OptionValue then { "#{OptionValue.table_name}.name" => option_value.name } when String then { "#{OptionValue.table_name}.name" => option_value } else { "#{OptionValue.table_name}.id" => option_value } end relation = relation.where(option_value_conditions) end relation end |
Instance Method Details
#available? ⇒ Boolean
returns true if this variant is allowed to be placed on a new order
73 74 75 |
# File 'app/models/spree/variant.rb', line 73 def available? Spree::Config[:track_inventory_levels] ? (Spree::Config[:allow_backorders] || self.in_stock?) : true end |
#deleted? ⇒ Boolean
use deleted? rather than checking the attribute directly. this allows extensions to override deleted? if they want to provide their own definition.
88 89 90 |
# File 'app/models/spree/variant.rb', line 88 def deleted? deleted_at end |
#gross_profit ⇒ Object
81 82 83 |
# File 'app/models/spree/variant.rb', line 81 def gross_profit self.cost_price.nil? ? 0 : (self.price - self.cost_price) end |
#in_stock? ⇒ Boolean Also known as: in_stock
returns true if at least one inventory unit of this variant is “on_hand”
59 60 61 |
# File 'app/models/spree/variant.rb', line 59 def in_stock? Spree::Config[:track_inventory_levels] ? on_hand > 0 : true end |
#on_backorder ⇒ Object
returns number of units currently on backorder for this variant.
54 55 56 |
# File 'app/models/spree/variant.rb', line 54 def on_backorder inventory_units.with_state('backordered').size end |
#on_hand ⇒ Object
Returns number of inventory units for this variant (new records haven’t been saved to database, yet)
29 30 31 |
# File 'app/models/spree/variant.rb', line 29 def on_hand Spree::Config[:track_inventory_levels] ? self.count_on_hand : (1.0/0) # Infinity end |
#on_hand=(new_level) ⇒ Object
Adjusts the inventory units to match the given new level.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'app/models/spree/variant.rb', line 34 def on_hand=(new_level) if Spree::Config[:track_inventory_levels] new_level = new_level.to_i # increase Inventory when if new_level > on_hand # fill backordered orders before creating new units inventory_units.with_state('backordered').slice(0, new_level).each do |iu| iu.fill_backorder new_level -= 1 end end self.count_on_hand = new_level else raise 'Cannot set on_hand value when Spree::Config[:track_inventory_levels] is false' end end |
#options_text ⇒ Object
77 78 79 |
# File 'app/models/spree/variant.rb', line 77 def self.option_values.sort { |ov1, ov2| ov1.option_type.position <=> ov2.option_type.position }.map { |ov| "#{ov.option_type.presentation}: #{ov.presentation}" }.to_sentence({ :words_connector => ", ", :two_words_connector => ", " }) end |