Class: Variant

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.additional_fieldsObject



62
63
64
# File 'app/models/variant.rb', line 62

def self.additional_fields
  @fields
end

.additional_fields=(new_fields) ⇒ Object



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

def self.additional_fields=(new_fields)
  @fields = new_fields
end

Instance Method Details

#available?Boolean

returns true if this variant is allowed to be placed on a new order

Returns:

  • (Boolean)


71
72
73
# File 'app/models/variant.rb', line 71

def available?
  self.in_stock? || Spree::Config[:allow_backorders]
end

#in_stock?Boolean Also known as: in_stock

returns true if at least one inventory unit of this variant is “on_hand”

Returns:

  • (Boolean)


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

def in_stock?
  on_hand > 0
end

#on_backorderObject

returns number of units currently on backorder for this variant.



52
53
54
# File 'app/models/variant.rb', line 52

def on_backorder
  inventory_units.with_state("backordered").size
end

#on_handObject

Returns number of inventory units for this variant (new records haven’t been saved to database, yet)



23
24
25
# File 'app/models/variant.rb', line 23

def on_hand
  new_record? ? inventory_units.size : inventory_units.with_state("on_hand").size
end

#on_hand=(new_level) ⇒ Object

Adjusts the inventory units to match the given new level.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/models/variant.rb', line 28

def on_hand=(new_level)
  delta_units = new_level.to_i - on_hand

  # decrease inventory
  if delta_units < 0
    inventory_units.with_state("on_hand").slice(0, delta_units.abs).each{|iu| iu.destroy}

  # otherwise, increase Inventory when positive delta
  elsif delta_units > 0

    # fill backordered orders before creating new units
    inventory_units.with_state("backordered").slice(0, delta_units).each do |iu|
      iu.fill_backorder
      delta_units -= 1
    end

    # create new units
    (delta_units).times do
      new_record? ? inventory_units.build(:state => 'on_hand') : inventory_units.create(:state => 'on_hand') 
    end
  end      
end

#options_textObject



75
76
77
# File 'app/models/variant.rb', line 75

def options_text
	self.option_values.map { |ov| "#{ov.option_type.presentation}: #{ov.presentation}" }.to_sentence({:words_connector => ", ", :two_words_connector => ", "})
end