Module: RailsConnector::MicronavHelper

Includes:
DisplayHelper
Included in:
DefaultCmsHelper
Defined in:
app/helpers/rails_connector/micronav_helper.rb

Overview

This module contains a helper that can be used to build a micronavigation.

Instance Method Summary collapse

Methods included from DisplayHelper

#display_field, #display_value

Instance Method Details

#micronav(obj, options = {}) ⇒ Object

Generates a micronavigation by producing HTML markup. obj becomes the rightmost Obj in the micronavigation since it is the one for which the micronavigation is built. Assume that all the ancestors of this obj are available as an array starting with the root object at index 1. The following options exist:

:start

index of the ancestor object to start with. Default is 1, i.e. the object Obj::root.

:micronav_id

ID of the micronavigation. Default is ‘micronav’.

All ancestors of obj are linked to the respective objects. obj has no linkage. The first and the last <li> tags have apropriate CSS classes.

For example, assume that you have the following object hierarchy:

  • Root

    • Ancestor_1

      • Ancestor_2

        • Current_Object

Normal usage with the context Obj set to @obj:

<%= micronav(@obj) %>

The helper will start with the Root and will generate the follwing HTML if @obj is set to Current_Object:

<ul id='micronav'>
  <li class='first'>
    <a href='/2001/Root'>
      Root
    </a>
  </li>
  <li>
    <a href='/2011/Ancestor_1'>
      Ancestor_1
    </a>
  </li>
  <li>
    <a href='/2012/Ancestor_2'>
      Ancestor_2
    </a>
  </li>
  <li class='last'>
    <span>
      Current_Object
    </span>
  </li>
</ul>

If the :start option is set to 2

<%= micronav(@obj, :start => 2) %>

then the helper will start with Ancestor_1 and will generate:

<ul id='micronav'>
  <li class='first'>
    <a href='/2011/Ancestor_1'>
      Ancestor_1
    </a>
  </li>
  <li>
    <a href='/2012/Ancestor_2'>
      Ancestor_2
    </a>
  </li>
  <li class='last'>
    <span>
      Current_Object
    </span>
  </li>
</ul>

If you specify the :micronav_id option as in

<%= micronav(@obj, :micronav_id => '<em><b>micronav_below_banner</b></em>') %>

then the <ul> tag of the micronavigation will get your custom ID:

<ul id='<em><b>micronav_below_banner</b></em>'></tt>
  ...
</ul>


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/helpers/rails_connector/micronav_helper.rb', line 89

def micronav(obj, options = {})
  options.reverse_merge!({:start => 1, :micronav_id => 'micronav'})
  ancestors = obj.ancestors
  start = options[:start] - 1
  start = 0 if start < 0
  ancestors = ancestors[start..-1]
  ancestors ||= []
  li_tags = "".html_safe
  ancestors.each do |ancestor|
    tag_options = {}
    tag_options[:class] = "first" if li_tags.empty?
    li_tags << (:li, link_to(display_value(ancestor.display_title), cms_path(ancestor)), tag_options)
  end
  li_tags << (:li, (:span, display_value(obj.display_title)), :class => 'last')
  (:ul, li_tags, :id => options[:micronav_id])
end