Module: Bootstrap::NavHelper

Includes:
ActionView::Helpers::SanitizeHelper
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('Link 1', '/link1') %>
    <%= nav_bar_divider %>
    <%= nav_bar_link('Link 2', '/link2') %>
  <% end >

  <%= nav_dropdown(pull: 'right') %>
    <%= nav_dropdown('Dropdown 1') 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>



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

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

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

  url = options.delete(:url)
  (:div, class: "navbar-header") do 
    if url.present?
      link_to(text, url, options)
    else
      (:span, text, options)
    end
  end
end

Returns a Bootstrap navigation bar

Yields:

Yield Returns:

  • the contents of the navigation bar

Returns:

  • (String)


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

def nav_bar()
  (:nav, class: 'navbar navbar-default') do
    (:div, class: 'container-fluid') do
      yield
    end
  end
end

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

Returns:

  • (String)

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



127
128
129
# File 'app/helpers/bootstrap/nav_helper.rb', line 127

def nav_bar_divider
  (:li, nil, class: "divider")
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>



113
114
115
116
117
118
119
120
121
122
# File 'app/helpers/bootstrap/nav_helper.rb', line 113

def nav_bar_link(text, url, options={})
  a_options = canonicalize_options(options)
  active = a_options.delete(:active)
  
  li_options = ensure_class({class: ('active' if active)}, "")

  (:li, li_options) do
    link_to(text, url, a_options)
  end
end

Usually called in yield block of #nav_bar

Parameters:

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

    options except for :pull become html attributes of generated <div>

Options Hash (options):

  • pull (:left, :right)

    will add class of “pull-left|right” for Bootstrap nav bar positioning

Yields:

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

Returns:

  • (String)

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



91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/helpers/bootstrap/nav_helper.rb', line 91

def nav_bar_links(options = {})
  options = canonicalize_options(options)
  options = ensure_class(options, 'nav navbar-nav')
  
  if pull = options.delete(:pull)
    options = ensure_class(options, "navbar-#{pull}")
  end
  
  (:ul, options) do
    yield
  end
end

Wraps text so it has proper leading and color for text in a nav bar. Usually in a nav_bar_links block

Note that the Bootstrap CSS has no horizontal margins or padding. This method pads with three &nbsp; at front and back. Use pad: false to suppress padding.

Parameters:

  • text (String)

    text to display

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

    unknown options become html attributes for the <p>

Options Hash (options):

  • :pull (:left, :right) — default: :left

    adds the Boostrap positioning class

  • :pad (Boolean) — default: true

    include padding around text

Returns:

  • (String)


165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'app/helpers/bootstrap/nav_helper.rb', line 165

def nav_bar_text(text, options = {})
  options = canonicalize_options(options)

  unless options.delete(:pad) == false
    padding = ("&nbsp;" * 3).html_safe
    text = padding + sanitize(text) + padding
  end
  
  pull_class = (options.delete(:pull).to_s == 'right' ? 'pull-right' : 'pull-left')
  options = ensure_class(options, [pull_class, 'navbar-text'])
  
  (:p, text, options)
end

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



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

def nav_bar_wrapper(options = {})
  (:div, class: "navbar-collapse collapse sidebar-navbar-collapse") 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



136
137
138
139
140
141
142
143
144
# File 'app/helpers/bootstrap/nav_helper.rb', line 136

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

Returns header for nav_list

Parameters:

  • text (String)

    text of header

Returns:

  • (String)

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



150
151
152
# File 'app/helpers/bootstrap/nav_helper.rb', line 150

def nav_list_header(text)
  (:li, text, class: '')
end

#vertical_nav(options = {}) ⇒ Object



179
180
181
182
183
# File 'app/helpers/bootstrap/nav_helper.rb', line 179

def vertical_nav(options = {})
  (:div, class: "sidebar-nav") do 
    yield
  end
end