Module: BootstrapHelpers

Defined in:
lib/bootstrap-helpers.rb

Overview

Author

Maurizio Casimirri ([email protected])

Copyright

Copyright © 2012 Maurizio Casimirri

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Defined Under Namespace

Classes: AccordionBuilder, TabsBuilder

Instance Method Summary collapse

Instance Method Details

#accordion(opts = {}) ⇒ Object

~ AccordionBuilder



308
309
310
311
312
313
314
# File 'lib/bootstrap-helpers.rb', line 308

def accordion(opts = {})
  opts[:accordion_id] ||= 'accordion'
  builder = AccordionBuilder.new opts, self
   :div, :class => 'accordion', :id => opts[:accordion_id] do
    yield builder
  end
end

#alert(message) ⇒ Object



120
121
122
# File 'lib/bootstrap-helpers.rb', line 120

def alert(message)
  "<div class='alert'>#{h(message)}</div>".html_safe
end

#bootstrap_container_classObject

Scaffold =



29
30
31
# File 'lib/bootstrap-helpers.rb', line 29

def bootstrap_container_class
  bootstrap_fluid? ? "container-fluid" : "container"
end

#bootstrap_fluid!Object



37
38
39
# File 'lib/bootstrap-helpers.rb', line 37

def bootstrap_fluid!
  @bootstrap_float_style = true
end

#bootstrap_fluid?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/bootstrap-helpers.rb', line 41

def bootstrap_fluid?
  !!@bootstrap_float_style
end

#bootstrap_row_classObject



33
34
35
# File 'lib/bootstrap-helpers.rb', line 33

def bootstrap_row_class
  bootstrap_fluid? ? "row-fluid" : "row"
end

#brand(*args, &block) ⇒ Object



213
214
215
216
217
218
219
220
221
# File 'lib/bootstrap-helpers.rb', line 213

def brand(*args, &block)

  if !args.last.is_a?(Hash)
    args << {}
  end
  args.last[:class] = "brand"

  link_to(*args, &block)
end

#button(*args, &block) ⇒ Object Also known as: btn



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/bootstrap-helpers.rb', line 84

def button(*args, &block)
  if !block_given?
    opts = args.extract_options!
    is_link = (!!args.second) || (args.any? && block_given?)

    icon = opts.delete(:icon)
    type_opt =  opts.delete(:type)
    kind = type_opt ? "btn-#{type_opt}" : nil
    size_opt = opts.delete(:size)
    size = size_opt ? "btn-#{size_opt}" : nil
    klasses = ("#{opts[:class]}".split(/\s+/) + ["btn", kind, size]).compact.uniq
    opts[:class] = klasses.join(" ")
    if icon
      icon_color = opts.delete(:icon_color)
      classes = [icon, icon_color].compact.map {|c| "icon-#{c}"}.join(" ")
      args.unshift "<i class=\"#{classes}\"></i> #{args.shift}".html_safe
    end

    args << opts
  end

  if is_link
    link_to(*args, &block)
  else
    ('button', *args, &block)
  end

end

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



45
46
47
48
49
50
51
52
# File 'lib/bootstrap-helpers.rb', line 45

def container(opts = {}, &block)
  if opts.delete(:fluid)
    bootstrap_fluid!
  end
    
  opts[:class] = ("#{opts[:class]}".split(" ") + [bootstrap_container_class]).join(" ")   
  ("div", opts, &block)
end


162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/bootstrap-helpers.rb', line 162

def dropdown_nav_item(label, opts = {}, &block)
  icon = opts.delete(:icon)
  if icon
    icon_color = opts.delete(:icon_color)
    classes = [icon, icon_color].compact.map {|c| "icon-#{c}"}.join(" ")
    label =  "<i class=\"#{classes}\"></i> #{label}".html_safe
  end

  html = <<-STR
    <li class="dropdown">
    <a class="dropdown-toggle"
     data-toggle="dropdown"
     href="#">
      #{label}
      <b class="caret"></b>
    </a>
    <ul class="dropdown-menu">#{capture(&block)}</ul></li>
  STR
  html.html_safe
end

#form_actions(&block) ⇒ Object



116
117
118
# File 'lib/bootstrap-helpers.rb', line 116

def form_actions(&block)
   :div, :class => "form-actions", &block
end

#icon(icon_id, opts = {}) ⇒ Object



72
73
74
75
76
77
# File 'lib/bootstrap-helpers.rb', line 72

def icon(icon_id, opts={})
  opts[:class] ||= ""
  opts[:class] << " icon icon-#{icon_id}"
  opts[:class].strip!
   :i, "", opts
end


146
147
148
# File 'lib/bootstrap-helpers.rb', line 146

def nav(options = {}, &block)
  "<ul class=\"nav #{options[:class]}\">#{capture(&block)}</ul>".html_safe
end


154
155
156
# File 'lib/bootstrap-helpers.rb', line 154

def nav_header(txt)
  "<li class=\"nav-header\">#{txt}</li>".html_safe
end


184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/bootstrap-helpers.rb', line 184

def nav_item(*args, &block)
  if !block_given?
    opts = args.extract_options!
    icon = opts.delete(:icon)
    float = opts.delete(:float)
    if icon
      icon_color = opts.delete(:icon_color)
      classes = [icon, icon_color].compact.map {|c| "icon-#{c}"}.join(" ")
      args.unshift "<i class=\"#{classes}\"></i> #{h(args.shift)}".html_safe
    end
    args << opts
  end

  url = block_given? ? args.first : args[1]

  classes = []
  if current_page?(url)
    classes <<  "active"
  end

  if  float
    classes += ["float", "#{float}"]
  end

  with_class = classes.any? ? " class='#{classes.join(' ')}'" : ""
  "<li#{with_class}>#{link_to(*args, &block)}</li>".html_safe
end


158
159
160
# File 'lib/bootstrap-helpers.rb', line 158

def nav_list(options = {}, &block)
  "<ul class=\"nav nav-list #{options[:class]}\">#{capture(&block)}</ul>".html_safe
end

Navigation =



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/bootstrap-helpers.rb', line 129

def navbar(*args, &block)
  opts = args.extract_options!
  fluid = opts.delete(:fluid)
  html = <<-STR
  <div class="navbar#{opts[:fixed].present? ? ' navbar-fixed-' + opts[:fixed].to_s : ''}">
    <div class="navbar-inner">
      <div class="#{fluid ? 'container-fluid' : 'container'}">
        #{capture(&block)}
      </div>
    </div>
  </div>
  STR

  html.html_safe
end

#pills(options = {}, &block) ⇒ Object



150
151
152
# File 'lib/bootstrap-helpers.rb', line 150

def pills(options = {}, &block)
  "<ul class=\"nav nav-pills #{options[:class]}\">#{capture(&block)}</ul>".html_safe
end

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



54
55
56
57
58
# File 'lib/bootstrap-helpers.rb', line 54

def row(opts = {}, &block)

  opts[:class] = ("#{opts[:class]}".split(" ") + [bootstrap_row_class]).join(" ")
  ("div", opts, &block)      
end

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



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/bootstrap-helpers.rb', line 60

def span(size, opts = {}, &block)
  offset = opts.delete(:offset)
  class_arr =  ("#{opts[:class]}".split(" ") + ["span#{size}"])
  if offset
    class_arr << "offset#{offset}"
  end
    
  opts[:class] = class_arr.join(" ")
  ("div", opts, &block)
    
end

#tabs(opts = {}) {|builder| ... } ⇒ Object

Yields:

  • (builder)


254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/bootstrap-helpers.rb', line 254

def tabs(opts = {})
  opts[:direction] ||= 'above'
  opts[:style] ||= 'tabs'
  builder = TabsBuilder.new self
  yield builder
  tabs = (:ul, builder.pane_handles.join("\n").html_safe, :class => "nav nav-#{opts[:style]}")
  contents = (:div, builder.pane_contents.join("\n").html_safe, :class => 'tab-content')
  css_direction = "tabs-#{opts[:direction]}" unless opts[:direction] == 'above'
   :div, :class => "tabbable #{css_direction}" do
    if opts[:direction] == 'below'
      contents + tabs
    else
      tabs + contents
    end
  end
end