Module: BootstrapHelper

Defined in:
app/helpers/bootstrap_helper.rb

Defined Under Namespace

Classes: TabBuilder

Instance Method Summary collapse

Instance Method Details

#bootstrap_btn_group(data = nil, &block) ⇒ Object

Render a bootstrap button group

data

The data content in the button group. If it is an emuratable object, this method

will yield the block with each item in the collection.



89
90
91
92
93
94
95
96
97
# File 'app/helpers/bootstrap_helper.rb', line 89

def bootstrap_btn_group data=nil, &block
  (:div, :class=>"btn-group") do
    if data.respond_to? :each
      data.each(&block)
    else
      yield data
    end
  end
end

#bootstrap_dropdown_button(content, opts = {}) ⇒ Object

Render a bootstrap dropdown button. The yielded block should render each dropdown item as a <li>

content

The html content on the button

opts

The dropdown button class, default is “btn”



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

def bootstrap_dropdown_button content, opts={}
  btn_class = (opts||{}).delete(:class) || "btn"
  (:div, :class=>"btn-group") do
    concat link_to(%{#{content} #{ :span, "", :class=>"caret"}}.html_safe, "#", :class=>"#{btn_class} dropdown-toggle", :"data-toggle"=>"dropdown")
    concat (:ul, :class=>"dropdown-menu"){yield if block_given?}
  end
end

#bootstrap_dropdown_menu(menu, cls = nil) ⇒ Object

Render a dropdown menu <li> markup in the bootstrap nav bar It will yield to a block that will output the menu items as html <li> tag

menu

The text on the menu



64
65
66
67
68
69
70
# File 'app/helpers/bootstrap_helper.rb', line 64

def bootstrap_dropdown_menu menu, cls=nil
  opts = {:class=>["dropdown",cls].compact.join(" ")}
  (:li, opts) do
    concat link_to(%{#{menu} #{ :b, "", :class=>"caret"}}.html_safe, "#", :class=>"dropdow-toggle", :"data-toggle"=>"dropdown")
    concat (:ul, :class=>"dropdown-menu"){yield}
  end
end

Render standard fav link icons for different devices. It normally embeded in the <header>



102
103
104
# File 'app/helpers/bootstrap_helper.rb', line 102

def bootstrap_fav_links
  render "components/bootstrap/fav_links"
end

#bootstrap_form_fields(form, model, &block) ⇒ Object



16
17
18
# File 'app/helpers/bootstrap_helper.rb', line 16

def bootstrap_form_fields form, model, &block
  BootstrapFormBuilder.render_items(form, model, self, &block)
end

#bootstrap_modal(options) ⇒ Object

Render a bootstrap modal panel. It will yield to a block that should ouput the modal-body content

options

Dialog id

options

Dialog title, default “Title”

options

Html markups for modal-footer section, default is a Cancel button

options

If presented, the modal div will have ‘data-remote-path’ attribute set to this value



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/helpers/bootstrap_helper.rb', line 113

def bootstrap_modal options
  id = options.delete(:id)
  remote_path = options.delete(:remote_path)
  title=options.delete(:title) || "Title"
  actions = options.delete(:actions) || [link_to("Cancel","#", :class=>"btn ", :"data-dismiss"=>"modal", :"aria-hidden"=>"true")]
  cls = options.delete(:class) || ""

  modal_options = {:class=>"modal hide fade #{cls}"}
  modal_options[:id] = id if id.present?
  modal_options[:"data-remote-path"] = remote_path if remote_path.present?

  (:div, modal_options) do
    concat((:div, :class=>"modal-header"){
      concat (:button,"&times;".html_safe, :type=>"button", :class=>"close", :"data-dismiss"=>"modal", :"aria-hidden"=>"true")
      concat (:h3, title)
    })
    concat((:div, :class=>"modal-body"){yield if block_given?})
    concat (:div, actions.join("\n").html_safe, :class=>"modal-footer")
  end
end

#bootstrap_navbar(opts = {}, &block) ⇒ Object

Render a standard fixed-top bootstrap navbar It will yield to a block that will output nav menus as <li> markup. #bootstrap_dropdown_menu will output a menu markup.

opts

The html markups for brand section

opts

The html markups for the pull-right section



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/helpers/bootstrap_helper.rb', line 41

def bootstrap_navbar opts={}, &block
  brand = opts.delete :brand || ""
  right = opts.delete :right || ""
  
  (:div, :class=>"navbar navbar-fixed-top") do
    (:div, :class=>"navbar-inner") do
      (:div, :class=>"container-fluid") do
        concat(nav_collapse.html_safe)
        concat(link_to brand.html_safe, "#", :class=>"brand")
        nav = (:div, :class=>"container-fluid nav-collapse") do
          concat((:ul, :class=>"nav", &block))
          concat((:ul, right, :class=>"nav pull-right"))
        end
        concat(nav)
      end
    end
  end
end

#bootstrap_sidebar(opts = {}) ⇒ Object

Render a standard bootstrap sidebar panel with nav-stacked, nav-pills content

opts

Options that will be passed to the yieled block



23
24
25
26
27
28
29
30
31
32
# File 'app/helpers/bootstrap_helper.rb', line 23

def bootstrap_sidebar opts={}
  (:div, :class=>"section sidebar-nav") do
    nav_class = opts.delete(:nav_class) || "nav-pills nav-stacked"
    (:ul, :class=>"nav #{nav_class}") do
      header = opts.delete(:header)
      concat( :li, header.html_safe, :class=>"nav-header") if header.present?
      yield(opts) if block_given?
    end
  end
end

#bootstrap_tab_pane(opts = {}) {|tab_builder| ... } ⇒ Object

Render a bootstrap tab pane Example code: <tt>

bootstrap_tabs(tab_pos: "tabs-top") do |t|
  t.tab "Tab1", :id=>"id_of_tab1" do
    ...
  end
  t.tab "Tab2", :id=>"id_of_tab2" do
    ...
  end
end

</tt>

Parameters for the tab panel

opts

The bootstrap class name for the tab position, like “tab-top”. Default value is “tab-top”

Parameters for each tab

name

The tab name

opts

The tab id, default value is the tab name

Yields:

  • (tab_builder)


156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'app/helpers/bootstrap_helper.rb', line 156

def bootstrap_tab_pane opts={}
  tab_builder = TabBuilder.new
  yield(tab_builder) if block_given?

  tab_pos = opts.delete(:tab_pos) || "tabs-top"
  tab_options = tab_builder.tabs.collect do |item|
    opts = item[:opts] || {}
    name = item[:name]
    id = opts.delete(:id) || name
    {:id=>id, :link=>link_to(name, "##{id}", :"data-toggle"=>"tab"), :content_proc=>item[:proc], :opts=>opts}
  end

   :div, :class=>"tabbable #{tab_pos}" do
    concat((:ul, :class=>"nav nav-tabs"){
      tab_options.each_with_index do |item, index|
        cls = "active" if index==0
        concat (:li, item[:link], :class=>cls)
      end
    })

    concat((:div, :class=>"tab-content"){
      tab_options.each_with_index do |item, index|
        opts = {:id=>item[:id], :class=>"tab-pane #{'active' if index==0}"}.merge(item[:opts])
        concat((:div, opts){
          proc = item[:content_proc] 
          proc.call if proc
        })
      end
    })

  end
end