Class: Workarea::Storefront::ProductViewModel
- Inherits:
-
ApplicationViewModel
- Object
- ApplicationViewModel
- Workarea::Storefront::ProductViewModel
show all
- Defined in:
- app/view_models/workarea/storefront/product_view_model.rb,
app/view_models/workarea/storefront/product_view_model/option.rb,
app/view_models/workarea/storefront/product_view_model/cache_key.rb,
app/view_models/workarea/storefront/product_view_model/option_set.rb,
app/view_models/workarea/storefront/product_view_model/sku_options.rb,
app/view_models/workarea/storefront/product_view_model/image_collection.rb
Defined Under Namespace
Classes: CacheKey, ImageCollection, Option, OptionSet, SkuOptions
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.wrap(model, options = {}) ⇒ Object
4
5
6
7
8
9
10
11
12
13
|
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 4
def self.wrap(model, options = {})
if model.is_a?(Enumerable)
model.map { |m| wrap(m, options) }
elsif Workarea.config.product_templates.include?(model.template.to_sym)
view_model_class = "Workarea::Storefront::ProductTemplates::#{model.template.camelize}ViewModel"
view_model_class.constantize.new(model, options)
else
new(model, options)
end
end
|
Instance Method Details
#breadcrumbs ⇒ Object
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 15
def breadcrumbs
@breadcrumbs ||=
if options[:via].present?
Navigation::Breadcrumbs.from_global_id(
options[:via],
last: model
)
else
Navigation::Breadcrumbs.new(
default_category,
last: model
)
end
end
|
#browse_link_options ⇒ Object
55
56
57
|
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 55
def browse_link_options
@browse_link_options ||= options.slice(:via).with_indifferent_access
end
|
#browser_title ⇒ Object
165
166
167
168
169
170
171
|
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 165
def browser_title
if model.browser_title.present?
model.browser_title
else
model.name
end
end
|
#cache_key ⇒ Object
39
40
41
42
43
44
|
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 39
def cache_key
@cache_key ||= CacheKey.new(
model,
options.merge(current_sku: current_sku)
).to_s
end
|
#catalog_id ⇒ Object
51
52
53
|
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 51
def catalog_id
options[:catalog_id]
end
|
#current_sku ⇒ Object
131
132
133
|
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 131
def current_sku
options[:sku]
end
|
#current_variant ⇒ Object
135
136
137
|
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 135
def current_variant
variants.detect { |variant| variant.sku == current_sku }
end
|
#default_category ⇒ Object
156
157
158
|
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 156
def default_category
@default_category ||= Categorization.new(model).default_model
end
|
#fulfillment_skus ⇒ Object
186
187
188
189
|
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 186
def fulfillment_skus
@fulfillment_skus ||=
Fulfillment::Sku.find_or_initialize_all(variants.map(&:sku))
end
|
#inventory ⇒ Object
143
144
145
146
|
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 143
def inventory
@inventory ||= options[:inventory] ||
Inventory::Collection.new(model.variants.map(&:sku))
end
|
#inventory_purchasable? ⇒ Boolean
152
153
154
|
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 152
def inventory_purchasable?
current_sku.blank? || inventory.for_sku(current_sku).purchasable?
end
|
#inventory_status ⇒ Object
148
149
150
|
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 148
def inventory_status
InventoryStatusViewModel.new(inventory.for_sku(current_sku)).message
end
|
173
174
175
176
177
178
179
|
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 173
def meta_description
if model.meta_description.present?
model.meta_description
else
model.description
end
end
|
#one_price? ⇒ Boolean
85
86
87
88
|
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 85
def one_price?
return false if sell_min_price.nil?
sell_min_price >= original_min_price
end
|
#original_max_price ⇒ Object
110
111
112
113
114
115
116
117
118
|
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 110
def original_max_price
return nil unless has_prices?
if pricing.msrp_max_price.present? && pricing.msrp_max_price > sell_max_price
pricing.msrp_max_price
else
pricing.regular_max_price
end
end
|
#original_min_price ⇒ Object
100
101
102
103
104
105
106
107
108
|
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 100
def original_min_price
return nil unless has_prices?
if pricing.msrp_min_price.present? && pricing.msrp_min_price > sell_min_price
pricing.msrp_min_price
else
pricing.regular_min_price
end
end
|
#pricing ⇒ Object
75
76
77
78
79
|
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 75
def pricing
@pricing ||= options[:pricing] || Pricing::Collection.new(
options[:sku].presence || variants.map(&:sku)
)
end
|
#primary_image ⇒ Object
30
31
32
|
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 30
def primary_image
images.primary
end
|
#purchasable? ⇒ Boolean
81
82
83
|
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 81
def purchasable?
has_prices? && model.purchasable? && inventory_purchasable?
end
|
Returns the set of recommendations for the product. The view model it returns behave like Enumerable.
64
65
66
|
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 64
def recommendations
@recommendations ||= DetailPageRecommendationsViewModel.new(model, options)
end
|
#requires_shipping? ⇒ Boolean
191
192
193
194
195
196
197
198
199
|
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 191
def requires_shipping?
if current_variant.present?
fulfillment_skus
.detect { |sku| sku.id == current_variant.sku }
.shipping?
else
fulfillment_skus.any?(&:shipping?)
end
end
|
#show_original_range? ⇒ Boolean
95
96
97
98
|
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 95
def show_original_range?
return false if original_min_price.nil? || original_max_price.nil?
original_min_price < original_max_price
end
|
#show_sell_range? ⇒ Boolean
90
91
92
93
|
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 90
def show_sell_range?
return false if sell_min_price.nil? || sell_max_price.nil?
sell_min_price < sell_max_price
end
|
#sku_options ⇒ Object
139
140
141
|
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 139
def sku_options
@sku_options ||= SkuOptions.new(variants).to_a
end
|
#variants ⇒ Object
125
126
127
128
129
|
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 125
def variants
@variants ||= model.variants.active.select do |variant|
!!inventory.for_sku(variant.sku).try(:displayable?)
end
end
|