Module: NavigationHelper
- Included in:
- PagesHelper
- Defined in:
- app/helpers/navigation_helper.rb
Overview
This module provices helpers for rendering common navigation from page structure
# => <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
-
#link_to_page(page) ⇒ String
Link to a
page, using the page name as the link name. -
#navigation(options = {}) ⇒ String
Use the page structure to render nested navigation, each visible page is rendered in a
lielement with child pages in a nestedul. -
#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>.
-
#secondary_navigation(options = {}) ⇒ String
Render the secondary navigation if the current page isn’t already rendered in the primary navigation.
Instance Method Details
#link_to_page(page) ⇒ String
Link to a page, using the page name as the link name.
link_to Page.find_by_path('/parent')
# => <a href="/parent">Parent</a>
139 140 141 |
# File 'app/helpers/navigation_helper.rb', line 139 def link_to_page(page) link_to(page.name, page.) end |
#navigation(options = {}) ⇒ String
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.
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>
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>
122 123 124 125 126 127 128 129 |
# File 'app/helpers/navigation_helper.rb', line 122 def ( = {}) .reverse_merge!( page: @page, depth: 1 ) page = [: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, [:depth]) content_tag(:ul, html, .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>
25 26 27 28 29 30 31 32 33 |
# File 'app/helpers/navigation_helper.rb', line 25 def (id = 'primary_navigation') cache 'page/navigation/' + id + '/' + @page.id.to_s do benchmark 'Rendered primary_navigation' do nav = (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>
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'app/helpers/navigation_helper.rb', line 54 def ( = {}) 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 (.reverse_merge(page: page, id: 'secondary_navigation')) end safe_concat nav '' end end end |