Module: Raw::NavigationHelper
- Defined in:
- lib/raw/helper/navigation.rb
Overview
A helper mixin for programmatically building Navigation Menus through easy to style XHTML. The code resulting from these methods is in line with that found in most examples at listamatic, thus you can use prebuilt CSS and Javascript to create horizontal or vertical menus. Basically it generates something like
<div id="navcontainer">
<ul id="navlist">
<li id="active"> <a href="/foo" id="current"> Current Page </a></li>
<li> <a href="/bar"> Other page </a> </li>
</ul>
</div>
This helper takes care of setting of putting the special CSS identifiers for the current controller automatically. You could override menuitem_active_on(path) to change the behaviour that choose the active item, for example to keep the item “Wiki” active both for /wiki/pageone and /wiki/pagetwo
Example of horizontal bar at listamatic: css.maxdesign.com.au/listamatic/horizontal26.htm Vertical example css.maxdesign.com.au/listamatic/vertical09.htm
NOTE: No tests were made with Publishable objects which are not subclass of Nitro::Controller, but it should work.
Constant Summary collapse
- TEMPLATE =
<<Eof <div id="navcontainer"> <ul id="navlist"> LIST </ul> </div> Eof
Instance Method Summary collapse
-
#menu_for(*controllers) ⇒ Object
Takes a list of controllers and builds a menu using #mount_path as the uri and the controller name as text.
-
#menu_from_hash(hash) ⇒ Object
The argument must be an hash of pairs for menu item’, no control will be applied on these values, they will be used directly.
- #menuitem_active_on?(path) ⇒ Boolean
Instance Method Details
#menu_for(*controllers) ⇒ Object
Takes a list of controllers and builds a menu using #mount_path as the uri and the controller name as text. An eventual “Controller” suffix will be stripped, so i.e. for controllers named HomeController
, Pages
, FeedCtl
it will use Home
, Page
, FeedCtl
.
For more finegrained control you can pass a block to this function, each controller will be passed to it and the result will be used as the text for the menu item.
Otherwise you can specify pairs of path/text using #navigation_for_hash
53 54 55 56 57 58 59 |
# File 'lib/raw/helper/navigation.rb', line 53 def (*controllers) #:yields: hash= {} controllers.each do |c| hash[c.mount_path] = block_given? ? yield(c) : c.name.gsub(/Controller/,'') end (hash) end |
#menu_from_hash(hash) ⇒ Object
The argument must be an hash of pairs for menu item’, no control will be applied on these values, they will be used directly. You can use the method like
'/foo/bar'=>'Page One', '/foo/baz'=>'Page Two'
The method takes care of setting the CSS values as expected.
To avoid specifying everything the method #navigation_menu can be used.
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/raw/helper/navigation.rb', line 70 def (hash) list=hash.map do |path,name| if (path) %{<li id="active"><a href="#{path}" id="current"> #{name} </a></li>} else %{<li><a href="#{path}"> #{name} </a></li>} end end.join("\n") TEMPLATE.gsub("LIST",list) end |
#menuitem_active_on?(path) ⇒ Boolean
81 82 83 |
# File 'lib/raw/helper/navigation.rb', line 81 def (path) path == request.path end |