Module: WCC::Contentful::App::MenuHelper

Defined in:
app/helpers/wcc/contentful/app/menu_helper.rb

Instance Method Summary collapse

Instance Method Details

Returns:

  • (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

Returns:

  • (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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


8
9
10
# File 'app/helpers/wcc/contentful/app/menu_helper.rb', line 8

def menu_button?(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, options)
  options[:class] = [*classes, *options[: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 render_button(button, options = {}, &block)
  html = render_button_inner_html(button, options, &block)

  if button.try(:external?)
    push_class('external', options)
    options[:target] = :_blank
  end
  if button.icon.present? || button.material_icon.present? || options.dig(:icon, :fallback)
    push_class('icon-only', options) unless button.text.present?
  elsif button.text.present?
    push_class('text-only', options)
  end

  push_class(button.style, options) if button.style

  href = button.href
  href = hash_only(href) if href.present? && local?(href)
  return link_to(html, href, options) if href.present?

  (:a, html, options)
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 render_button_icon(icon, options = {})
  fallback = options&.delete(:fallback)
  return fallback&.call unless icon

  options = {
    alt: icon.description || icon.title,
    width: icon.file.dig('details', 'image', 'width'),
    height: icon.file.dig('details', 'image', 'height')
  }.merge!(options || {})
  image_tag(icon&.file&.url, options)
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 render_button_inner_html(button, options = {}, &block)
  html = render_button_icon(button.icon, options.delete(:icon)) || ''.html_safe
  html += render_button_material_icon(button.material_icon) + (:span, button.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 render_button_material_icon(material_icon)
  (:i, material_icon&.downcase, class: ['material-icons'])
end