Module: NavigationHelper

Included in:
PagesHelper
Defined in:
app/helpers/navigation_helper.rb

Overview

This module provices helpers for rendering common navigation from page structure

primary_navigation
# =>  <ul id="primary_navigation">
# =>    <li class="first active nav-home"><a href="/">Home page</a></li>
# =>    <li class="nav-about"><a href="/about">About us</a></li>
# =>    <li class="last nav-blog"><a href="/blog">Blog</a></li>
# =>  </ul>

Instance Method Summary collapse

Instance Method Details

Link to a page, using the page name as the link name.

link_to Page.find_by_path('/parent')
# => <a href="/parent">Parent</a>

Parameters:

  • page (Page)

    Page to link to

Returns:

  • (String)


139
140
141
# File 'app/helpers/navigation_helper.rb', line 139

def link_to_page(page)
  link_to(page.name, page.navigation_path)
end

Use the page structure to render nested navigation, each visible page is rendered in a li element with child pages in a nested ul

The first li in a ul has the class first, the last li in a ul has the class last and the current page has the class active.

Each li has a class identifier generated from it’s path and prefixed with nav, for example a page with path /parent/child would have the class nav-parent-child. This identifier is a class is to avoid duplicate ids if multiple navigation elements are used on a page.

The home page is assumed to be at the same level as it’s children.

navigation depth: 4, page: Page.home, id: 'navigation'
# =>  <ul id="navigation">
# =>    <li class="first nav-home"><a href="/">Home page</a></li>
# =>    <li class="nav-about"><a href="/about">About us</a></li>
# =>    <li class="active nav-parent">
# =>      <a href="/parent">Parent</a>
# =>      <ul>
# =>        <li class="first nav-parent-child">
# =>          <a href="/parent/child">Child</a>
# =>          <ul>
# =>            <li class="first last nav-parent-child-grand-child"><a href="/parent/child/grand-child">Grand child</a></li>
# =>          </ul>
# =>        </li>
# =>        <li class="nav-parent-sibling"><a href="/parent/sibling">Sibling</a></li>
# =>        <li class="last nav-parent-youngest"><a href="/parent/youngest">Youngest</a></li>
# =>      </ul>
# =>    </li>
# =>    <li class="nav-aunt"><a href="/aunt">Aunt</a></li>
# =>    <li class="nav-uncle"><a href="/uncle">Uncle</a></li>
# =>    <li class="last nav-blog"><a href="/blog">Blog</a></li>
# =>  </ul>

navigation depth: 1, page: Page.home
# =>  <ul>
# =>    <li class="first nav-home"><a href="/">Home page</a></li>
# =>    <li class="nav-about"><a href="/about">About us</a></li>
# =>    <li class="active nav-parent"><a href="/parent">Parent</a></li>
# =>    <li class="nav-aunt"><a href="/aunt">Aunt</a></li>
# =>    <li class="nav-uncle"><a href="/uncle">Uncle</a></li>
# =>    <li class="last nav-blog"><a href="/blog">Blog</a></li>
# =>  </ul>

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :depth (Integer) — default: 1

    Number of levels to render

  • :page (Integer) — default: current_page

    Page to start rendering

  • :id (Integer)

    Id of surrounding UL

  • :class (Integer)

    Class of surrounding UL

Returns:

  • (String)


122
123
124
125
126
127
128
129
# File 'app/helpers/navigation_helper.rb', line 122

def navigation(options = {})
  options.reverse_merge!( page: @page, depth: 1 )
  page = options[:page]
  children = page.home? ? page.navigable_children : page.parent.navigable_children
  html = (is_home_and_shown_in_nav?(page) ? nav_link(Page.home) : '').html_safe
  html << list_items(children, options[:depth])
  (:ul, html, options.slice(:id, :class))
end

#primary_navigation(id = 'primary_navigation') ⇒ String

primary_navigation

# =>  <ul id="primary_navigation">
# =>    <li class="first active nav-home"><a href="/">Home page</a></li>
# =>    <li class="nav-about"><a href="/about">About us</a></li>
# =>    <li class="nav-parent"><a href="/parent">Parent</a></li>
# =>    <li class="nav-aunt"><a href="/aunt">Aunt</a></li>
# =>    <li class="nav-uncle"><a href="/uncle">Uncle</a></li>
# =>    <li class="last nav-blog"><a href="/blog">Blog</a></li>
# =>  </ul>

Parameters:

  • id (String) (defaults to: 'primary_navigation')

    Dom id of the surronding ul element

Returns:

  • (String)


25
26
27
28
29
30
31
32
33
# File 'app/helpers/navigation_helper.rb', line 25

def primary_navigation(id = 'primary_navigation')
  cache 'page/navigation/' + id + '/' + @page.id.to_s do
    benchmark 'Rendered primary_navigation' do
      nav = navigation(page: Page.home, depth: 1, id: id)
      safe_concat nav
      ''
    end
  end
end

#secondary_navigation(options = {}) ⇒ String

Render the secondary navigation if the current page isn’t already rendered in the primary navigation.

secondary_navigaiton :depth => 10
# =>  <ul id="secondary_navigation">
# =>    <li class="first nav-parent-child"><a href="/parent/child">Child</a>
# =>      <ul>
# =>        <li class="first last nav-parent-child-grand-child"><a href="/parent/child/grand-child">Grand child</a></li>
# =>      </ul>
# =>    </li>
# =>    <li class="nav-parent-sibling"><a href="/parent/sibling">Sibling</a></li>
# =>    <li class="last nav-parent-youngest"><a href="/parent/youngest">Youngest</a></li>
# =>  </ul>

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :depth (Integer) — default: 1

    Number of levels to render

  • :id (Integer) — default: 'secondary_navigation'

    Id of surrounding UL

Returns:

  • (String)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/helpers/navigation_helper.rb', line 54

def secondary_navigation(options = {})
  cache 'page/navigation/secondary/' + @page.id.to_s do
    benchmark 'Rendered secondary_navigation' do
      secondary_ancestors = (@page.ancestors[-2] || @page).navigable_children
      nav = if secondary_ancestors.empty? || @page.home?
              ''
            else
              page = secondary_ancestors.first
              navigation(options.reverse_merge(page: page, id: 'secondary_navigation'))
            end
      safe_concat nav
      ''
    end
  end
end