Module: Motr::Helpers::Navigation

Defined in:
lib/motr/helpers/navigation.rb

Overview

Convenience helpers for building navigation lists, and defining ‘on’ states.

Instance Method Summary collapse

Instance Method Details

Creates a link wrapped in a list item, to be used within a list-based navigation.

Examples:

Create a list item link to any page

nav_link_to('Home Page', '/home') #=> <li><a href="/home">Home Page</a>

‘Active state’ functionality for the current page

nav_link_to('Current Page', '/current_url') #=> <li class="on"><a href="/current_url" class="on">Current Page</a></li>

Parameters:

  • text (String)

    The text used within the link

  • path (String)

    The url used as the link’s href attribute

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

    Hash of attributes to be applied to the link. This format is the same as Rail’s link_to helper

  • wrapper (Symbol) (defaults to: :li)

    The html tag to be used as the wrapper. Defaults to :li

Options Hash (attrs):

  • :active_class (String)

    The class to use if this link is ‘active’. Defaults to “on”

  • :proc (Proc)

    A callback which, when called, determines the active state of the link

  • :matcher (Regex)

    A regular expression to be matched against path



27
28
29
30
31
32
33
34
# File 'lib/motr/helpers/navigation.rb', line 27

def nav_link_to(text, path, attrs = {}, wrapper = :li)

  link_attrs    = update_link_attrs(path, attrs)
  wrapper_attrs = link_attrs.delete(:wrapper)      
  child_link    = link_to(text, path, link_attrs)
  wrapper === false ? child_link : (wrapper, child_link, wrapper_attrs)
  
end

Creates a navigational list format, including a parent list / wrapper. Useful for nested list navigation

Examples:

Create a nested list navigation

navigation('Home Page', '/home') do
  nav_link_to('Sub Page', '/about')
end
<li><a href="/home">Home Page</a>
  <ol>
      <li><a href="/about">Sub Page</a></li>
  </ol>
</li>

Parameters:

  • text (String)

    The text used within the link

  • path (String)

    The url used as the link’s href attribute

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

    Hash of attributes to be applied to the link. This format is the same as Rail’s link_to helper

  • wrapper (Symbol) (defaults to: :li)

    The html tag to be used as the wrapper. Defaults to :li

  • container (Symbol) (defaults to: :ol)

    The element that will be used as a container for the nested list

  • &block (Block)

    A block containing the content of the nested items

Options Hash (attrs):

  • :active_class (String)

    The class to use if this link is ‘active’. Defaults to “on”

  • :proc (Proc)

    A callback which, when called, determines the active state of the link

  • :matcher (Regex)

    A regular expression to be matched against path



62
63
64
65
66
67
68
69
70
71
# File 'lib/motr/helpers/navigation.rb', line 62

def navigation(text, path, attrs = {}, wrapper = :li, container = :ol, &block)

  wrapper_attrs = attrs.delete(:wrapper)
  link_attrs    = update_link_attrs(path, attrs.merge(:wrapper => (attrs.delete(:item) || {}) ))
  parent_link   = nav_link_to(text, path, attrs, false)
  child_links   = (container, capture(&block), wrapper_attrs)        

  (wrapper, (parent_link << child_links), wrapper_attrs)
  
end