Module: WCC::Contentful::App::MenuHelper
- Defined in:
- app/helpers/wcc/contentful/app/menu_helper.rb
Instance Method Summary collapse
- #dropdown?(item) ⇒ Boolean
- #hash_only(href) ⇒ Object
- #item_active?(item) ⇒ Boolean
-
#local?(href) ⇒ Boolean
An href is local if it points to a part of the page.
- #menu_button?(item) ⇒ Boolean
- #push_class(classes, options) ⇒ Object
- #render_button(button, options = {}, &block) ⇒ Object
- #render_button_icon(icon, options = {}) ⇒ Object
- #render_button_inner_html(button, options = {}, &block) ⇒ Object
- #render_button_material_icon(material_icon) ⇒ Object
Instance Method Details
#dropdown?(item) ⇒ Boolean
4 5 6 |
# File 'app/helpers/wcc/contentful/app/menu_helper.rb', line 4 def dropdown?(item) item.respond_to?(:items) end |
#hash_only(href) ⇒ Object
70 71 72 73 |
# File 'app/helpers/wcc/contentful/app/menu_helper.rb', line 70 def hash_only(href) url = URI(href) "##{url.fragment}" if url.fragment.present? end |
#item_active?(item) ⇒ Boolean
12 13 14 15 16 17 18 |
# File 'app/helpers/wcc/contentful/app/menu_helper.rb', line 12 def item_active?(item) return true if item.try(:label) && item_active?(item.label) return item.items.any? { |i| item_active?(i) } if item.respond_to?(:items) return current_page?(item.href) if item.try(:href) false end |
#local?(href) ⇒ Boolean
An href is local if it points to a part of the page
76 77 78 79 80 81 82 83 84 |
# File 'app/helpers/wcc/contentful/app/menu_helper.rb', line 76 def local?(href) return true if href =~ /^#/ url = URI(href) return false unless url.fragment.present? url.fragment = nil current_page?(url.to_s) end |
#menu_button?(item) ⇒ Boolean
8 9 10 |
# File 'app/helpers/wcc/contentful/app/menu_helper.rb', line 8 def (item) item.is_a? WCC::Contentful::Model::MenuButton end |
#push_class(classes, options) ⇒ Object
66 67 68 |
# File 'app/helpers/wcc/contentful/app/menu_helper.rb', line 66 def push_class(classes, ) [:class] = [*classes, *[:class]] end |
#render_button(button, options = {}, &block) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'app/helpers/wcc/contentful/app/menu_helper.rb', line 20 def (, = {}, &block) html = (, , &block) if .try(:external?) push_class('external', ) [:target] = :_blank end if .icon.present? || .material_icon.present? || .dig(:icon, :fallback) push_class('icon-only', ) unless .text.present? elsif .text.present? push_class('text-only', ) end push_class(.style, ) if .style href = .href href = hash_only(href) if href.present? && local?(href) return link_to(html, href, ) if href.present? content_tag(:a, html, ) end |
#render_button_icon(icon, options = {}) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 |
# File 'app/helpers/wcc/contentful/app/menu_helper.rb', line 50 def (icon, = {}) fallback = &.delete(:fallback) return fallback&.call unless icon = { alt: icon.description || icon.title, width: icon.file.dig('details', 'image', 'width'), height: icon.file.dig('details', 'image', 'height') }.merge!( || {}) image_tag(icon&.file&.url, ) end |
#render_button_inner_html(button, options = {}, &block) ⇒ Object
42 43 44 45 46 47 48 |
# File 'app/helpers/wcc/contentful/app/menu_helper.rb', line 42 def (, = {}, &block) html = (.icon, .delete(:icon)) || ''.html_safe html += (.material_icon) + content_tag(:span, .text) html += capture(&block) if block_given? html end |
#render_button_material_icon(material_icon) ⇒ Object
62 63 64 |
# File 'app/helpers/wcc/contentful/app/menu_helper.rb', line 62 def (material_icon) content_tag(:i, material_icon&.downcase, class: ['material-icons']) end |