Module: Kadmin::BootstrapHelper

Defined in:
app/helpers/kadmin/bootstrap_helper.rb

Overview

Collection of Bootstrap helpers

Instance Method Summary collapse

Instance Method Details

#create_custom_label(key, options = {}) ⇒ Object

Custom label generator using bootstrap i.e. * for mendatory feilds, help icon and popover for help

Parameters:

  • key (String)

    identifier for the help label / input field

  • options (Hash) (defaults to: {})

    options for the help label



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/helpers/kadmin/bootstrap_helper.rb', line 49

def create_custom_label(key, options = {})
  defaults = {
    label_for: "#{key}",
    required: false,
    display_help: true,
    title: ".#{key}.title",
    message: ".#{key}.message",
    label_class: 'control-label',
    data_placement: 'top'
  }
  options = defaults.merge(options)

  label = t(".#{key}.label")
  label = label.html_safe

  require_html = options[:required] ? '<span class="required-field"><span>' : ''
  icon_html = "<i class='fa fa-question-circle' style='color:green'></i>"

  display_message = ''
  if options[:display_help]
    message = t(options[:message])
    message = message.gsub('"', '\"')
    message = message.gsub("'", "\'")
    html_message = message.html_safe

    title = t(options[:title])
    title = title.html_safe

    display_message = "<span data-toggle='popover' title='#{title}' data-placement='#{options[:data_placement]}' data-trigger='hover' data-content='#{html_message}'>#{icon_html}</span>"
  end

  return "<label for='#{options[:label_for]}' class='#{options[:label_class]}'>#{require_html}#{label} #{display_message}</label>".html_safe
end

Generates a navigation drop down for bootstrap

Parameters:

  • prompt (String)

    dropdown prompt

  • links (Array<Hash,String>) (defaults to: [])

    list of links to add within the dropdown



34
35
36
37
38
39
40
41
42
43
44
# File 'app/helpers/kadmin/bootstrap_helper.rb', line 34

def dropdown(prompt, links = [])
  button_content = (:span, '', class: 'caret').prepend(prompt)
  button = button_tag(button_content, type: 'button', data: { toggle: 'dropdown' }, class: 'btn btn-sm')
  list = (:ul, '', class: 'dropdown-menu') do
    links.reduce(ActiveSupport::SafeBuffer.new) do |buffer, link|
      buffer + (:li, link)
    end
  end

  return (:div, button + list, class: 'dropdown', style: 'display: inline-block;')
end

#glyphicon(icon) ⇒ Object

Parameters:

  • the (String)

    name of the icon you want

See Also:

  • icons: http://fontawesome.io/icons/


8
9
10
# File 'app/helpers/kadmin/bootstrap_helper.rb', line 8

def glyphicon(icon)
  return "<i class='fa fa-#{icon}' aria-hidden='true'></i>".html_safe
end

#glyphicon_if_else(condition, icon_true, icon_false) ⇒ Object

Parameters:

  • condition (Boolean)

    condition of evaluate

  • icon_true (String)

    the glyphicon to use if true

  • icon_false (String)

    the glyphicon to use if false



15
16
17
# File 'app/helpers/kadmin/bootstrap_helper.rb', line 15

def glyphicon_if_else(condition, icon_true, icon_false)
  return condition ? glyphicon(icon_true) : glyphicon(icon_false)
end

Parameters:

  • url_options (Hash, String)

    anything accepted by link_to and image_tag

  • max_height (Integer)

    maximum height of the thumbnail



21
22
23
24
25
26
27
28
29
# File 'app/helpers/kadmin/bootstrap_helper.rb', line 21

def thumbnail_link(url_options, max_height)
  return link_to(
    image_tag(url_options),
    url_options,
    class: 'thumbnail',
    style: "max-height: #{max_height}px",
    target: 'new'
  )
end