Module: Bootstrap::NavHelper

Defined in:
app/helpers/bootstrap/nav_helper.rb

Overview

Rails view helpers for various Bootstrap navigation components.

See: twitter.github.io/bootstrap/components.html#navbar

Examples:

navigation bar (across top of page)

<%= nav_bar do %>      

  <%= brand('Span Brand')%>
  <%= brand('Link Brand', url: '#')%>

  <%= nav_bar_links do %>

    <%= nav_bar_link('Active', '#', active: true) %>
    <%= nav_bar_link('Link1', '/link1') %>

    <%= nav_bar_divider %>

    <%= nav_dropdown('Foo') do %>
      <%= dropdown_item('One', 'foo')%>
    <% end %>

  <% end %>
<% end %>

navigation list (e.g., in sidebar)

<%= nav_list(id: 'my') do %>
  <%= nav_list_header('Buttons & Labels') %>
  <%= dropdown_item('Buttons', 'butons')%>
  <%= dropdown_item('Labels', 'butons')%>
<% end %>

Instance Method Summary collapse

Instance Method Details

#brand(text, options = {}) ⇒ String

Returns a Bootstrap brand element

Parameters:

  • text (String)

    text of the brand

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

    except for :url, becomes html attributes of returned tag

Options Hash (options):

  • :url (String)

    if present, returned tag is an <a> (else <span>)

  • :with_environment (Boolean)

    if present, environment is appended to text and and a class of “rails-<Rails.env>” is added to the returned tag.

Returns:

  • (String)

    <a> if :url option present, else <span>



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

def brand(text, options = {})
  options = canonicalize_options(options)
  options = ensure_class(options, 'brand')

  with_environment = options.delete(:with_environment)
  if with_environment && Rails.env != 'production'
    text = "#{text} - #{Rails.env}"
    options = ensure_class(options, "rails-#{Rails.env}") 
  end

  url = options.delete(:url)
  
  if url.present?
    link_to(text, url, options)
  else
    (:span, text, options)
  end
end

Returns a Bootstrap navigation bar

Yields:

Yield Returns:

  • the contents of the navigation bar

Returns:

  • (String)


36
37
38
39
40
41
42
# File 'app/helpers/bootstrap/nav_helper.rb', line 36

def nav_bar()
  (:header, class: 'navbar') do
    (:nav, class: 'navbar-inner') do
      yield
    end
  end
end

Returns divider (vertical bar) for separating items in a nav_bar

Returns:

  • (String)

    <li class=“divider-vertical”></li>



109
110
111
# File 'app/helpers/bootstrap/nav_helper.rb', line 109

def nav_bar_divider
  (:li, nil, class: "divider-vertical")
end

Returns a nav_bar_link

Usually called within yield block of #nav_bar

Parameters:

  • text (String)

    text of link

  • url (String)

    url of link

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

    except for :active, becomes html attributes of link

Options Hash (options):

  • :active (true)

    if set to true, displays as inactive

Returns:

  • (String)

    returns <a> within <li>



95
96
97
98
99
100
101
102
103
104
# File 'app/helpers/bootstrap/nav_helper.rb', line 95

def nav_bar_link(text, url, options={})
  a_options = canonicalize_options(options)
  active = a_options.delete(:active)
  
  li_options = {class: ('active' if active)}
  
  (:li, li_options) do
    link_to(text, url, a_options)
  end
end

Returns <ul> for a group of nav bar links.

Usually called in yield block of #nav_bar

Yields:

  • block usually consists of calls to #nav_bar_link and #nav_bar_divider

Returns:

  • (String)

    <div class=‘nav’> containing results of yielded block



77
78
79
80
81
82
83
84
# File 'app/helpers/bootstrap/nav_helper.rb', line 77

def nav_bar_links(options={})
  options = canonicalize_options(options)
  options = ensure_class(options, 'nav')
  
  (:div, options) do
    yield
  end
end

Returns nav list

Parameters:

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

    becomes html attributes of enclosing <div>

Yields:

  • block consists of calls to #nav_list_header and #dropdown_item

Returns:

  • (String)

    <div class=‘well’><ul class=‘nav nav-list’> containg results of yielded block



118
119
120
121
122
123
124
125
126
# File 'app/helpers/bootstrap/nav_helper.rb', line 118

def nav_list(options={})
  options = canonicalize_options(options)
  options = ensure_class(options, 'well')
  (:div, options) do
    (:ul, class: 'nav nav-list') do
      yield
    end
  end
end

Returns header for nav_list

Parameters:

  • text (String)

    text of header

Returns:

  • (String)

    <li.nav-header>text</li>



132
133
134
# File 'app/helpers/bootstrap/nav_helper.rb', line 132

def nav_list_header(text)
  (:li, text, class: 'nav-header')
end