module BootstrapHelpers

Author

Maurizio Casimirri (maurizio.cas@gmail.com)

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.

Public Instance Methods

accordion(opts = {}) { |builder| ... } click to toggle source
# File lib/bootstrap-helpers.rb, line 308
def accordion(opts = {})
  opts[:accordion_id] ||= 'accordion'
  builder = AccordionBuilder.new opts, self
  content_tag :div, :class => 'accordion', :id => opts[:accordion_id] do
    yield builder
  end
end
alert(message) click to toggle source
# File lib/bootstrap-helpers.rb, line 120
def alert(message)
  "<div class='alert'>#{h(message)}</div>".html_safe
end
bootstrap_container_class() click to toggle source

Scaffold =

# File lib/bootstrap-helpers.rb, line 29
def bootstrap_container_class
  bootstrap_fluid? ? "container-fluid" : "container"
end
bootstrap_fluid!() click to toggle source
# File lib/bootstrap-helpers.rb, line 37
def bootstrap_fluid!
  @bootstrap_float_style = true
end
bootstrap_fluid?() click to toggle source
# File lib/bootstrap-helpers.rb, line 41
def bootstrap_fluid?
  !!@bootstrap_float_style
end
bootstrap_row_class() click to toggle source
# File lib/bootstrap-helpers.rb, line 33
def bootstrap_row_class
  bootstrap_fluid? ? "row-fluid" : "row"
end
brand(*args, &block) click to toggle source
# 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
btn(*args, &block)
Alias for: button
button(*args, &block) click to toggle source

Base CSS =

# 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
    content_tag('button', *args, &block)
  end

end
Also aliased as: btn
container(opts = {}, &block) click to toggle source
# 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(" ")   
  content_tag("div", opts, &block)
end
dropdown_nav_item(label, opts = {}, &block) click to toggle source
form_actions(&block) click to toggle source
# File lib/bootstrap-helpers.rb, line 116
def form_actions(&block)
  content_tag :div, :class => "form-actions", &block
end
icon(icon_id, opts={}) click to toggle source
# File lib/bootstrap-helpers.rb, line 72
def icon(icon_id, opts={})
  opts[:class] ||= ""
  opts[:class] << " icon icon-#{icon_id}"
  opts[:class].strip!
  content_tag :i, "", opts
end
nav(options = {}, &block) click to toggle source
nav_header(txt) click to toggle source
nav_item(*args, &block) click to toggle source
nav_list(options = {}, &block) click to toggle source
navbar(*args, &block) click to toggle source

Navigation =

pills(options = {}, &block) click to toggle source
# 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) click to toggle source
# File lib/bootstrap-helpers.rb, line 54
def row(opts = {}, &block)

  opts[:class] = ("#{opts[:class]}".split(" ") + [bootstrap_row_class]).join(" ")
  content_tag("div", opts, &block)      
end
span(size, opts = {}, &block) click to toggle source
# 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(" ")
  content_tag("div", opts, &block)
    
end
tabs(opts = {}) { |builder| ... } click to toggle source
# File lib/bootstrap-helpers.rb, line 254
def tabs(opts = {})
  opts[:direction] ||= 'above'
  opts[:style] ||= 'tabs'
  builder = TabsBuilder.new self
  yield builder
  tabs = content_tag(:ul, builder.pane_handles.join("\n").html_safe, :class => "nav nav-#{opts[:style]}")
  contents = content_tag(:div, builder.pane_contents.join("\n").html_safe, :class => 'tab-content')
  css_direction = "tabs-#{opts[:direction]}" unless opts[:direction] == 'above'
  content_tag :div, :class => "tabbable #{css_direction}" do
    if opts[:direction] == 'below'
      contents + tabs
    else
      tabs + contents
    end
  end
end