Module: EffectiveBootstrapHelper
- Defined in:
- app/helpers/effective_bootstrap_helper.rb
Overview
Boostrap4 Helpers
Instance Method Summary collapse
-
#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: 'btn-outline-primary', 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
#dots(options = nil, &block) ⇒ Object
This is a special variant of dropdown dots do
= dropdown_link_to 'Edit', edit_path(thing)
47 48 49 50 51 52 53 54 |
# File 'app/helpers/effective_bootstrap_helper.rb', line 47 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
56 57 58 59 60 |
# File 'app/helpers/effective_bootstrap_helper.rb', line 56 def dots_link_to(label, path, = {}) [:class] = [[:class], 'dropdown-item'].compact.join(' ') concat link_to(label, path, ) end |
#dropdown(variation: nil, split: true, btn: 'btn-outline-primary', 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
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'app/helpers/effective_bootstrap_helper.rb', line 16 def dropdown(variation: nil, split: true, btn: 'btn-outline-primary', right: false, &block) raise 'expected a block' unless block_given? @_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} 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) : (first + split + ) end end else raise 'split false is unsupported' end @_dropdown_link_tos = nil retval end |
#dropdown_divider ⇒ Object
Works with dots ao and dropdown do
81 82 83 84 85 86 87 88 |
# File 'app/helpers/effective_bootstrap_helper.rb', line 81 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
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'app/helpers/effective_bootstrap_helper.rb', line 63 def dropdown_link_to(label, path, = {}) 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-outline-primary'].compact.join(' ') unless [:class].to_s.include?('btn-') else [:class] = [[:class], 'dropdown-item'].compact.join(' ') end @_dropdown_link_tos << link_to(label, path, ) nil end |
#list_group(&block) ⇒ Object
90 91 92 |
# File 'app/helpers/effective_bootstrap_helper.rb', line 90 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
97 98 99 100 101 102 103 104 105 106 |
# File 'app/helpers/effective_bootstrap_helper.rb', line 97 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
197 198 199 200 201 202 203 204 205 |
# File 'app/helpers/effective_bootstrap_helper.rb', line 197 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
142 143 144 |
# File 'app/helpers/effective_bootstrap_helper.rb', line 142 def nav_divider content_tag(:div, '', class: 'dropdown-divider') end |
#nav_dropdown(label, right: false, link_class: [], list_class: [], &block) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'app/helpers/effective_bootstrap_helper.rb', line 128 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
117 118 119 120 121 122 123 124 125 126 |
# File 'app/helpers/effective_bootstrap_helper.rb', line 117 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 # Regular nav link item content_tag(:li, class: (request.fullpath.include?(path) ? 'nav-item active' : 'nav-item')) do link_to(label, path, merge_class_key(opts, 'nav-link')) end end |
#tab(label, options = {}, &block) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'app/helpers/effective_bootstrap_helper.rb', line 176 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’)
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'app/helpers/effective_bootstrap_helper.rb', line 159 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 |