Module: EffectiveBootstrapHelper

Defined in:
app/helpers/effective_bootstrap_helper.rb

Overview

Boostrap4 Helpers

Instance Method Summary collapse

Instance Method Details

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

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

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

end



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

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

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

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

  (:a, label, link_opts.merge(opts)) +
  (:div, class: 'collapse', id: id) do
    (:div, capture(&block), class: 'card card-body mt-2')
  end
end

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

This is a special variant of dropdown dots do

= dropdown_link_to 'Edit', edit_path(thing)


71
72
73
74
75
76
77
78
# File 'app/helpers/effective_bootstrap_helper.rb', line 71

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

  (:span, 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


80
81
82
83
84
# File 'app/helpers/effective_bootstrap_helper.rb', line 80

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

  concat link_to(label, path, options)
end

Button Dropdowns getbootstrap.com/docs/4.0/components/dropdowns/

dropdown do

= dropdown_link_to 'Something', root_path
= dropdown_divider
= dropdown_link_to 'Another', root_path

Button Dropdowns variations can be :dropup, :dropleft, :dropright split can be true, false right is to right align things



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/helpers/effective_bootstrap_helper.rb', line 38

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

  btn_class = btn_class.presence || 'btn-outline-primary'

  @_dropdown_link_tos = []; yield

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

  retval = if split
    first = @_dropdown_link_tos.first
    menu = (:div, @_dropdown_link_tos[1..-1].join.html_safe, class: ['dropdown-menu', ('dropdown-menu-right' if right)].compact.join(' '))
    split = (:button, class: "btn #{btn_class} dropdown-toggle dropdown-toggle-split", type: 'button', 'data-toggle': 'dropdown', 'aria-haspopup': true, 'aria-expanded': false) do
      (:span, 'Toggle Dropdown', class: 'sr-only')
    end

    (:div, class: 'btn-group') do
      (:div, class: ['btn-group', variation.to_s.presence].compact.join(' '), role: 'group') do
        [:dropleft].include?(variation) ? (split + menu) : (first + split + menu)
      end + ([:dropleft].include?(variation) ? first : '').html_safe
    end
  else
    raise 'split false is unsupported'
  end

  @_dropdown_link_tos = nil

  retval
end

Works with dots ao and dropdown do



107
108
109
110
111
112
113
114
# File 'app/helpers/effective_bootstrap_helper.rb', line 107

def dropdown_divider
  unless @_dropdown_link_tos
    (:div, '', class: 'dropdown-divider')
  else
    @_dropdown_link_tos << (:div, '', class: 'dropdown-divider')
    nil
  end
end

Works with dots do and dropdown do



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/helpers/effective_bootstrap_helper.rb', line 87

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

  unless @_dropdown_link_tos
    options[:class] = [options[:class], 'dropdown-item'].compact.join(' ')
    return link_to(label, path, options)
  end

  if @_dropdown_link_tos.length == 0
    options[:class] = [options[:class], 'btn', btn_class].compact.join(' ')
  else
    options[:class] = [options[:class], 'dropdown-item'].compact.join(' ')
  end

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

  nil
end

#list_group(&block) ⇒ Object



116
117
118
# File 'app/helpers/effective_bootstrap_helper.rb', line 116

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



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

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



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

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


171
172
173
# File 'app/helpers/effective_bootstrap_helper.rb', line 171

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


157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'app/helpers/effective_bootstrap_helper.rb', line 157

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', 
  = nav_divider
  = nav_link_to 'Sign In', new_user_session_path, method: :delete


143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'app/helpers/effective_bootstrap_helper.rb', line 143

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



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'app/helpers/effective_bootstrap_helper.rb', line 205

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 # 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’)



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

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