Module: Bh::Helpers

Included in:
Form::BaseHelper, Form::FieldsetHelper
Defined in:
lib/bh/helpers/cdn_helper.rb,
lib/bh/helpers/nav_helper.rb,
lib/bh/helpers/icon_helper.rb,
lib/bh/helpers/panel_helper.rb,
lib/bh/helpers/modal_helper.rb,
lib/bh/helpers/button_helper.rb,
lib/bh/helpers/navbar_helper.rb,
lib/bh/helpers/link_to_helper.rb,
lib/bh/helpers/vertical_helper.rb,
lib/bh/helpers/dropdown_helper.rb,
lib/bh/helpers/glyphicon_helper.rb,
lib/bh/helpers/alert_box_helper.rb,
lib/bh/helpers/button_to_helper.rb,
lib/bh/helpers/panel_row_helper.rb,
lib/bh/helpers/horizontal_helper.rb,
lib/bh/helpers/progress_bar_helper.rb

Instance Method Summary collapse

Instance Method Details

#alert_box(content, options = {}) ⇒ String #alert_box(options = {}, &block) ⇒ String

Displays a Bootstrap-styled alert message.

Overloads:

  • #alert_box(content, options = {}) ⇒ String

    Examples:

    Display a dismissible alert box with a plain-text content.

    alert_box 'User updated successfully', dismissible: true
    

    Options Hash (options):

    • :dismissible (Boolean) — default: false

      whether to display an '×' to the right of the box that can be clicked to dismiss the alert.

    • :context (#to_s) — default: :info

      the contextual alternative to apply to the alert. Can be :danger, :info, :success or :warning.

    • :priority (#to_s)

      if set to one of the priority levels of Rails flash contents, determines the context of the alert box. Can be :alert or :notice.

  • #alert_box(options = {}, &block) ⇒ String

    Examples:

    Display a success alert box with an HTML content.

    alert_box context: :success do
       :strong, 'User updated successfully'
    end
    

    Yield Returns:

    • (#to_s)

      the content to display in the alert.

See Also:



29
30
31
32
33
34
35
36
37
38
# 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 = {}) ⇒ String

Returns the URL of the Bootstrap CSS file

Options Hash (options):

  • :version (String)

    the version of Bootstrap.

  • :scheme (String)

    the URI scheme to use.

  • :minified (Boolean)

    whether to use the minified version.

See Also:



11
12
13
# 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 = {}) ⇒ String

Returns the URL of the Bootstrap JS file

Options Hash (options):

  • :version (String)

    the version of Bootstrap.

  • :scheme (String)

    the URI scheme to use.

  • :minified (Boolean)

    whether to use the minified version.

See Also:



42
43
44
# 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 = {}) ⇒ String

Returns the URL of the Bootstrap Theme CSS file

Options Hash (options):

  • :version (String)

    the version of Bootstrap.

  • :scheme (String)

    the URI scheme to use.

  • :minified (Boolean)

    whether to use the minified version.

See Also:



21
22
23
# 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(caption, options = {}) ⇒ String #button(options = {}, &block) ⇒ String

Displays a Bootstrap-styled button.

Overloads:

  • #button(caption, options = {}) ⇒ String

    Examples:

    Display a button styled as a link.

    button 'Click here', context: :link
    

    Options Hash (options):

    • :context (#to_s) — default: :default

      the contextual alternative to apply to the button. Can be :danger, :info, :link, :primary, :success or :warning.

    • :size (#to_s)

      the size of the 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.

  • #button(options = {}, &block) ⇒ String

    Examples:

    Display a button with an HTML caption.

    button do
       :strong, 'Click here'
    end
    

    Yield Returns:

    • (#to_s)

      the caption to display in the button.

See Also:



29
30
31
32
33
34
35
36
37
38
# 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(caption, url, options = {}) ⇒ String #button_to(url, options = {}, &block) ⇒ String

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.

Overloads:

  • #button_to(caption, url, options = {}) ⇒ String

    Examples:

    Display a small button to submit to '/create_user'.

    button_to 'Create', '/create_user', size: :small
    

    Options Hash (options):

    • :context (#to_s) — default: :default

      the contextual alternative to apply to the button. Can be :danger, :info, :link, :primary, :success or :warning.

    • :size (#to_s)

      the size of the 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.

  • #button_to(url, options = {}, &block) ⇒ String

    Examples:

    Display a danger button with HTML content to delete a user.

    button_to '/user_destroy', context: :danger do
       :strong, "Delete user"
    end
    

    Yield Returns:

    • (#to_s)

      the caption to display in the button.

See Also:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/bh/helpers/button_to_helper.rb', line 39

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

  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
end

Returns an HTML block to display a dropdown.

Examples:

A right-aligned dropdown with a links.

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

Options Hash (options):

  • :groupable (Boolean) — default: 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).

  • :split (Boolean) — default: false

    if true, creates a split button that only toggles the dropdown when clicked on the rightmost part.

  • :direction (#to_s)

    if set to :up, the dropdown appears above the button, rather than below.

  • :layout (#to_s)

    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.

  • :align (#to_s)

    if set to :right, the dropdown is aligned to the right-end of the button, rather than to the left-end.

  • :context (#to_s) — default: :default

    the context for the button, which determines its color.

  • :size (#to_s)

    the size of the button.

Yield Returns:

  • (#to_s)

    the content of the dropdown.

See Also:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/bh/helpers/dropdown_helper.rb', line 30

def dropdown(caption, options = {}, &block)
  dropdown = Bh::Dropdown.new self, nil, options, &block
  dropdown.extract! :id, :groupable, :direction, :align, :split, :context,
                    :size, :layout, :button

  dropdown.extract_from :button, [:context, :size, :layout]
  dropdown.merge! button: {caption: caption, id: dropdown.id}
  dropdown.append_class_to! :button, :btn
  dropdown.append_class_to! :button, dropdown.context_class
  dropdown.append_class_to! :button, dropdown.size_class
  dropdown.append_class_to! :button, dropdown.layout_class
  dropdown.append_class_to! :div, dropdown.groupable_class
  dropdown.append_class_to! :div, dropdown.direction_class
  dropdown.append_class_to! :ul, :'dropdown-menu'
  dropdown.append_class_to! :ul, dropdown.align_class

  dropdown.render_partial dropdown.partial
end

#font_awesome_css(options = {}) ⇒ String

Returns the URL of the Font Awesome CSS file

Options Hash (options):

  • :version (String)

    the version of Font Awesome.

  • :scheme (String)

    the URI scheme to use.

  • :minified (Boolean)

    whether to use the minified version.

See Also:



32
33
34
# 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 = {}) ⇒ String

Deprecated.

Use #icon instead.

Displays any of the 200 glyphicons available in Bootstrap.

Examples:

Display the "zoom-in" glyphicon

glyphicon :zoom_in

See Also:



14
15
16
# File 'lib/bh/helpers/glyphicon_helper.rb', line 14

def glyphicon(name = nil, options = {})
  icon name, options.merge(library: :glyphicons)
end

#horizontal(content, options = {}) ⇒ String #horizontal(options = {}, &block) ⇒ String

Displays the collapsable portion of a Bootstrap-styled navbar.

Examples:

Display a navbar with two collapsable links.

navbar do
  horizontal do
    nav do
      link_to 'Home', '/'
      link_to 'Profile', '/profile'
    end
  end
end

Overloads:

  • #horizontal(options = {}, &block) ⇒ String

    Yield Returns:

    • (#to_s)

      the collapsable content to display in the navbar.

See Also:



26
27
28
29
30
31
32
33
# 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 = {}) ⇒ String

Displays a Bootstrap-styled vector icon.

Examples:

Display the "fire" font awesome icon with a title

icon 'fire', library: :font_awesome, title: 'Hot'

Options Hash (options):

  • :library (#to_s) — default: :glyphicons

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

See Also:



18
19
20
21
22
23
24
25
# 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

Overrides link_to to display a Bootstrap-styled link. Can only be used in Ruby frameworks that provide the link_to method.

Overloads:

  • #link_to(caption, url, options = {}) ⇒ String

    Examples:

    Display a plain-text link inside an alert-box.

    alert_box do
      link_to 'Check the terms and conditions', '/#terms'
    end
    
  • #button_to(url, options = {}, &block) ⇒ String

    Examples:

    Display a link with HTML inside a dropdown.

    dropdown 'Menu' do
      link_to '/#terms' do
         :strong, 'Check the terms and conditions'
      end
    end
    

    Yield Returns:

    • (#to_s)

      the caption to display in the link.

See Also:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/bh/helpers/link_to_helper.rb', line 37

def link_to(*args, &block)
  link_to = Bh::LinkTo.new self, *args, &block

  link_to.append_class! :'alert-link' if Bh::Stack.find(Bh::AlertBox)
  link_to.append_class! :'navbar-brand' if Bh::Stack.find(Bh::Vertical)
  link_to.merge! role: :menuitem if Bh::Stack.find(Bh::Dropdown)
  link_to.merge! tabindex: -1 if Bh::Stack.find(Bh::Dropdown)
  html = super link_to.content, link_to.url, link_to.attributes, &nil

  if Bh::Stack.find(Bh::Dropdown)
    container = Bh::Base.new(self) { html }
    container.merge! role: :presentation
    container.render_tag :li
  elsif Bh::Stack.find(Bh::Nav)
    container = Bh::Base.new(self) { html }
    container.append_class! :active if link_to.current_page?
    container.render_tag :li
  else
    html
  end
end

Displays a Bootstrap-styled modal.

Overloads:

  • #modal(body, options = {}) ⇒ String

    Examples:

    Display a simple modal toggled by a blue button.

    modal 'You clicked me!', title: 'Click me', button: {context: :info}
    

    Options Hash (options):

    • :title (#to_s) — default: 'Modal'

      the title of the modal.

    • :body (#to_s)

      the content to display as the modal body. Using this option is equivalent to passing the body as an argument.

    • :size (#to_s)

      the size of the modal. Can be :large (alias :lg) or :small (alias :sm).

    • :button (Hash)

      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.
  • #modal(options = {}, &block) ⇒ String

    Examples:

    Display a modal with HTML content.

    modal title: 'Click me' do
       :div, class: 'modal-body' do
         :em, 'You clicked me!'
      end
    end
    

    Yield Returns:

    • (#to_s)

      the content to display in the modal.

See Also:



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/bh/helpers/modal_helper.rb', line 38

def modal(*args, &block)
  modal = Bh::Modal.new self, *args, &block
  modal.extract! :button, :size, :body, :title, :id

  modal.extract_from :button, [:context, :size, :layout, :caption]
  modal.append_class_to! :button, :btn
  modal.append_class_to! :button, modal.button_context_class
  modal.append_class_to! :button, modal.button_size_class
  modal.merge! button: {caption: modal.caption}

  modal.append_class_to! :div, :'modal-dialog'
  modal.append_class_to! :div, modal.dialog_size_class
  modal.merge! div: {title: modal.title, id: modal.id}

  modal.render_partial 'modal'
end

Displays a Bootstrap-styled nav.

Examples:

Display a pills-styled nav with a link.

nav as: :pills do
  link_to 'Home', '/'
end

Options Hash (options):

  • :as (#to_s) — default: :tabs

    the style of the nav. Can be :tabs or :pills.

  • :layout (#to_s)

    the layout of the nav. Can be :justified or :stacked.

Yield Returns:

  • (#to_s)

    the content to display in the nav.

See Also:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/bh/helpers/nav_helper.rb', line 20

def nav(options = {}, &block)
  nav = Bh::Nav.new(self, options, &block)
  nav.extract! :as, :layout

  nav.append_class! :nav
  if Bh::Stack.find(Bh::Navbar)
    nav.append_class! :'navbar-nav'
  else
    nav.merge! role: :tablist
    nav.append_class! nav.style_class
    nav.append_class! nav.layout_class
  end

  nav.render_tag :ul
end

Displays a Bootstrap-styled navbar.

Examples:

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

Options Hash (options):

  • :fluid (Boolean) — default: false

    whether to use a fluid container to surround the navbar content.

  • :inverted (Boolean) — default: false

    whether to use an inverted palette of colors.

  • :position (#to_s)

    the position of the navbar. Can be :top (alias :fixed_top), :bottom (alias :fixed_bottom) or :static (alias :static_top).

  • :padding (#to_s) — default: 70

    if position is set to :top or :bottom, the padding to at the top (or bottom) of to prevent the navbar from overlaying the content.

Yield Returns:

  • (#to_s)

    the content to display in the navbar.

See Also:



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/bh/helpers/navbar_helper.rb', line 35

def navbar(options = {}, &block)
  navbar = Bh::Navbar.new(self, options, &block)
  navbar.extract! :inverted, :position, :padding, :fluid, :id

  navbar.append_class_to! :navigation, :navbar
  navbar.append_class_to! :navigation, navbar.style_class
  navbar.append_class_to! :navigation, navbar.position_class
  navbar.append_class_to! :div, navbar.layout_class
  navbar.prepend_html! navbar.body_padding_style

  navbar.render_partial 'navbar'
end

#panel(body, options = {}) ⇒ String #panel(options = {}, &block) ⇒ String

Displays a Bootstrap-styled panel.

Overloads:

  • #panel(body, options = {}) ⇒ String

    Examples:

    Display an informative panel with plain-text content.

    panel 'You accepted the Terms of service.', context: :success
    

    Options Hash (options):

    • :title (#to_s)

      the text to display as the panel title.

    • :heading (#to_s)

      the text to display as the panel heading.

    • :body (#to_s)

      the text to display as the panel body. Using this option is equivalent to passing the body as an argument.

    • :context (#to_s) — default: #to_s

      (:default) the contextual alternative to apply to the panel heading and border. Can be :danger, :info, :primary, :success or :warning.

    • :tag (#to_s) — default: #to_s

      (:div) the HTML tag to wrap the panel into.

  • #panel(options = {}, &block) ⇒ String

    Examples:

    Display a panel with HTML content.

    panel title: 'Thanks' do
       :div, class: 'panel-body' do
         :em, 'ou accepted the Terms of service.'
      end
    end
    

    Yield Returns:

    • (#to_s)

      the content to display in the panel.

See Also:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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. 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) ⇒ String

Wraps a set of Bootstrap-styled panels in a row.

Examples:

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

Options Hash (options):

  • :column_class (#to_s)

    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.

Yield Returns:

  • (#to_s)

    the panels to display in a row.

See Also:



20
21
22
23
24
25
26
# 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(bar_options = {}, container_options = {}) ⇒ String #progress_bar(stacked_bars_options = [], container_options = {}) ⇒ String

Displays one or more Bootstrap-styled progress bars.

Overloads:

  • #progress_bar(bar_options = {}, container_options = {}) ⇒ String

    Examples:

    Display a 30% warning progress bar.

    progress_bar percentage: 30, context: :warning
    

    Options Hash (bar_options):

    • :label (Boolean, #to_s) — default: 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.

    • :striped (Boolean) — default: false

      whether to display a striped version of the progress bar (rather than solid color).

    • :animated (Boolean) — default: false

      whether to display an animated version of the progress bar (rather than solid color).

    • :context (#to_s) — default: :default

      the contextual alternative to apply to the progress bar. Can be :success, :info, :warning or :danger.

  • #progress_bar(stacked_bars_options = [], container_options = {}) ⇒ String

    Examples:

    Display two stacked progress bars.

    progress_bar [{percentage: 30, context: :warning}, {percentage: 20}]
    

See Also:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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(content, options = {}) ⇒ String #vertical(options = {}, &block) ⇒ String

Displays the non-collapsable portion of a Bootstrap-styled navbar.

Examples:

Display a navbar a non-collapsable links.

navbar do
  vertical do
    link_to 'Home', '/'
  end
end

Overloads:

  • #vertical(options = {}, &block) ⇒ String

    Yield Returns:

    • (#to_s)

      the non-collapsable content to display in the navbar.

See Also:



24
25
26
27
28
29
30
31
# 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