Module: EffectiveMenusHelper

Defined in:
app/helpers/effective_menus_helper.rb

Instance Method Summary collapse

Instance Method Details

#admin_menu_parent_collection(page = nil) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/helpers/effective_menus_helper.rb', line 60

def admin_menu_parent_collection(page = nil)
  raise('expected a page') if page.present? && !page.kind_of?(Effective::Page)

  pages = Effective::Page.deep_menuable.menuable.root_level.where.not(id: page)

  if EffectivePages.max_menu_depth == 2
    # You can only select root level pages
    pages.group_by(&:menu_name)
  elsif EffectivePages.max_menu_depth == 3
    # You can only select root level pages and immediate children
    pages.map do |page|
      [[page.to_s, page.id, page.menu_name]] + page.menu_children.map do |child|
        label = (:div) do
          arrow = "→"
          group = (:span, child.menu_group, class: 'badge badge-info') if child.menu_group.present?
          title = child.menu_to_s

          [arrow, group, title].compact.join(' ').html_safe
        end

        [child.to_s, child.id, { 'data-html': label }, child.menu_name]
      end
    end.flatten(1).group_by(&:last)
  end
end


49
50
51
52
53
54
55
56
57
58
# File 'app/helpers/effective_menus_helper.rb', line 49

def breadcrumbs_fallback(page = @page, root: 'Home')
  label = (page if page.kind_of?(Effective::Page)) || @page_title || 'Here'

  (:ol, class: 'breadcrumb') do
    [
      (:li, link_to(root, root_path, title: root), class: 'breadcrumb-item'),
      (:li, label, class: 'breadcrumb-item active', 'aria-current': 'page')
    ].join.html_safe
  end
end


43
44
45
46
47
# File 'app/helpers/effective_menus_helper.rb', line 43

def breadcrumbs_root_url(page = @page, root: 'Home')
  (:ol, class: 'breadcrumb') do
    (:li, root, class: 'breadcrumb-item active', 'aria-current': 'page')
  end
end

#render_breadcrumbs(menu, page = @page, root: 'Home') ⇒ Object Also known as: render_breadcrumb



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/helpers/effective_menus_helper.rb', line 21

def render_breadcrumbs(menu, page = @page, root: 'Home')
  return breadcrumbs_root_url(page, root: root) if request.path == '/'
  return breadcrumbs_fallback(page, root: root) unless page.kind_of?(Effective::Page)

  parents = [page.menu_parent&.menu_parent, page.menu_parent].compact

  (:ol, class: 'breadcrumb') do
    (
      [(:li, link_to(root, root_path, title: root), class: 'breadcrumb-item')] +
      parents.map do |page|
        next if page.menu_root_with_children? # Don't show root pages because they have no content

        url = (page.menu_url.presence || effective_pages.page_path(page))
        (:li, link_to(page, url, title: page.title), class: 'breadcrumb-item')
      end +
      [(:li, page, class: 'breadcrumb-item active', 'aria-current': 'page')]
    ).join.html_safe
  end
end

#render_menu(name, options = {}, &block) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'app/helpers/effective_menus_helper.rb', line 4

def render_menu(name, options = {}, &block)
  name = name.to_s
  menu = Array(EffectivePages.menus).find { |menu| menu.to_s == name }

  if menu.blank?
    raise("unable to find menu #{name}. Please add it to config/initializers/effective_pages.rb")
  end

  (:ul, options) { render('effective/pages/menu', menu: menu) }
end

#render_page_menu(page, options = {}, &block) ⇒ Object



15
16
17
18
19
# File 'app/helpers/effective_menus_helper.rb', line 15

def render_page_menu(page, options = {}, &block)
  raise('expected a page with menu true') unless page.kind_of?(Effective::Page) && page.menu?

  (:ul, options) { render('effective/pages/page_menu', page: page) }
end