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.3"

Instance Method Summary collapse

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.



67
68
69
70
71
72
73
# File 'lib/twitter_bootstrap_helper/helpers/twitter_bootstrap_helper.rb', line 67

def tb_badge(name, options = {})
  options ||= {}

  options[:class] = "badge #{options[:class]}"

  (:span, name, options)
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
61
62
# File 'lib/twitter_bootstrap_helper/helpers/twitter_bootstrap_helper.rb', line 41

def tb_button(name, options = {})
  options = {
      class: "btn",
      icon: nil,
      type: :submit,
  }.merge(options || {})

  options = options.nil? ? default_options : default_options.merge(options)

  icon = nil
  if options[:icon]
    icon = options[:icon]
    options.delete(:icon)
  end

  button_label = [].tap do |b|
    b << tb_icon(icon)
    b << name
  end.join.html_safe

  (:button, button_label, options)
end

#tb_dropdown_toggle(name, options = {}) ⇒ Object



192
193
194
195
196
# File 'lib/twitter_bootstrap_helper/helpers/twitter_bootstrap_helper.rb', line 192

def tb_dropdown_toggle(name, options = {})
  options["data-toggle"] = "dropdown"
  options[:class] = "dropdown-toggle #{options[:class]}"
  tb_link "#{name} <b class=\"caret\"></b>".html_safe, "#", options
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, options = {})
  options = {
      class: ""
  }.merge(options)

  style = "color: #{options[:color]}" if options[:color]
  (:i, "", :class => "#{icon} #{options[: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.



78
79
80
81
82
83
84
# File 'lib/twitter_bootstrap_helper/helpers/twitter_bootstrap_helper.rb', line 78

def tb_label(name, options = {})
  options ||= {}

  options[:class] = "label #{options[:class]}"

  (:span, name, options)
end

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="#", options = nil)
  options ||= {}

  icon = nil
  if options[:icon]
    icon = options[:icon]
    options.delete(:icon)
  end

  label = [].tap do |b|
    b << tb_icon(icon) unless icon.nil?
    b << name
  end.join.html_safe

  link_to(label, link, options)
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



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
259
260
# File 'lib/twitter_bootstrap_helper/helpers/twitter_bootstrap_helper.rb', line 223

def tb_modal(modal_id, content_or_options_with_block = nil, options = nil, &block)
  if block_given?
    options = content_or_options_with_block if content_or_options_with_block.is_a?(Hash)
    content = capture(&block)
  else
    content = content_or_options_with_block
  end

  options = {
      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(options || {})

   :div, :id => modal_id, :class => "modal hide #{ options[:fade] ? "fade" : ""}", "data-remote" => options[:remote] do
    [].tap do |modal|
      modal << (:div, :class => "modal-header") do
        [].tap do |header|
          header << (:button, "&times;".html_safe, :class => "close", "data-dismiss" => "modal")
          header << (:h3, options[:title])
        end.join.html_safe
      end
      modal << (:div, content, :class => "modal-body")
      modal << (:div, :class => "modal-footer") do
        [].tap do |footer|
          footer << link_to(options[:cancel_label], "#", "data-dismiss" => "modal", :class => options[:cancel_class])
          footer << link_to(options[:ok_label], options[:ok_link], :class => options[:ok_class], :id => options[: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



204
205
206
207
208
209
210
# File 'lib/twitter_bootstrap_helper/helpers/twitter_bootstrap_helper.rb', line 204

def tb_modal_button(name, modal_id, options=nil)
  options = {
      "data-toggle" => "modal"
  }.merge(options || {})

  tb_link name, "##{modal_id}", options
end

#tb_nav(nav_type = nil, content_or_options_with_block = {}, options = {}, &block) ⇒ Object

Nav Helpers



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
147
148
# File 'lib/twitter_bootstrap_helper/helpers/twitter_bootstrap_helper.rb', line 120

def tb_nav(nav_type = nil, content_or_options_with_block = {}, options = {}, &block)
  if block_given?
    options = content_or_options_with_block if content_or_options_with_block.is_a?(Hash)
    content = capture(&block)
  else
    content = content_or_options_with_block
  end

  options = {
      :type => nil, # :tabs, :pills or :list
      :stacked => false,
      :dropdown_menu => false,
      :html => {}
  }.merge(options || {})

  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 options[:stacked]
    c << options[:html][:class] if options[:html][:class]
  end.join(" ")

  options[:html][:class] = classes.blank? ? nil : classes
   :ul, content, options[:html]
end

#tb_nav_item(content_or_options_with_block = nil, options = nil, &block) ⇒ Object



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
181
182
# File 'lib/twitter_bootstrap_helper/helpers/twitter_bootstrap_helper.rb', line 150

def tb_nav_item(content_or_options_with_block = nil, options = nil, &block)
  if block_given?
    options = content_or_options_with_block if content_or_options_with_block.is_a?(Hash)
    content = capture(&block)
  else
    content = content_or_options_with_block
  end

  options = {
      :header => false,
      :active => false,
      :disabled => false,
      :dropdown => false,
      :dropdown_submenu => false,
      :html => {}
  }.merge(options || {})

  classes = [].tap do |c|
    if [:divider, :divider_vertical].include?(content)
      c << content.to_s.gsub("_", "-")
      content = nil
    end
    c << options[:html][:class] if options[:html][:class]
    options.except(:html, :nav_header).each_pair do |key, value|
      c << key.to_s.gsub("_", "-") if value
    end
    c << "nav-header" if options[:header]
  end.join(" ")

  options[:html][:class] = classes.blank? ? nil : classes
  content = tb_dropdown_toggle(options[:dropdown]) + content if options[:dropdown]
   :li, content, options[:html]
end

#tb_nav_items(items) ⇒ Object



184
185
186
187
188
189
190
# File 'lib/twitter_bootstrap_helper/helpers/twitter_bootstrap_helper.rb', line 184

def tb_nav_items(items)
  [].tap do |l|
    items.each_pair do |title, options|
      l << tb_nav_item(tb_link(title, options[:path], options[:link]), options[:nav])
    end
  end.join.html_safe
end

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



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/twitter_bootstrap_helper/helpers/twitter_bootstrap_helper.rb', line 92

def tb_sidebar_link(name, link="#", options = {})
  options = {
      active: (request.fullpath == link)
  }.merge(options || {})

  icon = nil
  li_class = []
  i_class = []

  if options[:icon]
    icon = options[:icon]
    options.delete(:icon)
  end

  if options[:active]
    li_class << "active"
    i_class << "icon-white"
    options.delete(:active)
  end

  i_class << icon
  badge = (options[:badge] || 0) > 0 ? " #{tb_badge(options[:badge], :class => "pull-right #{options[:badge_class]}")}".html_safe : ""
  icon_tag = (:i, "", :class => i_class.join(" "))
  (:li, link_to(icon_tag + name + badge, link, options), :class => li_class.empty? ? nil : li_class.join(" "))
end