Class: Workarea::Storefront::CategoryViewModel

Inherits:
ApplicationViewModel
  • Object
show all
Includes:
DisplayContent, ProductBrowsing
Defined in:
app/view_models/workarea/storefront/category_view_model.rb

Instance Method Summary collapse

Methods included from ProductBrowsing

#facets, #filters, #has_filters?, #product_browse_cache_key

Methods included from Pagination

#first_page?, #last_page?, #next_page, #prev_page, #second_page?, #total_pages

Methods included from DisplayContent

#content, #content_blocks, #content_blocks_for, #meta_description, #open_graph_asset

Instance Method Details



7
8
9
# File 'app/view_models/workarea/storefront/category_view_model.rb', line 7

def breadcrumbs
  @breadcrumbs ||= Navigation::Breadcrumbs.new(model)
end

#browser_titleObject



15
16
17
18
19
20
21
# File 'app/view_models/workarea/storefront/category_view_model.rb', line 15

def browser_title
  if content.browser_title.present?
    content.browser_title
  else
    breadcrumbs.join(' - ')
  end
end

#cache_keyObject



11
12
13
# File 'app/view_models/workarea/storefront/category_view_model.rb', line 11

def cache_key
  @cache_key ||= "#{model.cache_key}/#{product_browse_cache_key}"
end

#product_breadcrumb_paramsObject



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

def product_breadcrumb_params
  { via: breadcrumbs.to_global_id }
end

#productsObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/view_models/workarea/storefront/category_view_model.rb', line 40

def products
  return @products if defined?(@products)

  view_models = search_query.results.map do |result|
    ProductViewModel.wrap(
      result[:model],
      result
        .merge(options)
        .merge(product_breadcrumb_params)
        .merge(facets: terms_facets)
    )
  end

  @products = PagedArray.from(view_models, page, per_page, total)
end

#search_queryObject

Products



28
29
30
31
32
33
34
35
36
37
38
# File 'app/view_models/workarea/storefront/category_view_model.rb', line 28

def search_query
  @search_query ||= Search::CategoryBrowse.new(
    options.except(:per_page).merge(
      category_ids: [model.id],
      rules: model.product_rules.usable,
      terms_facets: terms_facets - %w(category),
      range_facets: range_facets,
      sort: current_sorts
    )
  )
end

#sortObject

Sorting



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

def sort
  current_sorts.first
end

#sortsObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/view_models/workarea/storefront/category_view_model.rb', line 69

def sorts
  @sorts ||=
    begin
      tmp = Search::CategoryBrowse.available_sorts.reject do |sort|
        sort.slug == :relevance
      end

      tmp.map! { |s| [s.name, s.slug] }

      if model.featured_products?
        tmp.unshift(
          [I18n.t('workarea.storefront.products.featured'), :featured]
        )
      end

      tmp
    end
end