Module: BootstrapHelper

Defined in:
app/helpers/bootstrap_helper.rb

Instance Method Summary collapse

Instance Method Details

#back_button(label = nil, options = nil, html_options = {}) ⇒ Object

Generates a link back button.



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

def back_button(label = nil, options = nil, html_options = {})
  button_options = { icon: 'reply' }
  html_options = button_options.update html_options

  link_button(label, options, html_options)
end

#bs_icon(icon, html_options = {}) ⇒ Object

Generates an icon.



8
9
10
11
12
13
14
15
16
17
18
# File 'app/helpers/bootstrap_helper.rb', line 8

def bs_icon(icon, html_options = {})
  html_options = html_options.symbolize_keys
  classes = ['glyphicon']
  classes << "glyphicon-#{icon}"
  if html_options[:class]
    classes << html_options[:class]
  end
  html_options[:class] = classes * ' ' if classes.present?

  (:span, nil, html_options)
end

#button(label = nil, html_options = nil, &block) ⇒ Object

Generates a button.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'app/helpers/bootstrap_helper.rb', line 103

def button(label = nil, html_options = nil, &block)
  html_options, label = label, capture(&block) if block_given?

  html_options ||= {}
  html_options = html_options.symbolize_keys
  html_options = normalize_button_options_for_bootstrap(html_options, :button)

  html_options[:type] ||= 'button'

  if (label_hidden = html_options.delete(:label_hidden)) && !block_given?
    label =  :span, label, class: "hidden-#{label_hidden}"
  end

  if (icon = html_options.delete(:icon)) && !block_given?
    label = fa_icon(icon, text: label)
  end

  if html_options[:confirm]
    html_options[:'data-confirm'] = html_options.delete(:confirm)
  end

  if html_options[:disable_with]
    html_options[:'data-disable-with'] = html_options.delete(:disable_with)
  end

  button_tag(label, html_options)
end

#cancel_button(label = nil, options = nil, html_options = {}) ⇒ Object

Generates a link cancel button.



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

def cancel_button(label = nil, options = nil, html_options = {})
  button_options = { icon: 'ban' }
  html_options = button_options.update html_options

  link_button(label, options, html_options)
end

#content_tag(name, content_or_options_with_block = nil, options = nil, escape = true, &block) ⇒ Object

———— #

  • OVERRIDE - #

———— #



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'app/helpers/bootstrap_helper.rb', line 266

def (name, content_or_options_with_block = nil, options = nil, escape = true, &block)
  if block_given?
    if content_or_options_with_block.is_a?(Hash) || content_or_options_with_block.nil?
      options = content_or_options_with_block
      options ||= {}
      options = options.symbolize_keys
      options = normalize_options_for_typography(options)
    end
    (name, capture(&block), options, escape)
  else
    options ||= {}
    options = options.symbolize_keys
    options = normalize_options_for_typography(options)
    (name, content_or_options_with_block, options, escape)
  end
end

#destroy_button(label = nil, options = nil, html_options = {}) ⇒ Object

Generates a link destroy button.



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

def destroy_button(label = nil, options = nil, html_options = {})
  button_options = { icon: 'trash', color: 'danger', method: :delete, confirm: 'Are you sure?' }
  html_options = button_options.update html_options

  link_button(label, options, html_options)
end

#edit_button(label = nil, options = nil, html_options = {}) ⇒ Object

Generates a link edit button.



67
68
69
70
71
72
# File 'app/helpers/bootstrap_helper.rb', line 67

def edit_button(label = nil, options = nil, html_options = {})
  button_options = { icon: 'edit', color: 'warning' }
  html_options = button_options.update html_options

  link_button(label, options, html_options)
end

#image_circle(source, html_options = {}) ⇒ Object

Generates an image tag with circle.



235
236
237
238
239
240
241
242
243
244
245
246
# File 'app/helpers/bootstrap_helper.rb', line 235

def image_circle(source, html_options = {})
  html_options = html_options.symbolize_keys

  classes = ['img-circle']
  if html_options[:class]
    classes << html_options[:class]
  end

  html_options[:class] = classes * ' ' if classes.present?

  image_tag(source, html_options)
end

#image_responsive(source, html_options = {}) ⇒ Object

Generates a responsive-friendly image tag



207
208
209
210
211
212
213
214
215
216
217
218
# File 'app/helpers/bootstrap_helper.rb', line 207

def image_responsive(source, html_options = {})
  html_options = html_options.symbolize_keys

  classes = ['img-responsive']
  if html_options[:class]
    classes << html_options[:class]
  end

  html_options[:class] = classes * ' ' if classes.present?

  image_tag(source, html_options)
end

#image_rounded(source, html_options = {}) ⇒ Object

Generates an image tag with rounded corners.



221
222
223
224
225
226
227
228
229
230
231
232
# File 'app/helpers/bootstrap_helper.rb', line 221

def image_rounded(source, html_options = {})
  html_options = html_options.symbolize_keys

  classes = ['img-rounded']
  if html_options[:class]
    classes << html_options[:class]
  end

  html_options[:class] = classes * ' ' if classes.present?

  image_tag(source, html_options)
end

#image_thumbnail(source, html_options = {}) ⇒ Object

Generates an image tag within thumbnail.



249
250
251
252
253
254
255
256
257
258
259
260
# File 'app/helpers/bootstrap_helper.rb', line 249

def image_thumbnail(source, html_options = {})
  html_options = html_options.symbolize_keys

  classes = ['img-thumbnail']
  if html_options[:class]
    classes << html_options[:class]
  end

  html_options[:class] = classes * ' ' if classes.present?

  image_tag(source, html_options)
end

Generates a link button.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/helpers/bootstrap_helper.rb', line 25

def link_button(label = nil, options = nil, html_options = nil, &block)
  html_options, options, label = options, label, capture(&block) if block_given?

  html_options ||= {}
  html_options = html_options.symbolize_keys
  html_options = normalize_button_options_for_bootstrap(html_options)

  if (label_hidden = html_options.delete(:label_hidden)) && !block_given?
    label =  :span, label, class: "hidden-#{label_hidden}"
  end

  if (icon = html_options.delete(:icon)) && !block_given?
    label = fa_icon(icon, text: label)
  end

  if html_options[:confirm]
    html_options[:'data-confirm'] = html_options.delete(:confirm)
  end

  if html_options[:disable_with]
    html_options[:'data-disable-with'] = html_options.delete(:disable_with)
  end

  link_to(label, options, html_options)
end

#new_button(label = nil, options = nil, html_options = {}) ⇒ Object



51
52
53
54
55
56
# File 'app/helpers/bootstrap_helper.rb', line 51

def new_button(label = nil, options = nil, html_options = {})
  button_options = { icon: 'plus', color: 'primary' }
  html_options = button_options.update html_options

  link_button(label, options, html_options)
end

#radio_buttons_group(html_options = nil) {|radios| ... } ⇒ Object

Generates a radio buttons group

Yields:

  • (radios)


148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'app/helpers/bootstrap_helper.rb', line 148

def radio_buttons_group(html_options = nil, &block)
  # Set html_options
  html_options ||= {}
  html_options['data-toggle'] = 'buttons'
  html_options = html_options.symbolize_keys

  # Set html_options class
  classes = ['btn-group']
  classes << html_options[:class] if html_options[:class]
  html_options[:class] = classes * ' ' if classes.present?

  # Set radios options
  radios = []
  yield radios

  # Set radio input name
  name = html_options.delete(:name)

  # Set radio_items
  radio_items = []
  radios.each do |radio|
    radio_html_options = radio.symbolize_keys

    label = radio_html_options.delete(:label)

    if label_hidden = radio_html_options.delete(:label_hidden)
      label =  :span, label, class: "hidden-#{label_hidden}"
    end

    if icon = radio_html_options.delete(:icon)
      label = fa_icon(icon, text: label)
    end

    value       = radio_html_options.delete(:value)
    radio_input = radio_button_tag(name, value, radio_html_options[:active])

    classes = ['btn', 'btn-default']
    classes << radio_html_options[:class] if radio_html_options[:class]
    classes << 'active' if radio_html_options.delete(:active)
    radio_html_options[:class] = classes * ' ' if classes.present?

    radio_item = label_tag value, radio_html_options do
      radio_input + ' ' + label
    end

    radio_items << radio_item
  end

  # Set output
   :div, html_options do
    (radio_items * '').html_safe
  end
end

#reset_button(label = nil, html_options = {}) ⇒ Object

Generates a reset button.



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

def reset_button(label = nil, html_options = {})
  button_options = { icon: 'eraser', name: 'reset', type: 'reset' }
  html_options = button_options.update html_options

  button(label, html_options)
end

#show_button(label = nil, options = nil, html_options = {}) ⇒ Object

Generates a link show button.



59
60
61
62
63
64
# File 'app/helpers/bootstrap_helper.rb', line 59

def show_button(label = nil, options = nil, html_options = {})
  button_options = { icon: 'search', color: 'info' }
  html_options = button_options.update html_options

  link_button(label, options, html_options)
end

#submit_button(label = nil, html_options = {}) ⇒ Object

Generates a submit button.



132
133
134
135
136
137
# File 'app/helpers/bootstrap_helper.rb', line 132

def submit_button(label = nil, html_options = {})
  button_options = { icon: 'check', color: 'primary', name: 'submit', type: 'submit' }
  html_options = button_options.update html_options

  button(label, html_options)
end