Module: EffectiveBootstrapHelper
- Defined in:
- app/helpers/effective_bootstrap_helper.rb
Overview
Boostrap4 Helpers
Instance Method Summary collapse
-
#accordion(options = nil, &block) ⇒ Object
This is a special variant of collapse = accordion do = collapse(‘Add thing…’) do %p Something to add.
-
#bootstrap_paginate(collection, per_page:, url: nil, window: 2, collection_count: nil, render_single_page: false) ⇒ Object
(also: #paginate)
limit(per_page).offset(offset) }.
- #bootstrap_paginate_tag(index, page, url, params) ⇒ Object
-
#collapse(label, opts = {}, &block) ⇒ Object
collapse(items.length, class: ‘btn btn-primary’, class: ‘mt-2’) do items.map { |item| content_tag(:div, item.to_s) }.join.html_safe end.
-
#dots(options = nil, &block) ⇒ Object
This is a special variant of dropdown dots do = dropdown_link_to ‘Edit’, edit_path(thing).
- #dots_link_to(label, path, options = {}) ⇒ Object
-
#dropdown(variation: nil, split: true, btn_class: nil, right: false, &block) ⇒ Object
Button Dropdowns getbootstrap.com/docs/4.0/components/dropdowns/.
-
#dropdown_divider ⇒ Object
Works with dots ao and dropdown do.
-
#dropdown_link_to(label, path, options = {}) ⇒ Object
Works with dots do and dropdown do.
- #list_group(&block) ⇒ Object
-
#list_group_link_to(label, path, opts = {}) ⇒ Object
List group = list_group_link_to Automatically puts in the ‘active’ class based on request path.
- #merge_class_key(hash, value) ⇒ Object
- #nav_divider ⇒ Object
- #nav_dropdown(label, right: false, link_class: [], list_class: [], &block) ⇒ Object
-
#nav_link_to(label, path, opts = {}) ⇒ Object
%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.
- #tab(label, options = {}, &block) ⇒ Object
-
#tabs(active: nil, unique: false, list: {}, content: {}, &block) ⇒ Object
If you pass active ‘label’ it will make that tab active.
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
8 9 10 11 12 13 14 15 16 17 |
# File 'app/helpers/effective_bootstrap_helper.rb', line 8 def accordion( = nil, &block) ( ||= {})[:class] = "accordion #{.delete(:class)}".strip id = "accordion-#{''.object_id}" @_accordion_active = id content = content_tag(:div, capture(&block), .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)
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'app/helpers/effective_bootstrap_helper.rb', line 233 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 = content_tag(:li, class: ['page-item', ('disabled' if page <= 1)].compact.join(' ')) do link_to(content_tag(: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 = content_tag(:li, class: ['page-item', ('disabled' if page >= last)].compact.join(' ')) do link_to(content_tag(: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 = content_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 content_tag(: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
301 302 303 304 305 |
# File 'app/helpers/effective_bootstrap_helper.rb', line 301 def bootstrap_paginate_tag(index, page, url, params) content_tag(: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 |
#collapse(label, opts = {}, &block) ⇒ Object
collapse(items.length, class: ‘btn btn-primary’, class: ‘mt-2’) do
items.map { |item| content_tag(:div, item.to_s) }.join.html_safe
end
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'app/helpers/effective_bootstrap_helper.rb', line 30 def collapse(label, opts = {}, &block) raise 'expected a block' unless block_given? id = "collapse-#{''.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) || 'my-2' if @_accordion_active # Accordion collapse content_tag(:div, class: "card mb-0") do content_tag(:div, class: "card-header") do content_tag(:h2, class: "mb-0") do content_tag(:button, label, link_opts.merge(class: "btn btn-link")) end end + content_tag(:div, id: id, class: ['collapse', div_class, ('show' if show)].compact.join(' '), "data-parent": "##{@_accordion_active}") do content_tag(:div, capture(&block), class: "card-body") end end else # Normal collapse content_tag(:a, label, link_opts) + content_tag(:div, id: id, class: ['collapse', div_class, ('show' if show)].compact.join(' ')) do content_tag(:div, capture(&block), class: ['card', 'card-body', card_class].compact.join(' ')) end end end |
#dots(options = nil, &block) ⇒ Object
This is a special variant of dropdown dots do
= dropdown_link_to 'Edit', edit_path(thing)
108 109 110 111 112 113 114 115 |
# File 'app/helpers/effective_bootstrap_helper.rb', line 108 def dots( = nil, &block) ( ||= {})[:class] = "dropdown dropdown-dots #{.delete(:class)}".strip content_tag(:span, ) do content_tag(:button, class: "btn btn-dots dropdown-toggle #{.delete(:button_class)}", 'aria-expanded': true, 'aria-haspopup': true, 'data-toggle': 'dropdown', type: 'button') do end + content_tag(:div, capture(&block), class: 'dropdown-menu') end end |
#dots_link_to(label, path, options = {}) ⇒ Object
117 118 119 120 121 |
# File 'app/helpers/effective_bootstrap_helper.rb', line 117 def dots_link_to(label, path, = {}) [:class] = [[:class], 'dropdown-item'].compact.join(' ') concat link_to(label, path, ) end |
#dropdown(variation: nil, split: true, btn_class: nil, right: false, &block) ⇒ Object
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
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'app/helpers/effective_bootstrap_helper.rb', line 75 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 = content_tag(:div, @_dropdown_link_tos[1..-1].join.html_safe, class: ['dropdown-menu', ('dropdown-menu-right' if right)].compact.join(' ')) split = content_tag(:button, class: "btn #{btn_class} dropdown-toggle dropdown-toggle-split", type: 'button', 'data-toggle': 'dropdown', 'aria-haspopup': true, 'aria-expanded': false) do content_tag(:span, 'Toggle Dropdown', class: 'sr-only') end content_tag(:div, class: 'btn-group') do content_tag(:div, class: ['btn-group', variation.to_s.presence].compact.join(' '), role: 'group') do [:dropleft].include?(variation) ? (split + ) : (first + split + ) end + ([:dropleft].include?(variation) ? first : '').html_safe end else raise 'split false is unsupported' end @_dropdown_link_tos = nil retval end |
#dropdown_divider ⇒ Object
Works with dots ao and dropdown do
144 145 146 147 148 149 150 151 |
# File 'app/helpers/effective_bootstrap_helper.rb', line 144 def dropdown_divider unless @_dropdown_link_tos content_tag(:div, '', class: 'dropdown-divider') else @_dropdown_link_tos << content_tag(:div, '', class: 'dropdown-divider') nil end end |
#dropdown_link_to(label, path, options = {}) ⇒ Object
Works with dots do and dropdown do
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'app/helpers/effective_bootstrap_helper.rb', line 124 def dropdown_link_to(label, path, = {}) btn_class = .delete(:btn_class).presence || 'btn-outline-primary' unless @_dropdown_link_tos [:class] = [[:class], 'dropdown-item'].compact.join(' ') return link_to(label, path, ) end if @_dropdown_link_tos.length == 0 [:class] = [[:class], 'btn', btn_class].compact.join(' ') else [:class] = [[:class], 'dropdown-item'].compact.join(' ') end @_dropdown_link_tos << link_to(label, path, ) nil end |
#list_group(&block) ⇒ Object
153 154 155 |
# File 'app/helpers/effective_bootstrap_helper.rb', line 153 def list_group(&block) content_tag(:div, yield, class: 'list-group') end |
#list_group_link_to(label, path, opts = {}) ⇒ Object
List group
list_group_link_to
Automatically puts in the ‘active’ class based on request path
160 161 162 163 164 165 166 167 168 169 |
# File 'app/helpers/effective_bootstrap_helper.rb', line 160 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
361 362 363 364 365 366 367 368 369 |
# File 'app/helpers/effective_bootstrap_helper.rb', line 361 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 |
#nav_divider ⇒ Object
208 209 210 |
# File 'app/helpers/effective_bootstrap_helper.rb', line 208 def nav_divider content_tag(:div, '', class: 'dropdown-divider') end |
#nav_dropdown(label, right: false, link_class: [], list_class: [], &block) ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'app/helpers/effective_bootstrap_helper.rb', line 194 def nav_dropdown(label, right: false, link_class: [], list_class: [], &block) raise 'expected a block' unless block_given? id = "dropdown-#{''.object_id}" content_tag(:li, class: 'nav-item dropdown') do content_tag(: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 + content_tag(:div, class: (right ? 'dropdown-menu dropdown-menu-right' : 'dropdown-menu'), 'aria-labelledby': id) do @_nav_mode = :dropdown; yield; @_nav_mode = nil end end end |
#nav_link_to(label, path, opts = {}) ⇒ Object
%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
180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'app/helpers/effective_bootstrap_helper.rb', line 180 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 content_tag(: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
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
# File 'app/helpers/effective_bootstrap_helper.rb', line 340 def tab(label, = {}, &block) controls = .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 content_tag(:li, class: 'nav-item') do content_tag(: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), [:class].presence].compact.join(' ') content_tag(: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’)
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
# File 'app/helpers/effective_bootstrap_helper.rb', line 323 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 content_tag(:ul, {class: 'nav nav-tabs', role: 'tablist'}.merge(list)) do yield # Yield to tab the first time end + content_tag(:div, {class: 'tab-content'}.merge(content)) do @_tab_mode = :content @_tab_active = (active || :first) yield # Yield to tab the second time end end |