Module: TwitterBootstrapHelper
- Defined in:
- lib/twitter_bootstrap_helper.rb,
lib/twitter_bootstrap_helper/railtie.rb,
lib/twitter_bootstrap_helper/version.rb,
lib/twitter_bootstrap_helper/helpers/twitter_bootstrap_helper.rb
Defined Under Namespace
Classes: Railtie
Constant Summary collapse
- VERSION =
"0.0.6"
Instance Method Summary collapse
-
#tb_badge(name, options = {}) ⇒ Object
Creates a badge containing the content “name” ==== Options *
:class
- Defaults to “badge”, any other class specified will be appended to the “badge” class. -
#tb_button(name, options = {}) ⇒ Object
Creates a “button” tag.
- #tb_dropdown_toggle(name, options = {}) ⇒ Object
-
#tb_icon(icon, options = {}) ⇒ Object
Creates an “i” tag with the given
icon
class. -
#tb_label(name, options = {}) ⇒ Object
Creates a label containing the content “name” ==== Options *
:class
- Defaults to “label”, any other class specified will be appended to the “label” class. -
#tb_link(name, link = "#", options = nil) ⇒ Object
This is a wrapper for the link_to helper ==== Options *
:icon
- Optionally display an icon before the name. -
#tb_modal(modal_id, content_or_options_with_block = nil, options = nil, &block) ⇒ Object
Creates a Twitter Bootstrap modal with the given
modal_id
==== Options *:title
- The title at the top of the modal *:fade
- To specify whether the modal should animate in. -
#tb_modal_button(name, modal_id, options = nil) ⇒ Object
Creates a button with the given
name
that will active a Twitter Bootstrap Modal with the id ofmodal_id
. -
#tb_nav(nav_type = nil, content_or_options_with_block = {}, options = {}, &block) ⇒ Object
Nav Helpers.
- #tb_nav_item(content_or_options_with_block = nil, options = nil, &block) ⇒ Object
- #tb_nav_items(items) ⇒ Object
-
#tb_sidebar_link(name, link = "#", options = {}) ⇒ Object
Creates an “li” tag formatted for a sidebar.
Instance Method Details
#tb_badge(name, options = {}) ⇒ Object
Creates a badge containing the content “name”
Options
-
:class
- Defaults to “badge”, any other class specified will be appended to the “badge” class.
65 66 67 68 69 70 71 |
# File 'lib/twitter_bootstrap_helper/helpers/twitter_bootstrap_helper.rb', line 65 def tb_badge(name, = {}) ||= {} [:class] = "badge #{[:class]}" content_tag(:span, name, ) end |
#tb_button(name, options = {}) ⇒ Object
Creates a “button” tag. The class defaults to “btn” and the type defaults to :submit.
Options
-
:icon
- Optionally display an icon on the button. -
:class
- Specify a class for the button. Defaults to “btn”. -
:type
- Defaults to submit. -
All other options are passed through to the the content_tag helper.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/twitter_bootstrap_helper/helpers/twitter_bootstrap_helper.rb', line 41 def (name, = {}) = { class: "btn", icon: nil, type: :submit, }.merge( || {}) icon = nil if [:icon] icon = [:icon] .delete(:icon) end = [].tap do |b| b << tb_icon(icon) b << name end.join.html_safe content_tag(:button, , ) end |
#tb_dropdown_toggle(name, options = {}) ⇒ Object
190 191 192 193 194 |
# File 'lib/twitter_bootstrap_helper/helpers/twitter_bootstrap_helper.rb', line 190 def tb_dropdown_toggle(name, = {}) ["data-toggle"] = "dropdown" [:class] = "dropdown-toggle #{[:class]}" tb_link "#{name} <b class=\"caret\"></b>".html_safe, "#", end |
#tb_icon(icon, options = {}) ⇒ Object
Creates an “i” tag with the given icon
class.
-
This also works with Font Awesome
5 6 7 8 9 10 11 12 |
# File 'lib/twitter_bootstrap_helper/helpers/twitter_bootstrap_helper.rb', line 5 def tb_icon(icon, = {}) = { class: "" }.merge() style = "color: #{[:color]}" if [:color] content_tag(:i, "", :class => "#{icon} #{[:class]}", :style => style) unless icon.nil? end |
#tb_label(name, options = {}) ⇒ Object
Creates a label containing the content “name”
Options
-
:class
- Defaults to “label”, any other class specified will be appended to the “label” class.
76 77 78 79 80 81 82 |
# File 'lib/twitter_bootstrap_helper/helpers/twitter_bootstrap_helper.rb', line 76 def tb_label(name, = {}) ||= {} [:class] = "label #{[:class]}" content_tag(:span, name, ) end |
#tb_link(name, link = "#", options = nil) ⇒ Object
This is a wrapper for the link_to helper
Options
-
:icon
- Optionally display an icon before the name. -
All other options are passed through the the link_to helper.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/twitter_bootstrap_helper/helpers/twitter_bootstrap_helper.rb', line 18 def tb_link(name, link="#", = nil) ||= {} icon = nil if [:icon] icon = [:icon] .delete(:icon) end label = [].tap do |b| b << tb_icon(icon) unless icon.nil? b << name end.join.html_safe link_to(label, link, ) end |
#tb_modal(modal_id, content_or_options_with_block = nil, options = nil, &block) ⇒ Object
Creates a Twitter Bootstrap modal with the given modal_id
Options
-
:title
- The title at the top of the modal -
:fade
- To specify whether the modal should animate in. Default: true -
:cancel_label
- The name of the cancel button. Default: Cancel -
:cancel_class
- The class of the cancel button. Default: “btn” -
:ok_id
- The id of the ok button. Useful for javascript actions -
:ok_label
- The label of the OK button. Default: OK -
:ok_class
- The class of the OK button. Default: “btn btn-primary” -
:ok_link
- The link for the ok button. Default: “#” -
:remote
- (untested) The remote link for the model to get it’s content
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/twitter_bootstrap_helper/helpers/twitter_bootstrap_helper.rb', line 221 def tb_modal(modal_id, = nil, = nil, &block) if block_given? = if .is_a?(Hash) content = capture(&block) else content = end = { title: "Alert", fade: true, cancel_label: "Cancel", cancel_class: "btn", ok_id: nil, ok_label: "OK", ok_class: "btn btn-primary", ok_link: "#", remote: nil, }.merge( || {}) content_tag :div, :id => modal_id, :class => "modal hide #{ [:fade] ? "fade" : ""}", "data-remote" => [:remote] do [].tap do |modal| modal << content_tag(:div, :class => "modal-header") do [].tap do |header| header << content_tag(:button, "×".html_safe, :class => "close", "data-dismiss" => "modal") header << content_tag(:h3, [:title]) end.join.html_safe end modal << content_tag(:div, content, :class => "modal-body") modal << content_tag(:div, :class => "modal-footer") do [].tap do || << link_to([:cancel_label], "#", "data-dismiss" => "modal", :class => [:cancel_class]) << link_to([:ok_label], [:ok_link], :class => [:ok_class], :id => [:ok_id]) end.join.html_safe end end.join.html_safe end end |
#tb_modal_button(name, modal_id, options = nil) ⇒ Object
Creates a button with the given name
that will active a Twitter Bootstrap Modal with the id of modal_id
.
Options
-
:data-toggle
- Defaults to modal -
All other options are passed to tb_link
202 203 204 205 206 207 208 |
# File 'lib/twitter_bootstrap_helper/helpers/twitter_bootstrap_helper.rb', line 202 def (name, modal_id, =nil) = { "data-toggle" => "modal" }.merge( || {}) tb_link name, "##{modal_id}", end |
#tb_nav(nav_type = nil, content_or_options_with_block = {}, options = {}, &block) ⇒ Object
Nav Helpers
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/twitter_bootstrap_helper/helpers/twitter_bootstrap_helper.rb', line 118 def tb_nav(nav_type = nil, = {}, = {}, &block) if block_given? = if .is_a?(Hash) content = capture(&block) else content = end = { :type => nil, # :tabs, :pills or :list :stacked => false, :dropdown_menu => false, :html => {} }.merge( || {}) classes = [].tap do |c| unless nav_type == :dropdown_menu c << "nav" c << "nav-#{nav_type.to_s}" unless nav_type.nil? else c << "dropdown-menu" end c << "nav-stacked" if [:stacked] c << [:html][:class] if [:html][:class] end.join(" ") [:html][:class] = classes.blank? ? nil : classes content_tag :ul, content, [:html] end |
#tb_nav_item(content_or_options_with_block = nil, options = nil, &block) ⇒ Object
148 149 150 151 152 153 154 155 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 |
# File 'lib/twitter_bootstrap_helper/helpers/twitter_bootstrap_helper.rb', line 148 def tb_nav_item( = nil, = nil, &block) if block_given? = if .is_a?(Hash) content = capture(&block) else content = end = { :header => false, :active => false, :disabled => false, :dropdown => false, :dropdown_submenu => false, :html => {} }.merge( || {}) classes = [].tap do |c| if [:divider, :divider_vertical].include?(content) c << content.to_s.gsub("_", "-") content = nil end c << [:html][:class] if [:html][:class] .except(:html, :nav_header).each_pair do |key, value| c << key.to_s.gsub("_", "-") if value end c << "nav-header" if [:header] end.join(" ") [:html][:class] = classes.blank? ? nil : classes content = tb_dropdown_toggle([:dropdown]) + content if [:dropdown] content_tag :li, content, [:html] end |
#tb_nav_items(items) ⇒ Object
182 183 184 185 186 187 188 |
# File 'lib/twitter_bootstrap_helper/helpers/twitter_bootstrap_helper.rb', line 182 def tb_nav_items(items) [].tap do |l| items.each_pair do |title, | l << tb_nav_item(tb_link(title, [:path], [:link]), [:nav]) end end.join.html_safe end |
#tb_sidebar_link(name, link = "#", options = {}) ⇒ Object
Creates an “li” tag formatted for a sidebar.
Options
-
:icon
- Optionally display an icon. -
:badge
- Defaults to 0. Badge is only displayed if the value is greater than 0 -
:badge_class
- Optional badge class to add. Eg “badge-success” -
:active
- adds the active class showing the item is selected
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/twitter_bootstrap_helper/helpers/twitter_bootstrap_helper.rb', line 90 def (name, link="#", = {}) = { active: (request.fullpath == link) }.merge( || {}) icon = nil li_class = [] i_class = [] if [:icon] icon = [:icon] .delete(:icon) end if [:active] li_class << "active" i_class << "icon-white" .delete(:active) end i_class << icon badge = ([:badge] || 0) > 0 ? " #{tb_badge([:badge], :class => "pull-right #{[:badge_class]}")}".html_safe : "" icon_tag = content_tag(:i, "", :class => i_class.join(" ")) content_tag(:li, link_to(icon_tag + name + badge, link, ), :class => li_class.empty? ? nil : li_class.join(" ")) end |