Module: Goldencobra::NavigationHelper

Included in:
ApplicationHelper
Defined in:
app/helpers/goldencobra/navigation_helper.rb

Instance Method Summary collapse

Instance Method Details



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/helpers/goldencobra/navigation_helper.rb', line 3

def breadcrumb(options={})
  id_name = options["id"] || "breadcrumb"
  class_name = options["class"] || ""
  if @article
    list = ""
    @article.path.each do |art|
      link_name = link_to(art.breadcrumb_name, art.public_url)
      list << (:li, raw(link_name))
    end
    content_list = (:ol, raw(list))
    if id_name.present?
      result = (:nav, raw(content_list), id: "#{id_name}", class: "#{class_name}")
    else
      result = (:nav, raw(content_list), class: "#{class_name}")
    end
    return raw(result)
  end
end

TODO: offset implementieren TODO: refactor method to make it easier to test



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/helpers/goldencobra/navigation_helper.rb', line 30

def navigation_menu(menue_id, options={})
  return "id can't be blank" if menue_id.blank?
  depth = options[:depth] || 9999
  offset = options[:offset] || 0
  class_name = options[:class] || ""
  id_name = options[:id] || ""
  submenue_of_article = options[:submenue_of_article] || ""
  current_article = options[:current_article] || ""

  if menue_id.class == String
    if current_article.present? && current_article.public_url.present?
      current_menue = Goldencobra::Menue.active.where(target: current_article.public_url(false)).select{|a| a.path.map(&:title).join("/").include?(menue_id)}.first
      if current_menue
        master_menue = Goldencobra::Menue.find_by_id(current_menue.path_ids[offset])
      else
        return ""
      end
    elsif submenue_of_article.present? && submenue_of_article.public_url.present?
      master_menue = Goldencobra::Menue.active.where(target: submenue_of_article.public_url).select{|a| a.path.map(&:title).join("/").include?(menue_id)}.first
    else
      master_menue = Goldencobra::Menue.active.find_by_pathname(menue_id)
    end
  else
    master_menue = Goldencobra::Menue.active.find_by_id(menue_id)
  end

  return "" if master_menue.blank?

  current_depth = master_menue.ancestry_depth
  # Check for Permission
  begin
    if params.present? && params[:frontend_tags].present? && params[:frontend_tags].class != String && params[:frontend_tags][:format] && params[:frontend_tags][:format] == "email"
      #Wenn format email, dann gibt es keinen realen webseit besucher
      ability = Ability.new()
    else current_user.present? || current_visitor.present?
      operator = current_user || current_visitor
      ability = Ability.new(operator)
    end
  rescue
    ability = Ability.new()
  end

  return "" unless ability.can?(:read, master_menue)

  content = ""
  if current_article.present?
    subtree_menues = master_menue.subtree.after_depth(current_depth)
                                 .to_depth(current_depth + depth)
                                 .active.includes(:permissions).includes(:image)
  else
    subtree_menues = master_menue.subtree.after_depth(current_depth + offset)
                                 .to_depth(current_depth + depth)
                                 .active.includes(:permissions).includes(:image)
  end
  subtree_menues = subtree_menues.to_a.delete_if{|a| !ability.can?(:read, a)}

  current_depth = 1
  menue_roots(subtree_menues).each do |root|
    content << navigation_menu_helper(root, options, subtree_menues, current_depth)
  end

  klass = [
    class_name,
    depth,
    "navigation",
    master_menue.css_class.to_s.gsub(/[^\w\-]/, " ")
  ].join(" ").squeeze(" ").strip
  result = (
    :ul,
    raw(content),
    id: id_name.present? ? id_name.to_s : nil,
    class: klass
  )

  raw(result)
end