module Bh::Helpers

Public Instance Methods

alert_box(*args, &block) click to toggle source

Displays a Bootstrap-styled alert message. @see getbootstrap.com/components/#alerts @return [String] the HTML to display a Bootstrap-styled alert message. @overload alert_box(content, options = {})

@param [#to_s] content the content to display in the alert.
@param [Hash] options the options for the alert box. Any option not
  listed below is passed as an HTML attribute to the alert’s `<div>`.
@option options [Boolean] :dismissible (false) whether to display an
  '×' to the right of the box that can be clicked to dismiss the alert.
@option options [#to_s] :context (:info) the contextual alternative to
  apply to the alert. Can be `:danger`, `:info`, `:success` or
  `:warning`.
@option options [#to_s] :priority if set to one of the priority levels
  of Rails flash contents, determines the context of the alert box.
  Can be :alert or :notice.
@example Display a dismissible alert box with a plain-text content.
    alert_box 'User updated successfully', dismissible: true

@overload alert_box(options = {}, &block)

@param [Hash] options the options for the alert box (see above).
@yieldreturn [#to_s] the content to display in the alert.
@example Display a success alert box with an HTML content.
    alert_box context: :success do
      content_tag :strong, 'User updated successfully'
    end
# File lib/bh/helpers/alert_box_helper.rb, line 29
def alert_box(*args, &block)
  alert_box = Bh::AlertBox.new(self, *args, &block)
  alert_box.extract! :context, :priority, :dismissible

  alert_box.append_class! :alert
  alert_box.append_class! alert_box.context_class
  alert_box.merge! role: :alert
  alert_box.prepend_html! alert_box.dismissible_button
  alert_box.render_tag :div
end
bootstrap_css(options = {}) click to toggle source

@see www.bootstrapcdn.com @return [String] the URL of the Bootstrap CSS file @param [Hash] options the options for which CSS file to retrieve. @option options [String] :version the version of Bootstrap. @option options [String] :scheme the URI scheme to use. @option options [Boolean] :minified whether to use the minified version.

# File lib/bh/helpers/cdn_helper.rb, line 11
def bootstrap_css(options = {})
  Bh::Cdn.bootstrap options.merge(name: 'bootstrap', extension: 'css')
end
bootstrap_js(options = {}) click to toggle source

@see www.bootstrapcdn.com @return [String] the URL of the Bootstrap JS file @param [Hash] options the options for which JS file to retrieve. @option options [String] :version the version of Bootstrap. @option options [String] :scheme the URI scheme to use. @option options [Boolean] :minified whether to use the minified version.

# File lib/bh/helpers/cdn_helper.rb, line 42
def bootstrap_js(options = {})
  Bh::Cdn.bootstrap options.merge(name: 'bootstrap', extension: 'js')
end
bootstrap_theme_css(options = {}) click to toggle source

@see www.bootstrapcdn.com @return [String] the URL of the Bootstrap Theme CSS file @param [Hash] options the options for which CSS file to retrieve. @option options [String] :version the version of Bootstrap. @option options [String] :scheme the URI scheme to use. @option options [Boolean] :minified whether to use the minified version.

# File lib/bh/helpers/cdn_helper.rb, line 21
def bootstrap_theme_css(options = {})
  Bh::Cdn.bootstrap options.merge(name: 'bootstrap-theme', extension: 'css')
end
button(*args, &block) click to toggle source

Displays a Bootstrap-styled button. @see getbootstrap.com/css/#buttons @return [String] the HTML to display a Bootstrap-styled button. @overload button(caption, options = {})

@param [#to_s] caption the caption to display in the button.
@param [Hash] options the options for the button. Any option not
  listed below is passed as an HTML attribute to the `<button>` tag.
@option options [#to_s] :context (:default) the contextual alternative
  to apply to the button. Can be `:danger`, `:info`, `:link`,
  `:primary`, `:success` or `:warning`.
@option options [#to_s] :size the size of the button.
  Can be `:extra_small` (alias `:xs`), `:large` (alias `:lg`) or
  `:small` (alias `:sm`).
@option options [#to_s] :layout if set to `:block`, span the button
  for the full width of the parent.
@example Display a button styled as a link.
    button 'Click here', context: :link

@overload button(options = {}, &block)

@param [Hash] options the options for the button (see above).
@yieldreturn [#to_s] the caption to display in the button.
@example Display a button with an HTML caption.
    button do
      content_tag :strong, 'Click here'
    end
# File lib/bh/helpers/button_helper.rb, line 29
def button(*args, &block)
  button = Bh::Button.new(self, *args, &block)
  button.extract! :context, :size, :layout

  button.append_class! :btn
  button.append_class! button.context_class
  button.append_class! button.size_class
  button.append_class! button.layout_class
  button.render_tag :button
end
button_to(*args, &block) click to toggle source

Overrides ‘button_to` to display a Bootstrap-styled button. Can only be used in Ruby frameworks that provide the `button_to` method. Only overrides the original method if called with any of the `:context`, `:size` or `:layout` option, otherwise calls the original method. @see getbootstrap.com/css/#buttons @see getbootstrap.com/components/#nav @see getbootstrap.com/components/#navbar-buttons @see api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to @see rubydoc.info/gems/padrino-helpers/Padrino/Helpers/FormHelpers#button_to-instance_method @return [String] the HTML to display a Bootstrap-styled button. @overload button_to(caption, url, options = {})

@param [#to_s] caption the caption to display in the button.
@param [#to_s] url the URL to submit to.
@param [Hash] options the options for the button. Any option not
  listed below is passed to the original `button_to` method.
@option options [#to_s] :context (:default) the contextual alternative
  to apply to the button. Can be `:danger`, `:info`, `:link`,
  `:primary`, `:success` or `:warning`.
@option options [#to_s] :size the size of the button.
  Can be `:extra_small` (alias `:xs`), `:large` (alias `:lg`) or
  `:small` (alias `:sm`).
@option options [#to_s] :layout if set to `:block`, span the button
  for the full width of the parent.
@example Display a small button to submit to '/create_user'.
    button_to 'Create', '/create_user', size: :small

@overload button_to(url, options = {}, &block)

@param [#to_s] url the URL to submit to (see above).
@param [Hash] options the options for the button (see above).
@yieldreturn [#to_s] the caption to display in the button.
@example Display a danger button with HTML content to delete a user.
    button_to '/user_destroy', context: :danger do
      content_tag :strong, "Delete user"
    end
Calls superclass method
# File lib/bh/helpers/button_to_helper.rb, line 40
def button_to(*args, &block)
  button_to = Bh::ButtonTo.new self, *args, &block

  if button_to.extract! :context, :size, :layout
    button_to.append_button_class! :btn
    button_to.append_button_class! button_to.context_class
    button_to.append_button_class! button_to.size_class
    button_to.append_button_class! button_to.layout_class
    button_to.append_form_class! 'navbar-form' if Bh::Stack.find(Bh::Navbar)
  end

  html = if block_given? && button_to.accepts_block?
    super button_to.url, button_to.attributes, &-> { button_to.content }
  else
    super button_to.content, button_to.url, button_to.attributes, &nil
  end

  if Bh::Stack.find(Bh::Nav)
    container = Bh::Base.new(self) { html }
    container.render_tag :li
  else
    html
  end
end
dropdown(caption, options = {}, &block) click to toggle source

@see getbootstrap.com/components/#dropdowns @see getbootstrap.com/components/#btn-dropdowns @return [String] an HTML block to display a dropdown. @example A right-aligned dropdown with a links.

dropdown 'Menu', align: :right do
  content_tag :li, link_to('Home', '/')
end

@param [#to_s] caption the caption for the dropdown button. @param [Hash] options the display options for the dropdown. @option options [Boolean] :groupable (true) if true, uses the “btn-group”

class rather than then "dropdown" class, so that multiple dropdown
buttons can be aligned in the same row (as a group of buttons).

@option options [Boolean] :split (false) if true, creates a split button

that only toggles the dropdown when clicked on the rightmost part.

@option options [#to_s] :direction if set to :up, the dropdown appears

above the button, rather than below.

@option options [#to_s] :layout if set to ‘:block`, span the dropdown

button for the full width of the parent. Note that :groupable takes
precedence, so it must be set to `false` to display a full-width button.

@option options [#to_s] :align if set to :right, the dropdown is aligned

to the right-end of the button, rather than to the left-end.

@option options [#to_s] :context (:default) the context for the button,

which determines its color.

@option options [#to_s] :size the size of the button. @yieldreturn [#to_s] the content of the dropdown.

font_awesome_css(options = {}) click to toggle source

@see www.bootstrapcdn.com @return [String] the URL of the Font Awesome CSS file @param [Hash] options the options for which CSS file to retrieve. @option options [String] :version the version of Font Awesome. @option options [String] :scheme the URI scheme to use. @option options [Boolean] :minified whether to use the minified version. @see fontawesome.io/get-started/

# File lib/bh/helpers/cdn_helper.rb, line 32
def font_awesome_css(options = {})
  Bh::Cdn.font_awesome options.merge(name: 'font-awesome', extension: 'css')
end
glyphicon(name = nil, options = {}) click to toggle source

Displays any of the 200 glyphicons available in Bootstrap. @deprecated Use {#icon} instead. @see getbootstrap.com/components/#glyphicons @return [String] the HTML to display a glyphicon. @param [#to_s] name the name of the icon to display, with either dashes

or underscores to separate multiple words.

@param [Hash] options the options to pass to the icon’s ‘<span>`. @example Display the “zoom-in” glyphicon

glyphicon :zoom_in
# File lib/bh/helpers/glyphicon_helper.rb, line 14
def glyphicon(name = nil, options = {})
  icon name, options.merge(library: :glyphicons)
end
horizontal(*args, &block) click to toggle source

Displays the collapsable portion of a Bootstrap-styled navbar. @see getbootstrap.com/components/#navbar @return [String] the HTML to display the collapsable portion of a

Bootstrap-styled navbar.

@overload horizontal(content, options = {})

@param [#to_s] content the collapsable content to display in the navbar.
@param [Hash] options the options to pass to the wrapping `<div>`.
  Note that the `:id` option is ignored since the id must generated
  by the navbar in order to match with the target of the toggle button.

@overload horizontal(options = {}, &block)

@param [Hash] options the options to pass to the wrapping `<div>`.
@yieldreturn [#to_s] the collapsable content to display in the navbar.

@example Display a navbar with two collapsable links.

navbar do
  horizontal do
    nav do
      link_to 'Home', '/'
      link_to 'Profile', '/profile'
    end
  end
end
# File lib/bh/helpers/horizontal_helper.rb, line 26
def horizontal(*args, &block)
  if navbar = Bh::Stack.find(Bh::Navbar)
    horizontal = Bh::Base.new self, *args, &block
    horizontal.append_class! :'collapse navbar-collapse'
    horizontal.merge! id: navbar.id
    horizontal.render_tag :div
  end
end
icon(name = nil, options = {}) click to toggle source

Displays a Bootstrap-styled vector icon. @see getbootstrap.com/components/#glyphicons @see fortawesome.github.io/Font-Awesome/examples/#bootstrap @return [String] the HTML to display a vector (font) icon. @param [#to_s] name the name of the icon to display, with either dashes

or underscores to separate multiple words.

@param [Hash] options the options for the icon tag. Any option not

listed below is passed as an HTML attribute to the icon’s `<span>`.

@option options [#to_s] :library (:glyphicons) the vector icon library

to use. Valid values are 'glyphicon', 'glyphicons' (for Glyphicons),
'font-awesome', 'font_awesome' and 'fa' (for Font Awesome).

@example Display the “fire” font awesome icon with a title

icon 'fire', library: :font_awesome, title: 'Hot'
# File lib/bh/helpers/icon_helper.rb, line 18
def icon(name = nil, options = {})
  icon = Bh::Icon.new self, nil, options.merge(name: name)
  icon.extract! :library, :name

  icon.append_class! icon.library_class
  icon.append_class! icon.name_class
  icon.render_tag :span
end
modal(*args, &block) click to toggle source

Displays a Bootstrap-styled modal. @see getbootstrap.com/javascript/#modals @return [String] the HTML to display a Bootstrap-styled modal. @overload modal(body, options = {})

@param [#to_s] body the content to display as the modal body.
@param [Hash] options the options for the modal. Any option not listed
  below is ignored, except for `:id` which is passed as an HTML
  attribute to the modal’s `<div>`.
@option options [#to_s] :title ('Modal') the title of the modal.
@option options [#to_s] :body the content to display as the modal body.
  Using this option is equivalent to passing the body as an argument.
@option options [#to_s] :size the size of the modal. Can be `:large`
  (alias `:lg`) or `:small` (alias `:sm`).
@option options [Hash] :button the options for the toggle button.
  * :caption (#to_s) ('Modal') the caption of the toggle button.
  * :context (#to_s) (:default) the contextual alternative to apply to
     the toggle button. Can be `:danger`, `:info`, `:link`, `:primary`,
    `:success` or `:warning`.
  * :size (#to_s) the size of the toggle button. Can be `:extra_small`
    (alias `:xs`), `:large` (alias `:lg`) or `:small` (alias `:sm`).
  * :layout (#to_s) if set to `:block`, span the button for the full
    width of the parent.
@example Display a simple modal toggled by a blue button.
    modal 'You clicked me!', title: 'Click me', button: {context: :info}

@overload modal(options = {}, &block)

@param [Hash] options the options for the modal (see above).
@yieldreturn [#to_s] the content to display in the modal.
@example Display a modal with HTML content.
    modal title: 'Click me' do
      content_tag :div, class: 'modal-body' do
        content_tag :em, 'You clicked me!'
      end
    end
nav(options = {}, &block) click to toggle source

Displays a Bootstrap-styled nav. @see getbootstrap.com/components/#nav @return [String] the HTML to display a Bootstrap-styled nav. @param [Hash] options the options for the nav. Any option not listed below

is passed as an HTML attribute to the alert’s `<ul>`.

@option options [#to_s] :as (:tabs) the style of the nav. Can be ‘:tabs`

or `:pills`.

@option options [#to_s] :layout the layout of the nav. Can be

`:justified` or `:stacked`.

@yieldreturn [#to_s] the content to display in the nav. @example Display a pills-styled nav with a link.

nav as: :pills do
  link_to 'Home', '/'
end
navbar(options = {}, &block) click to toggle source

Displays a Bootstrap-styled navbar. @see getbootstrap.com/components/#navbar @return [String] the HTML to display a Bootstrap-styled navbar. @param [Hash] options the options for the navbar. Any option not listed

below is ignored, except for `:id` which is passed as an HTML
attribute to the navbar’s collapsable `<div>`.

@option options [Boolean] :fluid (false) whether to use a fluid container

to surround the navbar content.

@option options [Boolean] :inverted (false) whether to use an inverted

palette of colors.

@option options [#to_s] :position the position of the navbar. Can be

`:top` (alias `:fixed_top`), `:bottom` (alias `:fixed_bottom`) or
`:static` (alias `:static_top`).

@option options [#to_s] :padding (70) if position is set to :top or

:bottom, the padding to at the top (or bottom) of <body> to prevent the
navbar from overlaying the content.

@yieldreturn [#to_s] the content to display in the navbar. @example Display an inverted navbar with three links.

navbar inverted: true do
  vertical do
    image_tag('logo')
  end
  horizontal do
    nav do
      link_to 'Home', '/'
      link_to 'Profile', '/profile'
    end
  end
end
panel(*args, &block) click to toggle source

Displays a Bootstrap-styled panel. @see getbootstrap.com/components/#panels @return [String] the HTML to display a Bootstrap-styled panel. @overload panel(body, options = {})

@param [#to_s] body the content to display as the panel body.
@param [Hash] options the options for the panel. Any option not
  listed below is passed as an HTML attribute to the panel’s `<div>`.
@option options [#to_s] :title the text to display as the panel title.
@option options [#to_s] :heading the text to display as the panel 
  heading.
@option options [#to_s] :body the text to display as the panel body.
  Using this option is equivalent to passing the body as an argument.
@option options [#to_s] :context (#to_s) (:default) the contextual
  alternative to apply to the panel heading and border. Can be
  `:danger`, `:info`, `:primary`, `:success` or `:warning`.
@option options [#to_s] :tag (#to_s) (:div) the HTML tag to wrap the
  panel into.
@example Display an informative panel with plain-text content.
    panel 'You accepted the Terms of service.', context: :success

@overload panel(options = {}, &block)

@param [Hash] options the options for the panel (see above).
@yieldreturn [#to_s] the content to display in the panel.
@example Display a panel with HTML content.
    panel title: 'Thanks' do
      content_tag :div, class: 'panel-body' do
        content_tag :em, 'ou accepted the Terms of service.'
      end
    end
# File lib/bh/helpers/panel_helper.rb, line 35
def panel(*args, &block)
  panel = Bh::Panel.new self, *args, &block
  panel.extract! :body, :context, :title, :heading, :tag

  panel.append_class! :panel
  panel.append_class! panel.context_class
  panel.merge_html! panel.body
  panel.prepend_html! panel.heading

  if panel_row = Bh::Stack.find(Bh::PanelRow)
    container = Bh::Base.new(self) { panel.content_tag panel.tag }
    container.append_class! panel_row.column_class
    container.render_tag :div
  else
    panel.render_tag panel.tag
  end
end
panel_row(options = {}, &block) click to toggle source

Wraps a set of Bootstrap-styled panels in a row. @see getbootstrap.com/components/#panels @see getbootstrap.com/css/#grid @return [String] the HTML to display a row of Bootstrap-styled panels. @param [Hash] options the options for the row. Any option not

listed below is passed as an HTML attribute to the row’s `<div>`.

@option options [#to_s] :column_class the class to wrap each panel with.

Useful to specify a grid size for the column such as 'col-sm-4' to
indicate how many columns of the row each panel should occupy.

@yieldreturn [#to_s] the panels to display in a row. @example Display a row of two panels with the same width.

panel_row column_class: 'col-sm-6' do
  panel 'Panel #1', context: :success
  panel 'Panel #2', context: :info
end
# File lib/bh/helpers/panel_row_helper.rb, line 20
def panel_row(options = {}, &block)
  panel_row = Bh::PanelRow.new self, options, &block
  panel_row.extract! :column_class

  panel_row.append_class! :row
  panel_row.render_tag :div
end
progress_bar(args = nil, container_options = {}) click to toggle source

Displays one or more Bootstrap-styled progress bars. @see getbootstrap.com/components/#progress @return [String] the HTML to display Bootstrap-styled progress bars. @overload progress_bar(bar_options = {}, container_options = {})

@param [Hash] bar_options the options to display a single progress bar.
  Any option not listed below is passed as an HTML attribute to the
  bar’s `<div>`.
@option bar_options [Boolean, #to_s] :label (false) the label to display
  on top of the progress bar. If set to false, the label is hidden. If
  set to true, the label is generated from the percentage value. Any
  other provided value is used directly as the label.
@option bar_options [Boolean] :striped (false) whether to display a
  striped version of the progress bar (rather than solid color).
@option bar_options [Boolean] :animated (false) whether to display an
  animated version of the progress bar (rather than solid color).
@option bar_options [#to_s] :context (:default) the contextual
  alternative to apply to the progress bar. Can be `:success`, `:info`,
  `:warning` or `:danger`.
@param [Hash] container_options the options to pass as HTML attributes
  to the container’s `<div>`.
@example Display a 30% warning progress bar.
    progress_bar percentage: 30, context: :warning

@overload progress_bar(stacked_bars_options = [], container_options = {})

@param [Hash] stacked_bars_options an array of bar_options (see above).
  When an array is provided, a group of stacked progress bars is 
  displayed, each one matching the corresponding bar options.
@param [Hash] container_options the options to pass as HTML attributes
  to the container’s `<div>`.
@example Display two stacked progress bars.
    progress_bar [{percentage: 30, context: :warning}, {percentage: 20}]
# File lib/bh/helpers/progress_bar_helper.rb, line 35
def progress_bar(args = nil, container_options = {})
  progress_bars = Array.wrap(args).map do |options|
    progress_bar = Bh::ProgressBar.new self, nil, options
    progress_bar.extract! :percentage, :context, :striped, :animated, :label

    progress_bar.merge! progress_bar.aria_values
    progress_bar.append_class! :'progress-bar'
    progress_bar.append_class! progress_bar.context_class
    progress_bar.append_class! progress_bar.striped_class
    progress_bar.append_class! progress_bar.animated_class
    progress_bar.merge! progress_bar.values
    progress_bar.prepend_html! progress_bar.label
    progress_bar
  end

  container = Bh::Base.new self, progress_bars, container_options
  container.append_class! :progress
  container.render_tag :div
end
vertical(*args, &block) click to toggle source

Displays the non-collapsable portion of a Bootstrap-styled navbar. @see getbootstrap.com/components/#navbar @return [String] the HTML to display the non-collapsable portion of a

Bootstrap-styled navbar.

@overload vertical(content, options = {})

@param [#to_s] content the non-collapsable content to display in the
  navbar.
@param [Hash] options the options to pass to the wrapping `<div>`.

@overload vertical(options = {}, &block)

@param [Hash] options the options to pass to the wrapping `<div>`.
@yieldreturn [#to_s] the non-collapsable content to display in the
  navbar.

@example Display a navbar a non-collapsable links.

navbar do
  vertical do
    link_to 'Home', '/'
  end
end
# File lib/bh/helpers/vertical_helper.rb, line 24
def vertical(*args, &block)
  if navbar = Bh::Stack.find(Bh::Navbar)
    vertical = Bh::Vertical.new self, *args, &block
    vertical.append_class! :'navbar-header'
    vertical.prepend_html! vertical.toggle_button(navbar.id)
    vertical.render_tag :div
  end
end