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)

Instance Method Summary collapse

Methods inherited from Base

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

Instance Method Details

#amount_in(currency) ⇒ Object



182
183
184
# File 'app/models/spree/variant.rb', line 182

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

#available?Boolean

Returns:

  • (Boolean)


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

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)


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

def deleted?
  !!deleted_at
end

#descriptive_nameObject



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

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

#dimensionObject



244
245
246
# File 'app/models/spree/variant.rb', line 244

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

#discontinue!Object



248
249
250
# File 'app/models/spree/variant.rb', line 248

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

#discontinued?Boolean

Returns:

  • (Boolean)


252
253
254
# File 'app/models/spree/variant.rb', line 252

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

#exchange_nameObject

Default to master name



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

def exchange_name
  is_master? ? name : options_text
end

#in_stock?Boolean

Returns:

  • (Boolean)


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

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

#name_and_skuObject



212
213
214
# File 'app/models/spree/variant.rb', line 212

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

#option_value(opt_name) ⇒ Object



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

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

#options=(options = {}) ⇒ Object



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

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

#options_textObject



103
104
105
106
107
108
109
110
111
112
113
# File 'app/models/spree/variant.rb', line 103

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



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

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

#price_modifier_amount(options = {}) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
# File 'app/models/spree/variant.rb', line 199

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

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

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



186
187
188
189
190
191
192
193
194
195
196
197
# File 'app/models/spree/variant.rb', line 186

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

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



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

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

#set_option_value(opt_name, opt_value) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'app/models/spree/variant.rb', line 144

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

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

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

  if current_value.nil?
    # then we have to check to make sure that the product has the option type
    unless product.option_types.include? option_type
      product.option_types << option_type
    end
  else
    return if current_value.name == opt_value
    option_values.delete(current_value)
  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

  option_values << option_value
  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)


232
233
234
# File 'app/models/spree/variant.rb', line 232

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

#sku_and_options_textObject



216
217
218
# File 'app/models/spree/variant.rb', line 216

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

#tax_categoryObject



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

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

#track_inventoryObject



236
237
238
# File 'app/models/spree/variant.rb', line 236

def track_inventory
  should_track_inventory?
end

#volumeObject



240
241
242
# File 'app/models/spree/variant.rb', line 240

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