Module: BootstrapHelper

Defined in:
app/helpers/bootstrap_helper.rb

Instance Method Summary collapse

Instance Method Details

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

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

block_content

</div>


28
29
30
31
32
33
# File 'app/helpers/bootstrap_helper.rb', line 28

def alert(type = :default, options = {}, &block)
  options[:class] ||= String.new
  options[:role] ||= 'alert'
  options[:class] << " alert alert-#{type}"
  (:div, options, &block)
end

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


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

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

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


14
15
16
17
18
19
20
21
22
23
# File 'app/helpers/bootstrap_helper.rb', line 14

def bs_custom_panel(type, body_type, body_options, options, &block)
  title = options.delete(:title)
  options[:class] ||= String.new
  options[:class] << " card card-style-#{type} mb-3"
  (:div, options) do
    out = String.new.html_safe
    out << (:h3, title, class: 'card-header-custom') unless title.nil?
    out << (body_type, body_options, &block)
  end
end

#bs_select(*args) ⇒ Object


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

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

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


124
125
126
127
128
129
130
131
# File 'app/helpers/bootstrap_helper.rb', line 124

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

#form_group(&block) ⇒ Object

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


82
83
84
# File 'app/helpers/bootstrap_helper.rb', line 82

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

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

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


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

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') ⇒ Object

<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>


109
110
111
112
113
114
115
# File 'app/helpers/bootstrap_helper.rb', line 109

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

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

<div class=“page-header”>

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

</div>


62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/helpers/bootstrap_helper.rb', line 62

def page_title(title, subtitle = nil, titlecase: true)
  (:div, class: 'page-header') do
    title_class = title.length > 25 ? 'title-long' : 'title-short'
    (:h1, class: title_class) do
      if titlecase
        concat title.titleize
      else
        concat title
      end
      concat ' '
      concat (:span, subtitle, class: 'subtitle') if subtitle.present?
    end
  end
end

#pagination(collection) ⇒ Object


77
78
79
# File 'app/helpers/bootstrap_helper.rb', line 77

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

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


2
3
4
# File 'app/helpers/bootstrap_helper.rb', line 2

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

#progress_bar(count) ⇒ Object


90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/helpers/bootstrap_helper.rb', line 90

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

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


117
118
119
120
121
122
# File 'app/helpers/bootstrap_helper.rb', line 117

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

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

Summary composits 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>

46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/helpers/bootstrap_helper.rb', line 46

def summary(type = :default, options = {})
  options[:title] ||= 'Summary'
  bs_custom_panel(type, :table, { class: 'table table-summary' }, options) do
    String.new.html_safe.tap do |rows|
      yield.each do |key, value|
        rows << (:tr) do
          (:th, key) << (:td, value)
        end
      end
    end
  end
end