Class: Workarea::Storefront::ProductViewModel

Inherits:
ApplicationViewModel
  • Object
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
14
15
# 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
rescue NameError
  new(model, options)
end

Instance Method Details



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 17

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


57
58
59
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 57

def browse_link_options
  @browse_link_options ||= options.slice(:via).with_indifferent_access
end

#browser_titleObject

Detail



167
168
169
170
171
172
173
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 167

def browser_title
  if model.browser_title.present?
    model.browser_title
  else
    model.name
  end
end

#cache_keyObject



41
42
43
44
45
46
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 41

def cache_key
  @cache_key ||= CacheKey.new(
    model,
    options.merge(current_sku: current_sku)
  ).to_s
end

#catalog_idObject

Browsing



53
54
55
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 53

def catalog_id
  options[:catalog_id]
end

#current_skuObject



133
134
135
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 133

def current_sku
  options[:sku]
end

#current_variantObject



137
138
139
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 137

def current_variant
  variants.detect { |variant| variant.sku == current_sku }
end

#default_categoryObject



158
159
160
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 158

def default_category
  @default_category ||= Categorization.new(model).default_model
end

#fulfillment_skusObject

Fulfillment



188
189
190
191
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 188

def fulfillment_skus
  @fulfillment_skus ||=
    Fulfillment::Sku.find_or_initialize_all(variants.map(&:sku))
end

#imagesObject



36
37
38
39
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 36

def images
  @images_collection ||=
    Storefront::ProductViewModel::ImageCollection.new(model, options)
end

#inventoryObject



145
146
147
148
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 145

def inventory
  @inventory ||= options[:inventory] ||
    Inventory::Collection.new(model.variants.map(&:sku))
end

#inventory_purchasable?Boolean

Returns:

  • (Boolean)


154
155
156
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 154

def inventory_purchasable?
  current_sku.blank? || inventory.for_sku(current_sku).purchasable?
end

#inventory_statusObject



150
151
152
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 150

def inventory_status
  InventoryStatusViewModel.new(inventory.for_sku(current_sku)).message
end

#meta_descriptionObject



175
176
177
178
179
180
181
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 175

def meta_description
  if model.meta_description.present?
    model.meta_description
  else
    model.description
  end
end

#one_price?Boolean

Returns:

  • (Boolean)


87
88
89
90
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 87

def one_price?
  return false if sell_min_price.nil?
  sell_min_price >= original_min_price
end

#original_max_priceObject



112
113
114
115
116
117
118
119
120
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 112

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_priceObject



102
103
104
105
106
107
108
109
110
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 102

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

#pricingObject



77
78
79
80
81
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 77

def pricing
  @pricing ||= options[:pricing] || Pricing::Collection.new(
    options[:sku].presence || variants.map(&:sku)
  )
end

#primary_imageObject



32
33
34
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 32

def primary_image
  images.primary
end

#purchasable?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 83

def purchasable?
  has_prices? && model.purchasable? && inventory_purchasable?
end

#recommendationsWorkarea::Storefront::DetailPageRecommendationsViewModel

Returns the set of recommendations for the product. The view model it returns behave like Enumerable.



66
67
68
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 66

def recommendations
  @recommendations ||= DetailPageRecommendationsViewModel.new(model, options)
end

#requires_shipping?Boolean

Returns:

  • (Boolean)


193
194
195
196
197
198
199
200
201
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 193

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

Returns:

  • (Boolean)


97
98
99
100
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 97

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

Returns:

  • (Boolean)


92
93
94
95
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 92

def show_sell_range?
  return false if sell_min_price.nil? || sell_max_price.nil?
  sell_min_price < sell_max_price
end

#sku_optionsObject



141
142
143
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 141

def sku_options
  @sku_options ||= SkuOptions.new(variants).to_a
end

#variantsObject

Variants



127
128
129
130
131
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 127

def variants
  @variants ||= model.variants.active.select do |variant|
    !!inventory.for_sku(variant.sku).try(:displayable?)
  end
end