Module: EffectiveBootstrapHelper

Defined in:
app/helpers/effective_bootstrap_helper.rb

Overview

Boostrap4 Helpers

Constant Summary collapse

{class: "btn dropdown-toggle dropdown-toggle-split btn-sm btn-outline-primary", type: 'button', 'data-toggle': 'dropdown', 'aria-haspopup': true, 'aria-expanded': false}
{class: "btn dropdown-toggle btn-sm btn-outline-primary", type: 'button', 'data-toggle': 'dropdown', 'aria-haspopup': true, 'aria-expanded': false}
{class: 'btn-group'}
{class: 'btn-group dropleft', role: 'group'}
{class: 'dropdown-menu'}
{class: 'dropdown-menu dropdown-menu-right'}
'btn-sm btn-outline-primary'
"<span class='sr-only'>Toggle Dropdown</span>".html_safe

Instance Method Summary collapse

Instance Method Details

#accordion(options = nil, &block) ⇒ Object

This is a special variant of collapse

accordion do

= collapse('Add thing...') do
  %p Something to add


10
11
12
13
14
15
16
17
18
19
# File 'app/helpers/effective_bootstrap_helper.rb', line 10

def accordion(options = nil, &block)
  (options ||= {})[:class] = "accordion #{options.delete(:class)}".strip

  id = "accordion-#{''.object_id}"

  @_accordion_active = id
  content = (:div, capture(&block), options.merge(id: id))
  @_accordion_active = nil
  content
end

#bootstrap_paginate(collection, per_page:, url: nil, window: 2, collection_count: nil, render_single_page: false) ⇒ Object Also known as: paginate

limit(per_page).offset(offset) }

Add this to your controller: Add this to your view %nav= paginate(@posts, per_page: 10)



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'app/helpers/effective_bootstrap_helper.rb', line 298

def bootstrap_paginate(collection, per_page:, url: nil, window: 2, collection_count: nil, render_single_page: false)
  raise 'expected an ActiveRecord::Relation' unless collection.respond_to?(:limit) && collection.respond_to?(:offset)

  collection_count ||= collection.limit(nil).offset(nil).count # You can pass the total count, or not.

  page = (params[:page] || 1).to_i
  last = (collection_count.to_f / per_page).ceil

  return unless (last > 1 || render_single_page) # If there's only 1 page, don't render a pagination at all.

  # Build URL
  uri = URI(url || request.fullpath)
  params = Rack::Utils.parse_nested_query(uri.query)
  url = uri.path + '?'

  # Pagination Tags
  prev_tag = (:li, class: ['page-item', ('disabled' if page <= 1)].compact.join(' ')) do
    link_to((:span, 'Previous'.html_safe), (page <= 1 ? '#' : url + params.merge('page' => page - 1).to_query),
      class: 'page-link', 'aria-label': 'Previous', title: 'Previous', 'aria-disabled': ('true' if page <= 1), 'tabindex': ('-1' if page <= 1)
    )
  end

  next_tag = (:li, class: ['page-item', ('disabled' if page >= last)].compact.join(' ')) do
    link_to((:span, 'Next'.html_safe), (page >= last ? '#' : url + params.merge('page' => page + 1).to_query),
      class: 'page-link', 'aria-label': 'Next', title: 'Next', 'aria-disabled': ('true' if page >= last), 'tabindex': ('-1' if page >= last)
    )
  end

  dots_tag = (:li, class: 'page-item disabled') do
    link_to('...', '#', class: 'page-link', 'aria-label': '...', 'aria-disabled': true, tabindex: '-1')
  end

  # Calculate Windows
  length = 1 + (window * 2)
  left = 1.upto(last).to_a.first(length)
  right = 1.upto(last).to_a.last(length)
  center = []
  max = length + 2

  if last <= max
    left = left - right
    right = right - left
  elsif left.include?(page + 1)
    right = [last]
    left = left - right
  elsif right.include?(page - 1)
    left = [1]
    right = right - left
  else
    left = [1]
    right = [last]
    center = (page - window + 1).upto(page + window - 1).to_a
  end

  # Render the pagination
  (:ul, class: 'pagination') do
    [
      prev_tag,
      left.map { |index| bootstrap_paginate_tag(index, page, url, params) },
      (dots_tag if last > max && left == [1]),
      center.map { |index| bootstrap_paginate_tag(index, page, url, params) },
      (dots_tag if last > max && right == [last]),
      right.map { |index| bootstrap_paginate_tag(index, page, url, params) },
      next_tag
    ].flatten.join.html_safe
  end
end

#bootstrap_paginate_tag(index, page, url, params) ⇒ Object



366
367
368
369
370
# File 'app/helpers/effective_bootstrap_helper.rb', line 366

def bootstrap_paginate_tag(index, page, url, params)
  (:li, class: ['page-item', ('active' if index == page)].compact.join(' '), title: "Page #{index}") do
    link_to(index, (url + params.merge('page' => index).to_query), class: 'page-link')
  end
end

#card(value = nil, opts = {}, &block) ⇒ Object

getbootstrap.com/docs/4.0/components/card/

card(‘title do’)

%p Stuff

card(‘Stuff’, header: ‘header title’)



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

def card(value = nil, opts = {}, &block)
  raise('expected a block') unless block_given?

  if value.kind_of?(Hash)
    opts = value; value = nil
  end

  header = opts.delete(:header)
  title = opts.delete(:title) || value

  (:div, merge_class_key(opts, 'card mb-4')) do
    header = (:div, header, class: 'card-header') if header.present?

    body = (:div, class: 'card-body') do
      if title.present?
        (:h5, title, class: 'card-title') + capture(&block)
      else
        capture(&block)
      end
    end

    header ? (header + body) : body
  end
end

#collapse(label, opts = {}, &block) ⇒ Object

collapse(items.length, class: ‘btn btn-primary’, class: ‘mt-2’) do

items.map { |item| (:div, item.to_s) }.join.html_safe

end



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/helpers/effective_bootstrap_helper.rb', line 62

def collapse(label, opts = {}, &block)
  raise 'expected a block' unless block_given?

  id = "collapse-#{String.new.object_id}"
  show = (opts.delete(:show) == true)

  link_opts = { 'data-toggle': 'collapse', role: 'button', href: "##{id}", 'aria-controls': "##{id}", 'aria-expanded': show }

  link_opts[:class] = opts.delete(:link_class) || 'btn btn-link'
  div_class = opts.delete(:div_class)
  card_class = opts.delete(:card_class) || 'card card-body my-2'

  if @_accordion_active
    # Accordion collapse
    (:div, class: "card mb-0") do
      (:div, class: "card-header") do
        (:h2, class: "mb-0") do
          (:button, label, link_opts.merge(class: "btn btn-link"))
        end
      end +
      (:div, id: id, class: ['collapse', div_class, ('show' if show)].compact.join(' '), "data-parent": "##{@_accordion_active}") do
        (:div, capture(&block), class: "card-body")
      end
    end
  else
    # Normal collapse
    (:a, label, link_opts) +
    (:div, id: id, class: ['collapse', div_class, ('show' if show)].compact.join(' ')) do
      (:div, capture(&block), class: card_class)
    end
  end
end

#dots(options = nil, &block) ⇒ Object

This is a special variant of dropdown dots do

= dropdown_link_to 'Edit', edit_path(thing)


171
172
173
174
175
176
177
178
# File 'app/helpers/effective_bootstrap_helper.rb', line 171

def dots(options = nil, &block)
  (options ||= {})[:class] = "dropdown dropdown-dots #{options.delete(:class)}".strip

  (:div, options) do
    (:button, class: "btn btn-dots dropdown-toggle #{options.delete(:button_class)}", 'aria-expanded': true, 'aria-haspopup': true, 'data-toggle': 'dropdown', type: 'button') do
    end + (:div, capture(&block), class: 'dropdown-menu')
  end
end


180
181
182
183
184
# File 'app/helpers/effective_bootstrap_helper.rb', line 180

def dots_link_to(label, path, options = {})
  options[:class] = [options[:class], 'dropdown-item'].compact.join(' ')

  concat link_to(label, path, options)
end


119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'app/helpers/effective_bootstrap_helper.rb', line 119

def dropdown(variation: nil, split: true, btn_class: nil, btn_content: nil, right: false, &block)
  raise 'expected a block' unless block_given?

  btn_class ||= DROPDOWN_BTN_CLASS

  # Process all dropdown_link_tos
  @_dropdown_link_tos = []
  @_dropdown_split = split
  @_dropdown_button_class = btn_class
  yield

  return @_dropdown_link_tos.first if @_dropdown_link_tos.length <= 1

  # Build tags
  first = @_dropdown_link_tos.first

  button_opts = (split ? DROPDOWN_SPLIT_OPTS : DROPDOWN_UNSPLIT_OPTS)

  if btn_class != DROPDOWN_BTN_CLASS
    button_opts[:class] = button_opts[:class].sub(DROPDOWN_BTN_CLASS, btn_class)
  end

  button = (:button, button_opts) do
    btn_content || DROPDOWN_TOGGLE_DROPDOWN
  end

  menu_opts = (right ? DROPDOWN_MENU_RIGHT_OPTS : DROPDOWN_MENU_OPTS)

  menu = if split
    (:div, @_dropdown_link_tos[1..-1].join.html_safe, menu_opts)
  else
    (:div, @_dropdown_link_tos.join.html_safe, menu_opts)
  end

  @_dropdown_link_tos = nil

  if split && variation == :dropleft
    (:div, DROPDOWN_DROPLEFT_GROUP_OPTS) do
      (:div, (button + menu), DROPDOWN_DROPLEFT_OPTS) + first.html_safe
    end
  elsif split
    (:div, class: 'btn-group') do
      (:div, (first + button + menu), class: "btn-group #{variation}", role: 'group')
    end
  else
    (:div, (button + menu), class: 'dropdown')
  end
end

Works with dots ao and dropdown do



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

def dropdown_divider(options = {})
  options[:class] = [options[:class], 'dropdown-divider'].compact.join(' ')

  unless @_dropdown_link_tos
    (:div, '', options)
  else
    @_dropdown_link_tos << (:div, '', options)
    nil
  end
end

Works with dots do and dropdown do



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'app/helpers/effective_bootstrap_helper.rb', line 187

def dropdown_link_to(label, path, options = {})
  btn_class = options.delete(:btn_class).presence || @_dropdown_button_class || 'btn-outline-primary'

  unless @_dropdown_link_tos
    options[:class] = (options[:class] ? "dropdown-item #{options[:class]}" : 'dropdown-item')
    return link_to(label, path, options)
  end

  if @_dropdown_link_tos.length == 0 && @_dropdown_split
    options[:class] = (options[:class] ? "btn #{btn_class} #{options[:class]}" : "btn #{btn_class}")
  else
    options[:class] = (options[:class] ? "dropdown-item #{options[:class]}" : 'dropdown-item')
  end

  @_dropdown_link_tos << link_to(label, path, options)

  nil
end

#list_group(&block) ⇒ Object



218
219
220
# File 'app/helpers/effective_bootstrap_helper.rb', line 218

def list_group(&block)
  (:div, yield, class: 'list-group')
end

List group

list_group_link_to

Automatically puts in the ‘active’ class based on request path



225
226
227
228
229
230
231
232
233
234
# File 'app/helpers/effective_bootstrap_helper.rb', line 225

def list_group_link_to(label, path, opts = {})
  # Regular link item
  opts[:class] = if request.fullpath.include?(path)
    [opts[:class], 'list-group-item active'].compact.join(' ')
  else
    [opts[:class], 'list-group-item'].compact.join(' ')
  end

  link_to(label.to_s, path, opts)
end

#merge_class_key(hash, value) ⇒ Object



451
452
453
454
455
456
457
458
459
# File 'app/helpers/effective_bootstrap_helper.rb', line 451

def merge_class_key(hash, value)
  return { :class => value } unless hash.kind_of?(Hash)

  if hash[:class].present?
    hash.merge!(:class => "#{hash[:class]} #{value}")
  else
    hash.merge!(:class => value)
  end
end


273
274
275
# File 'app/helpers/effective_bootstrap_helper.rb', line 273

def nav_divider
  (:div, '', class: 'dropdown-divider')
end


259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'app/helpers/effective_bootstrap_helper.rb', line 259

def nav_dropdown(label, right: false, link_class: [], list_class: [], &block)
  raise 'expected a block' unless block_given?

  id = "dropdown-#{''.object_id}"

  (:li, class: 'nav-item dropdown') do
    (:a, class: 'nav-link dropdown-toggle', href: '#', id: id, role: 'button', 'data-toggle': 'dropdown', 'aria-haspopup': true, 'aria-expanded': false) do
      label.html_safe
    end + (:div, class: (right ? 'dropdown-menu dropdown-menu-right' : 'dropdown-menu'), 'aria-labelledby': id) do
      @_nav_mode = :dropdown; yield; @_nav_mode = nil
    end
  end
end

%ul.navbar-nav

= nav_link_to 'Sign In', new_user_session_path
= nav_dropdown 'Settings' do
  = nav_link_to 'Account Settings', user_settings_path
  = nav_divider
  = nav_link_to 'Sign In', new_user_session_path, method: :delete


245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'app/helpers/effective_bootstrap_helper.rb', line 245

def nav_link_to(label, path, opts = {})
  if @_nav_mode == :dropdown  # We insert dropdown-items
    return link_to(label, path, merge_class_key(opts, 'dropdown-item'))
  end

  strict = opts.delete(:strict)
  active = (strict ? (request.fullpath == path) : request.fullpath.include?(path))

  # Regular nav link item
  (:li, class: (active ? 'nav-item active' : 'nav-item')) do
    link_to(label, path, merge_class_key(opts, 'nav-link'))
  end
end

#tab(label, options = {}, &block) ⇒ Object



405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# File 'app/helpers/effective_bootstrap_helper.rb', line 405

def tab(label, options = {}, &block)
  controls = options.delete(:controls) || label.to_s.parameterize.gsub('_', '-')
  controls = controls[1..-1] if controls[0] == '#'
  controls = "#{controls}-#{@_tab_unique}" if @_tab_unique

  active = (@_tab_active == :first || @_tab_active == label)

  @_tab_active = nil if @_tab_active == :first

  if @_tab_mode == :tablist_vertical
    (:a, label, id: ('tab-' + controls), class: ['nav-link', ('active' if active)].compact.join(' '), href: '#' + controls, 'aria-controls': controls, 'aria-selected': active.to_s, 'data-toggle': 'tab', role: 'tab')
  elsif @_tab_mode == :tablist # Inserting the label into the tablist top
    (:li, class: 'nav-item') do
      (:a, label, id: ('tab-' + controls), class: ['nav-link', ('active' if active)].compact.join(' '), href: '#' + controls, 'aria-controls': controls, 'aria-selected': active.to_s, 'data-toggle': 'tab', role: 'tab')
    end
  else # Inserting the content into the tab itself
    classes = ['tab-pane', 'fade', ('show active' if active), options[:class].presence].compact.join(' ')
    (:div, id: controls, class: classes, role: 'tabpanel', 'aria-labelledby': ('tab-' + controls)) do
      yield
    end
  end
end

#tabs(active: nil, unique: false, list: {}, content: {}, &block) ⇒ Object

If you pass active ‘label’ it will make that tab active. Otherwise first. Unique will make sure the tab html IDs are unique $(‘#tab-demographics’).tab(‘show’)



388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'app/helpers/effective_bootstrap_helper.rb', line 388

def tabs(active: nil, unique: false, list: {}, content: {}, &block)
  raise 'expected a block' unless block_given?

  @_tab_mode = :tablist
  @_tab_active = (active || :first)
  @_tab_unique = ''.object_id if unique

  (:ul, {class: 'nav nav-tabs', role: 'tablist'}.merge(list)) do
    yield # Yield to tab the first time
  end +
  (:div, {class: 'tab-content'}.merge(content)) do
    @_tab_mode = :content
    @_tab_active = (active || :first)
    yield # Yield to tab the second time
  end
end

#vertical_tabs(active: nil, unique: false, list: {}, content: {}, &block) ⇒ Object



428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# File 'app/helpers/effective_bootstrap_helper.rb', line 428

def vertical_tabs(active: nil, unique: false, list: {}, content: {}, &block)
  raise 'expected a block' unless block_given?

  @_tab_mode = :tablist_vertical
  @_tab_active = (active || :first)
  @_tab_unique = ''.object_id if unique

  (:div, class: 'row border') do
    (:div, class: 'col-3 border-right') do
      (:div, {class: 'nav flex-column nav-pills my-2', role: 'tablist', 'aria-orientation': :vertical}.merge(list)) do
        yield # Yield to tab the first time
      end
    end +
    (:div, class: 'col-9') do
      (:div, {class: 'tab-content my-2'}.merge(content)) do
        @_tab_mode = :content
        @_tab_active = (active || :first)
        yield # Yield to tab the second time
      end
    end
  end
end