Class: Spree::Variant
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Base
page
#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
47
48
49
|
# File 'app/models/spree/variant.rb', line 47
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
|
.having_orders ⇒ Object
51
52
53
|
# File 'app/models/spree/variant.rb', line 51
def self.having_orders
joins(:line_items).distinct
end
|
Instance Method Details
#amount_in(currency) ⇒ Object
159
160
161
|
# File 'app/models/spree/variant.rb', line 159
def amount_in(currency)
price_in(currency).try(:amount)
end
|
#can_supply?(quantity = 1) ⇒ Boolean
#cost_price=(price) ⇒ Object
63
64
65
|
# File 'app/models/spree/variant.rb', line 63
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.
104
105
106
|
# File 'app/models/spree/variant.rb', line 104
def deleted?
!!deleted_at
end
|
#descriptive_name ⇒ Object
97
98
99
|
# File 'app/models/spree/variant.rb', line 97
def descriptive_name
is_master? ? name + ' - Master' : name + ' - ' + options_text
end
|
#exchange_name ⇒ Object
93
94
95
|
# File 'app/models/spree/variant.rb', line 93
def exchange_name
is_master? ? name : options_text
end
|
#in_stock? ⇒ Boolean
197
198
199
200
201
|
# File 'app/models/spree/variant.rb', line 197
def in_stock?
Rails.cache.fetch(in_stock_cache_key) do
total_on_hand > 0
end
end
|
#is_backorderable? ⇒ Boolean
#name_and_sku ⇒ Object
189
190
191
|
# File 'app/models/spree/variant.rb', line 189
def name_and_sku
"#{name} - #{sku}"
end
|
#on_backorder ⇒ Object
returns number of units currently on backorder for this variant.
72
73
74
|
# File 'app/models/spree/variant.rb', line 72
def on_backorder
inventory_units.with_state('backordered').size
end
|
#option_value(opt_name) ⇒ Object
151
152
153
|
# File 'app/models/spree/variant.rb', line 151
def option_value(opt_name)
self.option_values.detect { |o| o.option_type.name == opt_name }.try(:presentation)
end
|
#options=(options = {}) ⇒ Object
115
116
117
118
119
|
# File 'app/models/spree/variant.rb', line 115
def options=(options = {})
options.each do |option|
set_option_value(option[:name], option[:value])
end
end
|
#options_text ⇒ Object
80
81
82
83
84
85
86
87
88
89
90
|
# File 'app/models/spree/variant.rb', line 80
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
155
156
157
|
# File 'app/models/spree/variant.rb', line 155
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
176
177
178
179
180
181
182
183
184
185
186
187
|
# File 'app/models/spree/variant.rb', line 176
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
163
164
165
166
167
168
169
170
171
172
173
174
|
# File 'app/models/spree/variant.rb', line 163
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
|
#product ⇒ Object
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.
111
112
113
|
# File 'app/models/spree/variant.rb', line 111
def product
Spree::Product.unscoped { super }
end
|
#set_option_value(opt_name, opt_value) ⇒ Object
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
# File 'app/models/spree/variant.rb', line 121
def set_option_value(opt_name, opt_value)
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
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
213
214
215
|
# File 'app/models/spree/variant.rb', line 213
def should_track_inventory?
self.track_inventory? && Spree::Config.track_inventory_levels
end
|
#sku_and_options_text ⇒ Object
193
194
195
|
# File 'app/models/spree/variant.rb', line 193
def sku_and_options_text
"#{sku} #{options_text}".strip
end
|
#tax_category ⇒ Object
55
56
57
58
59
60
61
|
# File 'app/models/spree/variant.rb', line 55
def tax_category
if self[:tax_category_id].nil?
product.tax_category
else
TaxCategory.find(self[:tax_category_id])
end
end
|
#weight=(weight) ⇒ Object
67
68
69
|
# File 'app/models/spree/variant.rb', line 67
def weight=(weight)
self[:weight] = Spree::LocalizedNumber.parse(weight) if weight.present?
end
|