Module: Redmine::MenuManager::MenuHelper

Defined in:
lib/redmine/menu_manager.rb

Overview

Since:

  • 0.7.0

Instance Method Summary collapse

Instance Method Details

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

See MenuItem#allowed?

Returns:

  • (Boolean)

Raises:

Since:

  • 0.9.0



224
225
226
227
# File 'lib/redmine/menu_manager.rb', line 224

def allowed_node?(node, user, project)
  raise MenuError, ":child_menus must be an array of MenuItems" unless node.is_a? MenuItem
  node.allowed?(user, project)
end

#current_menu_itemObject

Returns the current menu item name



94
95
96
# File 'lib/redmine/menu_manager.rb', line 94

def current_menu_item
  controller.current_menu_item
end

#display_main_menu?(project) ⇒ Boolean

Returns:

  • (Boolean)

Since:

  • 0.9.4



105
106
107
108
# File 'lib/redmine/menu_manager.rb', line 105

def display_main_menu?(project)
  menu_name = controller.current_menu(project)
  menu_name.present? && Redmine::MenuManager.items(menu_name).children.present?
end

#extract_node_details(node, project = nil) ⇒ Object

Since:

  • 0.9.0



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/redmine/menu_manager.rb', line 205

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
    if project
      send(item.url, project)
    else
      send(item.url)
    end
  else
    item.url
  end
  caption = item.caption(project)
  return [caption, url, (current_menu_item == item.name)]
end

Since:

  • 0.8.0



191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/redmine/menu_manager.rb', line 191

def menu_items_for(menu, project=nil)
  items = []
  Redmine::MenuManager.items(menu).root.children.each do |node|
    if node.allowed?(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



99
100
101
102
103
# File 'lib/redmine/menu_manager.rb', line 99

def render_main_menu(project)
  if menu_name = controller.current_menu(project)
    render_menu(menu_name, project)
  end
end

#render_menu(menu, project = nil) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/redmine/menu_manager.rb', line 110

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.html_safe)
end

#render_menu_node(node, project = nil) ⇒ Object

Since:

  • 0.9.0



118
119
120
121
122
123
124
125
126
# File 'lib/redmine/menu_manager.rb', line 118

def render_menu_node(node, project=nil)
  if node.children.present? || !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

Since:

  • 0.9.0



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/redmine/menu_manager.rb', line 128

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 = "".html_safe.tap do |child_html|
      node.children.each do |child|
        child_html << render_menu_node(child, project) if allowed_node?(child, User.current, 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").html_safe
end

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

Since:

  • 0.9.0



171
172
173
174
175
176
177
178
179
180
181
# File 'lib/redmine/menu_manager.rb', line 171

def render_single_menu_node(item, caption, url, selected)
  options = item.html_options(:selected => selected)

  # virtual nodes are only there for their children to be displayed in the menu
  # and should not do anything on click, except if otherwise defined elsewhere
  if url.blank?
    url = '#'
    options.reverse_merge!(:onclick => 'return false;')
  end
  link_to(h(caption), url, options)
end

#render_unattached_children_menu(node, project) ⇒ Object

Returns a list of unattached children menu items

Since:

  • 0.9.0



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/redmine/menu_manager.rb', line 155

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

  "".html_safe.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)) if allowed_node?(child, User.current, project)
      end
    else
      raise MenuError, ":child_menus must be an array of MenuItems"
    end
  end
end

#render_unattached_menu_item(menu_item, project) ⇒ Object

Raises:

Since:

  • 0.9.0



183
184
185
186
187
188
189
# File 'lib/redmine/menu_manager.rb', line 183

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

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