Class: Spree::Variant

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.active(currency = nil) ⇒ Object



44
45
46
# File 'app/models/spree/variant.rb', line 44

def self.active(currency = nil)
  joins(:prices).where(deleted_at: nil).where('spree_prices.currency' => currency || Spree::Config[:currency]).where('spree_prices.amount IS NOT NULL')
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

#amount_in(currency) ⇒ Object



121
122
123
# File 'app/models/spree/variant.rb', line 121

def amount_in(currency)
  price_in(currency).try(:amount)
end

#cost_price=(price) ⇒ Object



48
49
50
# File 'app/models/spree/variant.rb', line 48

def cost_price=(price)
  self[:cost_price] = parse_price(price) if price.present?
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)


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

def deleted?
  deleted_at
end

#gross_profitObject



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

def gross_profit
  cost_price.nil? ? 0 : (price - cost_price)
end

#has_default_price?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'app/models/spree/variant.rb', line 113

def has_default_price?
  !self.default_price.nil?
end

#in_stock?(quantity = 1) ⇒ Boolean

Returns:

  • (Boolean)


136
137
138
# File 'app/models/spree/variant.rb', line 136

def in_stock?(quantity=1)
  Spree::Stock::Quantifier.new(self).can_supply?(quantity)
end

#name_and_skuObject



125
126
127
# File 'app/models/spree/variant.rb', line 125

def name_and_sku
  "#{name} - #{sku}"
end

#on_backorderObject

returns number of units currently on backorder for this variant.



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

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

#option_value(opt_name) ⇒ Object



109
110
111
# File 'app/models/spree/variant.rb', line 109

def option_value(opt_name)
  self.option_values.detect { |o| o.option_type.name == opt_name }.try(:presentation)
end

#options_textObject



57
58
59
60
61
62
63
64
65
# File 'app/models/spree/variant.rb', line 57

def options_text
  values = self.option_values.joins(:option_type).order("#{Spree::OptionType.table_name}.position asc")

  values.map! do |ov|
    "#{ov.option_type.presentation}: #{ov.presentation}"
  end

  values.to_sentence({ words_connector: ", ", two_words_connector: ", " })
end

#price_in(currency) ⇒ Object



117
118
119
# File 'app/models/spree/variant.rb', line 117

def price_in(currency)
  prices.select{ |price| price.currency == currency }.first || Spree::Price.new(variant_id: self.id, currency: currency)
end

#productObject

Product may be created with deleted_at already set, which would make AR’s default finder return nil. This is a stopgap for that little problem.



132
133
134
# File 'app/models/spree/variant.rb', line 132

def product
  Spree::Product.unscoped { super }
end

#set_option_value(opt_name, opt_value) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/models/spree/variant.rb', line 78

def set_option_value(opt_name, opt_value)
  # no option values on master
  return if self.is_master

  option_type = Spree::OptionType.where(name: opt_name).first_or_initialize do |o|
    o.presentation = opt_name
    o.save!
  end

  current_value = self.option_values.detect { |o| o.option_type.name == opt_name }

  unless current_value.nil?
    return if current_value.name == opt_value
    self.option_values.delete(current_value)
  else
    # then we have to check to make sure that the product has the option type
    unless self.product.option_types.include? option_type
      self.product.option_types << option_type
      self.product.save
    end
  end

  option_value = Spree::OptionValue.where(option_type_id: option_type.id, name: opt_value).first_or_initialize do |o|
    o.presentation = opt_value
    o.save!
  end

  self.option_values << option_value
  self.save
end

#total_on_handObject



140
141
142
# File 'app/models/spree/variant.rb', line 140

def total_on_hand
  Spree::Stock::Quantifier.new(self).total_on_hand
end