Module: Redmine::MenuManager::MenuHelper

Defined in:
lib/redmine/menu_manager/menu_helper.rb

Overview

– copyright ChiliProject is a project management system.

Copyright (C) 2010-2013 the ChiliProject Team

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

See doc/COPYRIGHT.rdoc for more details. ++

Instance Method Summary collapse

Instance Method Details

#allowed_node?(node, user, project) ⇒ Boolean

Checks if a user is allowed to access the menu item by:

  • Checking the conditions of the item

  • Checking the url target (project only)

Returns:

  • (Boolean)

138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/redmine/menu_manager/menu_helper.rb', line 138

def allowed_node?(node, user, project)
  if node.condition && !node.condition.call(project)
    # Condition that doesn't pass
    return false
  end

  if project
    return user && user.allowed_to?(node.url, project)
  else
    # outside a project, all menu items allowed
    return true
  end
end

#current_menu_itemObject

Returns the current menu item name


17
18
19
# File 'lib/redmine/menu_manager/menu_helper.rb', line 17

def current_menu_item
  @controller.current_menu_item
end

#display_main_menu?(project) ⇒ Boolean

Returns:

  • (Boolean)

26
27
28
29
# File 'lib/redmine/menu_manager/menu_helper.rb', line 26

def display_main_menu?(project)
  menu_name = project && !project.new_record? ? :project_menu : :application_menu
  Redmine::MenuManager.items(menu_name).size > 1 # 1 element is the root
end

#extract_node_details(node, project = nil) ⇒ Object


120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/redmine/menu_manager/menu_helper.rb', line 120

def extract_node_details(node, project=nil)
  item = node
  url = case item.url
  when Hash
    project.nil? ? item.url : {item.param => project}.merge(item.url)
  when Symbol
    send(item.url)
  else
    item.url
  end
  caption = item.caption(project)
  return [caption, url, (current_menu_item == item.name)]
end

106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/redmine/menu_manager/menu_helper.rb', line 106

def menu_items_for(menu, project=nil)
  items = []
  Redmine::MenuManager.items(menu).root.children.each do |node|
    if allowed_node?(node, User.current, project)
      if block_given?
        yield node
      else
        items << node  # TODO: not used?
      end
    end
  end
  return block_given? ? nil : items
end

#render_main_menu(project) ⇒ Object

Renders the application main menu


22
23
24
# File 'lib/redmine/menu_manager/menu_helper.rb', line 22

def render_main_menu(project)
  render_menu((project && !project.new_record?) ? :project_menu : :application_menu, project)
end

#render_menu(menu, project = nil) ⇒ Object


31
32
33
34
35
36
37
# File 'lib/redmine/menu_manager/menu_helper.rb', line 31

def render_menu(menu, project=nil)
  links = []
  menu_items_for(menu, project) do |node|
    links << render_menu_node(node, project)
  end
  links.empty? ? nil : ('ul', links.join("\n"))
end

#render_menu_node(node, project = nil) ⇒ Object


39
40
41
42
43
44
45
46
47
# File 'lib/redmine/menu_manager/menu_helper.rb', line 39

def render_menu_node(node, project=nil)
  if node.hasChildren? || !node.child_menus.nil?
    return render_menu_node_with_children(node, project)
  else
    caption, url, selected = extract_node_details(node, project)
    return ('li',
                         render_single_menu_node(node, caption, url, selected))
  end
end

#render_menu_node_with_children(node, project = nil) ⇒ Object


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
# File 'lib/redmine/menu_manager/menu_helper.rb', line 49

def render_menu_node_with_children(node, project=nil)
  caption, url, selected = extract_node_details(node, project)

  html = [].tap do |html|
    html << '<li>'
    # Parent
    html << render_single_menu_node(node, caption, url, selected)

    # Standard children
    standard_children_list = "".tap do |child_html|
      node.children.each do |child|
        child_html << render_menu_node(child, project)
      end
    end

    html << (:ul, standard_children_list, :class => 'menu-children') unless standard_children_list.empty?

    # Unattached children
    unattached_children_list = render_unattached_children_menu(node, project)
    html << (:ul, unattached_children_list, :class => 'menu-children unattached') unless unattached_children_list.blank?

    html << '</li>'
  end
  return html.join("\n")
end

#render_single_menu_node(item, caption, url, selected) ⇒ Object


92
93
94
# File 'lib/redmine/menu_manager/menu_helper.rb', line 92

def render_single_menu_node(item, caption, url, selected)
  link_to(h(caption), url, item.html_options(:selected => selected))
end

#render_unattached_children_menu(node, project) ⇒ Object

Returns a list of unattached children menu items


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/redmine/menu_manager/menu_helper.rb', line 76

def render_unattached_children_menu(node, project)
  return nil unless node.child_menus

  "".tap do |child_html|
    unattached_children = node.child_menus.call(project)
    # Tree nodes support #each so we need to do object detection
    if unattached_children.is_a? Array
      unattached_children.each do |child|
        child_html << (:li, render_unattached_menu_item(child, project))
      end
    else
      raise Redmine::MenuManager::MenuError, ":child_menus must be an array of MenuItems"
    end
  end
end

#render_unattached_menu_item(menu_item, project) ⇒ Object


96
97
98
99
100
101
102
103
104
# File 'lib/redmine/menu_manager/menu_helper.rb', line 96

def render_unattached_menu_item(menu_item, project)
  raise Redmine::MenuManager::MenuError, ":child_menus must be an array of MenuItems" unless menu_item.is_a? Redmine::MenuManager::MenuItem

  if User.current.allowed_to?(menu_item.url, project)
    link_to(h(menu_item.caption),
            menu_item.url,
            menu_item.html_options)
  end
end