Module: Workarea::Storefront::NavigationHelper

Defined in:
app/helpers/workarea/storefront/navigation_helper.rb

Instance Method Summary collapse

Instance Method Details

#left_navigationObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/helpers/workarea/storefront/navigation_helper.rb', line 33

def left_navigation
  return '' unless @breadcrumbs.present?

  taxon = @breadcrumbs[-2] || @breadcrumbs.last
  return '' unless taxon.present?

  if taxon.has_children?
    selected_child_taxon = taxon.children.detect do |child|
      link_selected?(child)
    end
    render 'workarea/storefront/shared/left_navigation',
      taxon: taxon,
      selected_child_taxon: selected_child_taxon
  else
    ''
  end
end

Returns:

  • (Boolean)


29
30
31
# File 'app/helpers/workarea/storefront/navigation_helper.rb', line 29

def link_selected?(taxon)
  !!@breadcrumbs.try(:selected?, taxon)
end


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/helpers/workarea/storefront/navigation_helper.rb', line 51

def link_to_menu(menu)
  styles = 'primary-nav__link'
  styles << ' primary-nav__link--selected' if link_selected?(menu.taxon)
  options = {
    class: styles,
    data: {
      analytics: primary_navigation_analytics_data(menu.taxon).to_json
    }
  }

  if menu.taxon.placeholder?
     :span, menu.name, options
  else
    link_to menu.name, storefront_path_for(menu.taxon), options
  end
end

#mobile_nav_return_pathObject



19
20
21
22
23
24
25
26
27
# File 'app/helpers/workarea/storefront/navigation_helper.rb', line 19

def mobile_nav_return_path
  uri = URI.parse(
    params[:return_to].presence ||
    request.referer.presence ||
    root_url
  )

  uri.path
end


12
13
14
15
16
17
# File 'app/helpers/workarea/storefront/navigation_helper.rb', line 12

def navigation_menu_item_data_attribute(menu)
  return if menu.content.blank?
  return if ContentViewModel.new(menu.content).content_blocks.blank?

  { primary_nav_content: menu.id }
end


4
5
6
7
8
9
10
# File 'app/helpers/workarea/storefront/navigation_helper.rb', line 4

def navigation_menus
  @navigation_menus ||= Navigation::Menu
                          .all
                          .includes(:content)
                          .sort_by(&:position)
                          .select(&:active?)
end

#storefront_path_for(taxon) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'app/helpers/workarea/storefront/navigation_helper.rb', line 68

def storefront_path_for(taxon)
  if taxon.url?
    taxon.url
  elsif taxon.root?
    respond_to?(:storefront) ? storefront.root_path : root_path
  elsif taxon.search_results?
    search_path(taxon.navigable.params)
  elsif taxon.navigable?
    send("#{taxon.resource_name}_path", taxon.navigable_slug)
  end
end

#storefront_url_for(taxon) ⇒ Object



80
81
82
83
84
85
86
87
# File 'app/helpers/workarea/storefront/navigation_helper.rb', line 80

def storefront_url_for(taxon)
  path = storefront_path_for(taxon)

  return if path.blank?

  protocol = Rails.application.config.force_ssl ? 'https' : 'http'
  "#{protocol}://#{Workarea.config.host}/#{path.sub(/^\//, '')}"
end

#taxon_cache_key(taxon, section = nil, selected: nil) ⇒ Array

Generate a cache key for a taxon’s left navigation. Uses the ‘:selected` taxon in the cache key if a given node is selected, otherwise just appends the `section` argument onto the `taxon`’s cache key.

expand.

Parameters:

  • section (Symbol) (defaults to: nil)
    • Section of the page we are caching

  • [Workarea::Navigation::Taxon] (Hash)

    a customizable set of options

Returns:

  • (Array)

    Elements of a given cache key that Rails will



99
100
101
102
103
104
# File 'app/helpers/workarea/storefront/navigation_helper.rb', line 99

def taxon_cache_key(taxon, section = nil, selected: nil)
  selected_cache_key = "selected:#{selected.cache_key}" if selected.present?
  fragment = section&.to_s

  [taxon.cache_key, selected_cache_key, fragment]
end