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 = {}) ⇒ Object



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

def alert(type = :default, options = {}, &)
  options[:role] ||= 'alert'
  append_class!(options, "alert alert-#{type}")
  tag.div(**options, &)
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



175
176
177
178
179
# File 'app/helpers/bootstrap_helper.rb', line 175

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

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



99
100
101
# File 'app/helpers/bootstrap_helper.rb', line 99

def bs_column(size = 6, screen = 'md', &)
  tag.div(class: "col-#{screen}-#{size}", &)
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



158
159
160
161
162
163
# File 'app/helpers/bootstrap_helper.rb', line 158

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



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

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_groupObject



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

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


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

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

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



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

def list_panel(type = :default, options = {}, &)
  bs_custom_panel(type, :ul, { class: 'list-group list-group-flush' }, options, &)
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



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

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

rubocop:todo Metrics/MethodLength



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

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

rubocop:enable Metrics/MethodLength



90
91
92
# File 'app/helpers/bootstrap_helper.rb', line 90

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

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



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

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

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



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

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

#progress_bar(count) ⇒ Object

rubocop:todo Metrics/MethodLength



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

def progress_bar(count) # rubocop:todo Metrics/MethodLength
  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



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

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



137
138
139
140
141
142
143
144
145
# File 'app/helpers/bootstrap_helper.rb', line 137

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]</th> <td>Array[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