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



5
6
7
8
9
10
11
12
13
14
# File 'app/view_models/workarea/storefront/product_view_model.rb', line 5

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



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

def breadcrumbs
  @breadcrumbs ||=
    if options[:via].present?
      Navigation::Breadcrumbs.from_global_id(
        options[:via],
        last: model.name
      )
    else
      Navigation::Breadcrumbs.new(
        default_category,
        last: model.name
      )
    end
end


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

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

#browser_titleObject

Detail



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

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

#cache_keyObject



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

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

#catalog_idObject

Browsing



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

def catalog_id
  options[:catalog_id]
end

#current_skuObject



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

def current_sku
  options[:sku]
end

#current_variantObject



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

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

#default_categoryObject



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

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

#imagesObject



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

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

#inventoryObject



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

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

#inventory_purchasable?Boolean

Returns:

  • (Boolean)


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

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

#inventory_statusObject



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

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

#meta_descriptionObject



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

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

#one_price?Boolean

Returns:

  • (Boolean)


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

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

#original_max_priceObject



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

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



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

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



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

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

#primary_imageObject



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

def primary_image
  images.primary
end

#purchasable?Boolean

Returns:

  • (Boolean)


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

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.



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

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

#show_original_range?Boolean

Returns:

  • (Boolean)


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

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)


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

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

#sku_optionsObject



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

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

#variantsObject

Variants



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

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