Module: BootstrapHelper

Defined in:
app/helpers/bootstrap_helper.rb

Overview

A collection of view helpers to assist with rendering bootstrap components

Instance Method Summary collapse

Instance Method Details

#alert(type = :default, options = {}, &block) ⇒ Object

<div class=“alert alert-warning” role=“alert”>

block_content

</div>


34
35
36
37
38
# File 'app/helpers/bootstrap_helper.rb', line 34

def alert(type = :default, options = {}, &block)
  options[:role] ||= 'alert'
  append_class!(options, "alert alert-#{type}")
  tag.div(options, &block)
end

#append_class!(options, klass) ⇒ Hash

Mutates the input html_options hash to add klass to the css classes while maintaining any existing classes

Parameters:

  • options (Hash)

    Hash of HTML options for the rails form renderer

  • klass (String)

    A css class to add to the :class key

Returns:

  • (Hash)

    The HTML options hash. @note The original hash is mutated, the return value is provided for method chaining


171
172
173
174
175
# File 'app/helpers/bootstrap_helper.rb', line 171

def append_class!(options, klass)
  options[:class] = Array(options[:class])
  options[:class] << klass
  options
end

#bs_column(size = 6, screen = 'md', &block) ⇒ Object


96
97
98
# File 'app/helpers/bootstrap_helper.rb', line 96

def bs_column(size = 6, screen = 'md', &block)
  tag.div(class: "col-#{screen}-#{size}", &block)
end

#bs_custom_panel(type, body_type, body_options, options, &block) ⇒ Object


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

def bs_custom_panel(type, body_type, body_options, options, &block)
  title = options.delete(:title)
  append_class!(options, "ss-card card-style-#{type}")
  tag.div(options) do
    concat tag.h3(title, class: 'card-header-custom') unless title.nil?
    concat (body_type, body_options, &block)
  end
end

#bs_select(*args) ⇒ Object


154
155
156
157
158
159
# File 'app/helpers/bootstrap_helper.rb', line 154

def bs_select(*args)
  hashes = args.last(2).count { |arg| arg.respond_to?(:keys) }
  (2 - hashes).times { args << {} }
  append_class!(args.last, 'custom-select')
  select(*args)
end

#form_collection(label, field, help = nil) ⇒ Object


150
151
152
# File 'app/helpers/bootstrap_helper.rb', line 150

def form_collection(label, field, help = nil)
  form_group { bs_column(2, 'md') { label } << bs_column(10, 'md') { field << help_text { raw(help) } } }
end

#form_group(&block) ⇒ Object

<div class=“col-md-size form-group sqs-form”></div>


92
93
94
# File 'app/helpers/bootstrap_helper.rb', line 92

def form_group(&block)
  tag.div(class: 'form-group row sqs-form', &block)
end

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

def link_panel(type = :default, options = {}, &block)
  bs_custom_panel(type, :div, { class: 'link-panel' }, options, &block)
end

#list_panel(type = :default, options = {}, &block) ⇒ Object


10
11
12
# File 'app/helpers/bootstrap_helper.rb', line 10

def list_panel(type = :default, options = {}, &block)
  bs_custom_panel(type, :ul, { class: 'list-group list-group-flush' }, options, &block)
end

#loading_bar(id = 'update_loader', show: false, text: 'Loading') ⇒ Object

rubocop:disable Layout/LineLength <div class=“progress”>

<div class="progress-bar progress-bar-striped active" role="progressbar" aria-valuenow="45" aria-valuemin="0" aria-valuemax="100" style="width: 45%">
  <span class="sr-only">45% Complete</span>
</div>

</div> rubocop:enable Layout/LineLength


127
128
129
130
131
132
133
# File 'app/helpers/bootstrap_helper.rb', line 127

def loading_bar(id = 'update_loader', show: false, text: 'Loading')
  tag.div(class: 'loading-bar-placeholder') do
    tag.div(id: id, class: 'loading-bar-container', style: show ? '' : 'display: none;') do
      tag.div(text, class: 'loading-bar', role: 'progressbar')
    end
  end
end

#page_title(title, subtitle = nil, titlecase: true, badges: []) ⇒ Object

<div class=“page-header”>

<h1>Title <small>subtitle</small></h1>

</div>


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/helpers/bootstrap_helper.rb', line 68

def page_title(title, subtitle = nil, titlecase: true, badges: []) # rubocop:todo Metrics/AbcSize
  tag.div(class: 'page-header') do
    title_class = title.length > 25 ? 'title-long' : 'title-short'
    tag.h1(class: title_class) do
      if titlecase
        concat title.titleize
      else
        concat title
      end
      concat ' '
      concat tag.span(subtitle, class: 'subtitle') if subtitle.present?
      badges.each do |badge_text|
        concat ' '
        concat badge(badge_text, type: 'title-badge')
      end
    end
  end
end

#pagination(collection) ⇒ Object


87
88
89
# File 'app/helpers/bootstrap_helper.rb', line 87

def pagination(collection)
  will_paginate collection, renderer: BootstrapPagination::Rails, previous_label: '&laquo;', next_label: '&raquo;'
end

#panel(type = :default, options = {}, &block) ⇒ Object


6
7
8
# File 'app/helpers/bootstrap_helper.rb', line 6

def panel(type = :default, options = {}, &block)
  bs_custom_panel(type, :div, { class: 'card-body' }, options, &block)
end

#panel_no_body(type = :default, options = {}, &block) ⇒ Object


18
19
20
# File 'app/helpers/bootstrap_helper.rb', line 18

def panel_no_body(type = :default, options = {}, &block)
  bs_custom_panel(type, :div, {}, options, &block)
end

#progress_bar(count) ⇒ Object


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'app/helpers/bootstrap_helper.rb', line 100

def progress_bar(count)
  css_class =
    if count < 25
      'bg-danger'
    elsif count > 99
      'bg-success'
    else
      'bg-warning'
    end
  tag.span(count, style: 'display:none') <<
    tag.div(class: 'progress') do
      tag.div(
        "#{count}%",
        class: ['progress-bar', 'progress-bar-striped', css_class],
        role: 'progressbar',
        style: "width: #{count}%;"
      )
    end
end

#render_radio_section(_form, _field_name, sections, field) ⇒ Object


143
144
145
146
147
148
# File 'app/helpers/bootstrap_helper.rb', line 143

def render_radio_section(_form, _field_name, sections, field)
  label =
    tag.label(sections.label, sections.label_options) << tag.span(sections.edit_info, class: 'property_edit_info')
  help = sections.help
  tag.legend(sections.label, class: 'sr-only') << form_collection(label, field, help)
end

#render_section(form, field_name, sections, field) ⇒ Object


135
136
137
138
139
140
141
# File 'app/helpers/bootstrap_helper.rb', line 135

def render_section(form, field_name, sections, field)
  label =
    form.label(field_name, sections.label, sections.label_options) <<
      tag.span(sections.edit_info, class: 'property_edit_info')
  help = sections.help
  form_collection(label, field, help)
end

#summary(type = :default, options = {}) ⇒ Object

Summary composites a panel with a table to deliver a list of key-value pairs

<div class="card card-default">
  <h3 class="card-header">Summary</h3>
  <table class='table table-summary'>
    <tr>
      <th>Array[0][0]</th>
      <td>Array[0][1]</td>
    </tr>
  </table>
</div>

51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/helpers/bootstrap_helper.rb', line 51

def summary(type = :default, options = {}) # rubocop:todo Metrics/MethodLength
  options[:title] ||= 'Summary'
  bs_custom_panel(type, :table, { class: 'table table-summary' }, options) do
    yield.each do |key, value|
      concat(
        tag.tr do
          concat tag.th(key)
          concat tag.td(value)
        end
      )
    end
  end
end