Class: Spree::Variant

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

page

Methods included from Preferences::Preferable

#clear_preferences, #default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference

Class Method Details

.active(currency = nil) ⇒ Object


46
47
48
# File 'app/models/spree/variant.rb', line 46

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


145
146
147
# File 'app/models/spree/variant.rb', line 145

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

#can_supply?(quantity = 1) ⇒ Boolean


189
190
191
# File 'app/models/spree/variant.rb', line 189

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

#cost_price=(price) ⇒ Object


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

def cost_price=(price)
  self[:cost_price] = Spree::LocalizedNumber.parse(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.


90
91
92
# File 'app/models/spree/variant.rb', line 90

def deleted?
  !!deleted_at
end

#in_stock?Boolean


183
184
185
186
187
# File 'app/models/spree/variant.rb', line 183

def in_stock?
  Rails.cache.fetch(in_stock_cache_key) do
    total_on_hand > 0
  end
end

#is_backorderable?Boolean


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

def is_backorderable?
  Spree::Stock::Quantifier.new(self).backorderable?
end

#name_and_skuObject


175
176
177
# File 'app/models/spree/variant.rb', line 175

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

#on_backorderObject

returns number of units currently on backorder for this variant.


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

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

#option_value(opt_name) ⇒ Object


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

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

#options=(options = {}) ⇒ Object


101
102
103
104
105
# File 'app/models/spree/variant.rb', line 101

def options=(options = {})
  options.each do |option|
    set_option_value(option[:name], option[:value])
  end
end

#options_textObject


75
76
77
78
79
80
81
82
83
84
85
# File 'app/models/spree/variant.rb', line 75

def options_text
  values = self.option_values.sort do |a, b|
    a.option_type.position <=> b.option_type.position
  end

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

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

#price_in(currency) ⇒ Object


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

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

#price_modifier_amount(options = {}) ⇒ Object


162
163
164
165
166
167
168
169
170
171
172
173
# File 'app/models/spree/variant.rb', line 162

def price_modifier_amount(options = {})
  return 0 unless options.present?

  options.keys.map { |key|
    m = "#{options[key]}_price_modifier_amount".to_sym
    if self.respond_to? m
      self.send(m, options[key])
    else
      0
    end
  }.sum
end

#price_modifier_amount_in(currency, options = {}) ⇒ Object


149
150
151
152
153
154
155
156
157
158
159
160
# File 'app/models/spree/variant.rb', line 149

def price_modifier_amount_in(currency, options = {})
  return 0 unless options.present?

  options.keys.map { |key|
    m = "#{key}_price_modifier_amount_in".to_sym
    if self.respond_to? m
      self.send(m, currency, options[key])
    else
      0
    end
  }.sum
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.


97
98
99
# File 'app/models/spree/variant.rb', line 97

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

#set_option_value(opt_name, opt_value) ⇒ Object


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'app/models/spree/variant.rb', line 107

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

#should_track_inventory?Boolean

Shortcut method to determine if inventory tracking is enabled for this variant This considers both variant tracking flag and site-wide inventory tracking settings


199
200
201
# File 'app/models/spree/variant.rb', line 199

def should_track_inventory?
  self.track_inventory? && Spree::Config.track_inventory_levels
end

#sku_and_options_textObject


179
180
181
# File 'app/models/spree/variant.rb', line 179

def sku_and_options_text
  "#{sku} #{options_text}".strip
end

#tax_categoryObject


50
51
52
53
54
55
56
# File 'app/models/spree/variant.rb', line 50

def tax_category
  if self[:tax_category_id].nil?
    product.tax_category
  else
    TaxCategory.find(self[:tax_category_id])
  end
end

#total_on_handObject


193
194
195
# File 'app/models/spree/variant.rb', line 193

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

#weight=(weight) ⇒ Object


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

def weight=(weight)
  self[:weight] = Spree::LocalizedNumber.parse(weight) if weight.present?
end