Class: Spree::Variant

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

Constant Summary collapse

LOCALIZED_NUMBERS =
%w(cost_price weight depth width height)

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

page, spree_base_scopes

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

.having_ordersObject



92
93
94
# File 'app/models/spree/variant.rb', line 92

def self.having_orders
  joins(:line_items).distinct
end

Instance Method Details

#amount_in(currency) ⇒ Object



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

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

#available?Boolean

Returns:

  • (Boolean)


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

def available?
  !discontinued? && product.available?
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)


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

def deleted?
  !!deleted_at
end

#descriptive_nameObject



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

def descriptive_name
  is_master? ? name + ' - Master' : name + ' - ' + options_text
end

#dimensionObject



250
251
252
# File 'app/models/spree/variant.rb', line 250

def dimension
  (width || 0) + (height || 0) + (depth || 0)
end

#discontinue!Object



254
255
256
# File 'app/models/spree/variant.rb', line 254

def discontinue!
  update_attribute(:discontinue_on, Time.current)
end

#discontinued?Boolean

Returns:

  • (Boolean)


258
259
260
# File 'app/models/spree/variant.rb', line 258

def discontinued?
  !!discontinue_on && discontinue_on <= Time.current
end

#exchange_nameObject

Default to master name



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

def exchange_name
  is_master? ? name : options_text
end

#in_stock?Boolean

Returns:

  • (Boolean)


226
227
228
229
230
# File 'app/models/spree/variant.rb', line 226

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

#name_and_skuObject



218
219
220
# File 'app/models/spree/variant.rb', line 218

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

#on_backorderObject

returns number of units currently on backorder for this variant.



105
106
107
# File 'app/models/spree/variant.rb', line 105

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

#option_value(opt_name) ⇒ Object



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

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

#options=(options = {}) ⇒ Object



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

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

#options_textObject



109
110
111
112
113
114
115
116
117
118
119
# File 'app/models/spree/variant.rb', line 109

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



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

def price_in(currency)
  prices.detect { |price| price.currency == currency } || prices.build(currency: currency)
end

#price_modifier_amount(options = {}) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
# File 'app/models/spree/variant.rb', line 205

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

  options.keys.map { |key|
    m = "#{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



192
193
194
195
196
197
198
199
200
201
202
203
# File 'app/models/spree/variant.rb', line 192

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.



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

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

#set_option_value(opt_name, opt_value) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'app/models/spree/variant.rb', line 150

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

Returns:

  • (Boolean)


238
239
240
# File 'app/models/spree/variant.rb', line 238

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

#sku_and_options_textObject



222
223
224
# File 'app/models/spree/variant.rb', line 222

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

#tax_categoryObject



96
97
98
99
100
101
102
# File 'app/models/spree/variant.rb', line 96

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

#track_inventoryObject



242
243
244
# File 'app/models/spree/variant.rb', line 242

def track_inventory
  self.should_track_inventory?
end

#volumeObject



246
247
248
# File 'app/models/spree/variant.rb', line 246

def volume
  (width || 0) * (height || 0) * (depth || 0)
end