Class: Variant

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.additional_fieldsObject



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

def self.additional_fields
  @fields
end

.additional_fields=(new_fields) ⇒ Object



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

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)


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

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.

Returns:

  • (Boolean)


87
88
89
# File 'app/models/variant.rb', line 87

def deleted?
  deleted_at
end

#gross_profitObject



80
81
82
# File 'app/models/variant.rb', line 80

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”

Returns:

  • (Boolean)


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

def in_stock?
  Spree::Config[:track_inventory_levels] ? on_hand > 0 : true
end

#on_backorderObject

returns number of units currently on backorder for this variant.



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

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)



28
29
30
# File 'app/models/variant.rb', line 28

def on_hand
  Spree::Config[:track_inventory_levels] ? self.count_on_hand : nil
end

#on_hand=(new_level) ⇒ Object

Adjusts the inventory units to match the given new level.



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

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_textObject



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

def options_text
  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