Module: Gluttonberg::Public::NavTree

Included in:
Gluttonberg::Public
Defined in:
app/helpers/gluttonberg/public/nav_tree.rb

Instance Method Summary collapse

Instance Method Details

#build_page(page, opts) ⇒ Object

build each page and returns an li



59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/helpers/gluttonberg/public/nav_tree.rb', line 59

def build_page(page, opts)
  span = (:span, page.nav_label).html_safe
  if page.home?
    return (:a, span, :href => "/").html_safe
  else
    if page.description && page.description.top_level_page?
      return (:a, span, :href=>"javascript:;", :class => "menu_disabled").html_safe
    else
      return (:a, span, :href => page_url(page , opts), :target => "#{page.redirect_required? && URI(page.redirect_url).absolute? ? '_blank' : ''}").html_safe
    end
  end
end

#children_active?(parent) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
75
76
77
78
79
80
# File 'app/helpers/gluttonberg/public/nav_tree.rb', line 72

def children_active?(parent)
  active = false
  active = true if parent == @page
  parent.children.each do |page|
    active = true if children_active?(page) == true
    active = true if page == @page
  end
  return active
end

#find_children(parent, page_depth, opts) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/helpers/gluttonberg/public/nav_tree.rb', line 44

def find_children(parent, page_depth, opts)
  content = ""
  parent.children.published.each do |page|
    child_depth = page_depth + 1
    page.load_localization(@locale)
    li_opts = {:class => page.localizations[0] && page.localizations[0].slug ? "#{page.localizations[0].slug}-nav" : "#{page.slug}-nav"}
    li_opts[:class] += " active" if page == @page  || children_active?(page)
    li_content = build_page(page, opts)
    li_content << find_children(page, child_depth, opts) if opts[:max_depth] >= child_depth
    content << (:li, li_content.html_safe, li_opts).html_safe
  end
  return (:ul, content.html_safe, opts).html_safe if !content.blank?
end

A simple helper which loops through a heirarchy of pages and produces a set of nested lists with links to each page.



9
10
11
12
13
14
15
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/gluttonberg/public/nav_tree.rb', line 9

def navigation_tree(pages, opts = {})
  opts[:max_depth] ||= 10
  content = ""
  home = Gluttonberg::Page.home_page if pages.nil?
  pages = home.children.published if home
  pages = Gluttonberg::Page.where(:parent_id => nil, :state => "published").order("position ASC") if pages.nil?
  pages = pages.to_a
  pages.insert(0, Gluttonberg::Page.home_page) if opts[:include_home] == true

  # if opts[:path]
  #   content_ul = Rails.cache.read("nav-#{opts[:path]}")
  #   return content_ul if content_ul
  # end

  pages.each do |page|
    page_depth = 1
    page.load_localization(@locale)
    li_opts = {:class => page.localizations[0] && page.localizations[0].slug ? "#{page.localizations[0].slug}-nav" : "#{page.slug}-nav"}
    unless Gluttonberg::Page.home_page == page
      li_opts[:class] += " active" if page == @page || children_active?(page)
    end
    li_content = build_page(page, opts)
    unless Gluttonberg::Page.home_page == page
      li_content << find_children(page, page_depth, opts) if opts[:max_depth] >= page_depth && page.number_of_children > 0
    end
    content << (:li, li_content.html_safe, li_opts).html_safe
  end

  content_ul = (:ul, content.html_safe, opts).html_safe

  # Rails.cache.write("nav-#{opts[:path]}", content_ul, :expires_in => 5.minutes) if opts[:path]

  return content_ul
end

#page_url(path_or_page, opts = {}) ⇒ Object

finds the correct url for a page.



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/helpers/gluttonberg/public/nav_tree.rb', line 83

def page_url(path_or_page , opts = {})
  if path_or_page.redirect_required?
    path_or_page.redirect_url
  elsif path_or_page.rewrite_required?
    "#{path_or_page.description.rewrite_route}"
  else
    if Gluttonberg.localized? && !opts[:slug].blank?
      "/#{opts[:slug]}/#{path_or_page.path}"
    else
      "#{path_or_page.public_path}"
    end
  end
end