@@ -1,435 +1,437 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2013 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | module Redmine |
|
19 | 19 | module MenuManager |
|
20 | 20 | class MenuError < StandardError #:nodoc: |
|
21 | 21 | end |
|
22 | 22 | |
|
23 | 23 | module MenuController |
|
24 | 24 | def self.included(base) |
|
25 | 25 | base.extend(ClassMethods) |
|
26 | 26 | end |
|
27 | 27 | |
|
28 | 28 | module ClassMethods |
|
29 | 29 | @@menu_items = Hash.new {|hash, key| hash[key] = {:default => key, :actions => {}}} |
|
30 | 30 | mattr_accessor :menu_items |
|
31 | 31 | |
|
32 | 32 | # Set the menu item name for a controller or specific actions |
|
33 | 33 | # Examples: |
|
34 | 34 | # * menu_item :tickets # => sets the menu name to :tickets for the whole controller |
|
35 | 35 | # * menu_item :tickets, :only => :list # => sets the menu name to :tickets for the 'list' action only |
|
36 | 36 | # * menu_item :tickets, :only => [:list, :show] # => sets the menu name to :tickets for 2 actions only |
|
37 | 37 | # |
|
38 | 38 | # The default menu item name for a controller is controller_name by default |
|
39 | 39 | # Eg. the default menu item name for ProjectsController is :projects |
|
40 | 40 | def menu_item(id, options = {}) |
|
41 | 41 | if actions = options[:only] |
|
42 | 42 | actions = [] << actions unless actions.is_a?(Array) |
|
43 | 43 | actions.each {|a| menu_items[controller_name.to_sym][:actions][a.to_sym] = id} |
|
44 | 44 | else |
|
45 | 45 | menu_items[controller_name.to_sym][:default] = id |
|
46 | 46 | end |
|
47 | 47 | end |
|
48 | 48 | end |
|
49 | 49 | |
|
50 | 50 | def menu_items |
|
51 | 51 | self.class.menu_items |
|
52 | 52 | end |
|
53 | 53 | |
|
54 | 54 | # Returns the menu item name according to the current action |
|
55 | 55 | def current_menu_item |
|
56 | 56 | @current_menu_item ||= menu_items[controller_name.to_sym][:actions][action_name.to_sym] || |
|
57 | 57 | menu_items[controller_name.to_sym][:default] |
|
58 | 58 | end |
|
59 | 59 | |
|
60 | 60 | # Redirects user to the menu item of the given project |
|
61 | 61 | # Returns false if user is not authorized |
|
62 | 62 | def redirect_to_project_menu_item(project, name) |
|
63 | 63 | item = Redmine::MenuManager.items(:project_menu).detect {|i| i.name.to_s == name.to_s} |
|
64 | 64 | if item && User.current.allowed_to?(item.url, project) && (item.condition.nil? || item.condition.call(project)) |
|
65 | 65 | redirect_to({item.param => project}.merge(item.url)) |
|
66 | 66 | return true |
|
67 | 67 | end |
|
68 | 68 | false |
|
69 | 69 | end |
|
70 | 70 | end |
|
71 | 71 | |
|
72 | 72 | module MenuHelper |
|
73 | 73 | # Returns the current menu item name |
|
74 | 74 | def current_menu_item |
|
75 | 75 | controller.current_menu_item |
|
76 | 76 | end |
|
77 | 77 | |
|
78 | 78 | # Renders the application main menu |
|
79 | 79 | def render_main_menu(project) |
|
80 | 80 | render_menu((project && !project.new_record?) ? :project_menu : :application_menu, project) |
|
81 | 81 | end |
|
82 | 82 | |
|
83 | 83 | def display_main_menu?(project) |
|
84 | 84 | menu_name = project && !project.new_record? ? :project_menu : :application_menu |
|
85 | 85 | Redmine::MenuManager.items(menu_name).children.present? |
|
86 | 86 | end |
|
87 | 87 | |
|
88 | 88 | def render_menu(menu, project=nil) |
|
89 | 89 | links = [] |
|
90 | 90 | menu_items_for(menu, project) do |node| |
|
91 | 91 | links << render_menu_node(node, project) |
|
92 | 92 | end |
|
93 | 93 | links.empty? ? nil : content_tag('ul', links.join("\n").html_safe) |
|
94 | 94 | end |
|
95 | 95 | |
|
96 | 96 | def render_menu_node(node, project=nil) |
|
97 | 97 | if node.children.present? || !node.child_menus.nil? |
|
98 | 98 | return render_menu_node_with_children(node, project) |
|
99 | 99 | else |
|
100 | 100 | caption, url, selected = extract_node_details(node, project) |
|
101 | 101 | return content_tag('li', |
|
102 | 102 | render_single_menu_node(node, caption, url, selected)) |
|
103 | 103 | end |
|
104 | 104 | end |
|
105 | 105 | |
|
106 | 106 | def render_menu_node_with_children(node, project=nil) |
|
107 | 107 | caption, url, selected = extract_node_details(node, project) |
|
108 | 108 | |
|
109 | 109 | html = [].tap do |html| |
|
110 | 110 | html << '<li>' |
|
111 | 111 | # Parent |
|
112 | 112 | html << render_single_menu_node(node, caption, url, selected) |
|
113 | 113 | |
|
114 | 114 | # Standard children |
|
115 | 115 | standard_children_list = "".html_safe.tap do |child_html| |
|
116 | 116 | node.children.each do |child| |
|
117 | 117 | child_html << render_menu_node(child, project) |
|
118 | 118 | end |
|
119 | 119 | end |
|
120 | 120 | |
|
121 | 121 | html << content_tag(:ul, standard_children_list, :class => 'menu-children') unless standard_children_list.empty? |
|
122 | 122 | |
|
123 | 123 | # Unattached children |
|
124 | 124 | unattached_children_list = render_unattached_children_menu(node, project) |
|
125 | 125 | html << content_tag(:ul, unattached_children_list, :class => 'menu-children unattached') unless unattached_children_list.blank? |
|
126 | 126 | |
|
127 | 127 | html << '</li>' |
|
128 | 128 | end |
|
129 | 129 | return html.join("\n").html_safe |
|
130 | 130 | end |
|
131 | 131 | |
|
132 | 132 | # Returns a list of unattached children menu items |
|
133 | 133 | def render_unattached_children_menu(node, project) |
|
134 | 134 | return nil unless node.child_menus |
|
135 | 135 | |
|
136 | 136 | "".html_safe.tap do |child_html| |
|
137 | 137 | unattached_children = node.child_menus.call(project) |
|
138 | 138 | # Tree nodes support #each so we need to do object detection |
|
139 | 139 | if unattached_children.is_a? Array |
|
140 | 140 | unattached_children.each do |child| |
|
141 | 141 | child_html << content_tag(:li, render_unattached_menu_item(child, project)) |
|
142 | 142 | end |
|
143 | 143 | else |
|
144 | 144 | raise MenuError, ":child_menus must be an array of MenuItems" |
|
145 | 145 | end |
|
146 | 146 | end |
|
147 | 147 | end |
|
148 | 148 | |
|
149 | 149 | def render_single_menu_node(item, caption, url, selected) |
|
150 | 150 | link_to(h(caption), url, item.html_options(:selected => selected)) |
|
151 | 151 | end |
|
152 | 152 | |
|
153 | 153 | def render_unattached_menu_item(menu_item, project) |
|
154 | 154 | raise MenuError, ":child_menus must be an array of MenuItems" unless menu_item.is_a? MenuItem |
|
155 | 155 | |
|
156 | 156 | if User.current.allowed_to?(menu_item.url, project) |
|
157 | 157 | link_to(h(menu_item.caption), |
|
158 | 158 | menu_item.url, |
|
159 | 159 | menu_item.html_options) |
|
160 | 160 | end |
|
161 | 161 | end |
|
162 | 162 | |
|
163 | 163 | def menu_items_for(menu, project=nil) |
|
164 | 164 | items = [] |
|
165 | 165 | Redmine::MenuManager.items(menu).root.children.each do |node| |
|
166 | 166 | if allowed_node?(node, User.current, project) |
|
167 | 167 | if block_given? |
|
168 | 168 | yield node |
|
169 | 169 | else |
|
170 | 170 | items << node # TODO: not used? |
|
171 | 171 | end |
|
172 | 172 | end |
|
173 | 173 | end |
|
174 | 174 | return block_given? ? nil : items |
|
175 | 175 | end |
|
176 | 176 | |
|
177 | 177 | def extract_node_details(node, project=nil) |
|
178 | 178 | item = node |
|
179 | 179 | url = case item.url |
|
180 | 180 | when Hash |
|
181 | 181 | project.nil? ? item.url : {item.param => project}.merge(item.url) |
|
182 | 182 | when Symbol |
|
183 | 183 | send(item.url) |
|
184 | 184 | else |
|
185 | 185 | item.url |
|
186 | 186 | end |
|
187 | 187 | caption = item.caption(project) |
|
188 | 188 | return [caption, url, (current_menu_item == item.name)] |
|
189 | 189 | end |
|
190 | 190 | |
|
191 | 191 | # Checks if a user is allowed to access the menu item by: |
|
192 | 192 | # |
|
193 | 193 | # * Checking the url target (project only) |
|
194 | 194 | # * Checking the conditions of the item |
|
195 | 195 | def allowed_node?(node, user, project) |
|
196 | 196 | if project && user && !user.allowed_to?(node.url, project) |
|
197 | 197 | return false |
|
198 | 198 | end |
|
199 | 199 | if node.condition && !node.condition.call(project) |
|
200 | 200 | # Condition that doesn't pass |
|
201 | 201 | return false |
|
202 | 202 | end |
|
203 | 203 | return true |
|
204 | 204 | end |
|
205 | 205 | end |
|
206 | 206 | |
|
207 | 207 | class << self |
|
208 | 208 | def map(menu_name) |
|
209 | 209 | @items ||= {} |
|
210 | 210 | mapper = Mapper.new(menu_name.to_sym, @items) |
|
211 | 211 | if block_given? |
|
212 | 212 | yield mapper |
|
213 | 213 | else |
|
214 | 214 | mapper |
|
215 | 215 | end |
|
216 | 216 | end |
|
217 | 217 | |
|
218 | 218 | def items(menu_name) |
|
219 | 219 | @items[menu_name.to_sym] || MenuNode.new(:root, {}) |
|
220 | 220 | end |
|
221 | 221 | end |
|
222 | 222 | |
|
223 | 223 | class Mapper |
|
224 | attr_reader :menu, :menu_items | |
|
225 | ||
|
224 | 226 | def initialize(menu, items) |
|
225 | 227 | items[menu] ||= MenuNode.new(:root, {}) |
|
226 | 228 | @menu = menu |
|
227 | 229 | @menu_items = items[menu] |
|
228 | 230 | end |
|
229 | 231 | |
|
230 | 232 | # Adds an item at the end of the menu. Available options: |
|
231 | 233 | # * param: the parameter name that is used for the project id (default is :id) |
|
232 | 234 | # * if: a Proc that is called before rendering the item, the item is displayed only if it returns true |
|
233 | 235 | # * caption that can be: |
|
234 | 236 | # * a localized string Symbol |
|
235 | 237 | # * a String |
|
236 | 238 | # * a Proc that can take the project as argument |
|
237 | 239 | # * before, after: specify where the menu item should be inserted (eg. :after => :activity) |
|
238 | 240 | # * parent: menu item will be added as a child of another named menu (eg. :parent => :issues) |
|
239 | 241 | # * children: a Proc that is called before rendering the item. The Proc should return an array of MenuItems, which will be added as children to this item. |
|
240 | 242 | # eg. :children => Proc.new {|project| [Redmine::MenuManager::MenuItem.new(...)] } |
|
241 | 243 | # * last: menu item will stay at the end (eg. :last => true) |
|
242 | 244 | # * html_options: a hash of html options that are passed to link_to |
|
243 | 245 | def push(name, url, options={}) |
|
244 | 246 | options = options.dup |
|
245 | 247 | |
|
246 | 248 | if options[:parent] |
|
247 | 249 | subtree = self.find(options[:parent]) |
|
248 | 250 | if subtree |
|
249 | 251 | target_root = subtree |
|
250 | 252 | else |
|
251 | 253 | target_root = @menu_items.root |
|
252 | 254 | end |
|
253 | 255 | |
|
254 | 256 | else |
|
255 | 257 | target_root = @menu_items.root |
|
256 | 258 | end |
|
257 | 259 | |
|
258 | 260 | # menu item position |
|
259 | 261 | if first = options.delete(:first) |
|
260 | 262 | target_root.prepend(MenuItem.new(name, url, options)) |
|
261 | 263 | elsif before = options.delete(:before) |
|
262 | 264 | |
|
263 | 265 | if exists?(before) |
|
264 | 266 | target_root.add_at(MenuItem.new(name, url, options), position_of(before)) |
|
265 | 267 | else |
|
266 | 268 | target_root.add(MenuItem.new(name, url, options)) |
|
267 | 269 | end |
|
268 | 270 | |
|
269 | 271 | elsif after = options.delete(:after) |
|
270 | 272 | |
|
271 | 273 | if exists?(after) |
|
272 | 274 | target_root.add_at(MenuItem.new(name, url, options), position_of(after) + 1) |
|
273 | 275 | else |
|
274 | 276 | target_root.add(MenuItem.new(name, url, options)) |
|
275 | 277 | end |
|
276 | 278 | |
|
277 | 279 | elsif options[:last] # don't delete, needs to be stored |
|
278 | 280 | target_root.add_last(MenuItem.new(name, url, options)) |
|
279 | 281 | else |
|
280 | 282 | target_root.add(MenuItem.new(name, url, options)) |
|
281 | 283 | end |
|
282 | 284 | end |
|
283 | 285 | |
|
284 | 286 | # Removes a menu item |
|
285 | 287 | def delete(name) |
|
286 | 288 | if found = self.find(name) |
|
287 | 289 | @menu_items.remove!(found) |
|
288 | 290 | end |
|
289 | 291 | end |
|
290 | 292 | |
|
291 | 293 | # Checks if a menu item exists |
|
292 | 294 | def exists?(name) |
|
293 | 295 | @menu_items.any? {|node| node.name == name} |
|
294 | 296 | end |
|
295 | 297 | |
|
296 | 298 | def find(name) |
|
297 | 299 | @menu_items.find {|node| node.name == name} |
|
298 | 300 | end |
|
299 | 301 | |
|
300 | 302 | def position_of(name) |
|
301 | 303 | @menu_items.each do |node| |
|
302 | 304 | if node.name == name |
|
303 | 305 | return node.position |
|
304 | 306 | end |
|
305 | 307 | end |
|
306 | 308 | end |
|
307 | 309 | end |
|
308 | 310 | |
|
309 | 311 | class MenuNode |
|
310 | 312 | include Enumerable |
|
311 | 313 | attr_accessor :parent |
|
312 | 314 | attr_reader :last_items_count, :name |
|
313 | 315 | |
|
314 | 316 | def initialize(name, content = nil) |
|
315 | 317 | @name = name |
|
316 | 318 | @children = [] |
|
317 | 319 | @last_items_count = 0 |
|
318 | 320 | end |
|
319 | 321 | |
|
320 | 322 | def children |
|
321 | 323 | if block_given? |
|
322 | 324 | @children.each {|child| yield child} |
|
323 | 325 | else |
|
324 | 326 | @children |
|
325 | 327 | end |
|
326 | 328 | end |
|
327 | 329 | |
|
328 | 330 | # Returns the number of descendants + 1 |
|
329 | 331 | def size |
|
330 | 332 | @children.inject(1) {|sum, node| sum + node.size} |
|
331 | 333 | end |
|
332 | 334 | |
|
333 | 335 | def each &block |
|
334 | 336 | yield self |
|
335 | 337 | children { |child| child.each(&block) } |
|
336 | 338 | end |
|
337 | 339 | |
|
338 | 340 | # Adds a child at first position |
|
339 | 341 | def prepend(child) |
|
340 | 342 | add_at(child, 0) |
|
341 | 343 | end |
|
342 | 344 | |
|
343 | 345 | # Adds a child at given position |
|
344 | 346 | def add_at(child, position) |
|
345 | 347 | raise "Child already added" if find {|node| node.name == child.name} |
|
346 | 348 | |
|
347 | 349 | @children = @children.insert(position, child) |
|
348 | 350 | child.parent = self |
|
349 | 351 | child |
|
350 | 352 | end |
|
351 | 353 | |
|
352 | 354 | # Adds a child as last child |
|
353 | 355 | def add_last(child) |
|
354 | 356 | add_at(child, -1) |
|
355 | 357 | @last_items_count += 1 |
|
356 | 358 | child |
|
357 | 359 | end |
|
358 | 360 | |
|
359 | 361 | # Adds a child |
|
360 | 362 | def add(child) |
|
361 | 363 | position = @children.size - @last_items_count |
|
362 | 364 | add_at(child, position) |
|
363 | 365 | end |
|
364 | 366 | alias :<< :add |
|
365 | 367 | |
|
366 | 368 | # Removes a child |
|
367 | 369 | def remove!(child) |
|
368 | 370 | @children.delete(child) |
|
369 | 371 | @last_items_count -= +1 if child && child.last |
|
370 | 372 | child.parent = nil |
|
371 | 373 | child |
|
372 | 374 | end |
|
373 | 375 | |
|
374 | 376 | # Returns the position for this node in it's parent |
|
375 | 377 | def position |
|
376 | 378 | self.parent.children.index(self) |
|
377 | 379 | end |
|
378 | 380 | |
|
379 | 381 | # Returns the root for this node |
|
380 | 382 | def root |
|
381 | 383 | root = self |
|
382 | 384 | root = root.parent while root.parent |
|
383 | 385 | root |
|
384 | 386 | end |
|
385 | 387 | end |
|
386 | 388 | |
|
387 | 389 | class MenuItem < MenuNode |
|
388 | 390 | include Redmine::I18n |
|
389 | 391 | attr_reader :name, :url, :param, :condition, :parent, :child_menus, :last |
|
390 | 392 | |
|
391 | 393 | def initialize(name, url, options) |
|
392 | 394 | raise ArgumentError, "Invalid option :if for menu item '#{name}'" if options[:if] && !options[:if].respond_to?(:call) |
|
393 | 395 | raise ArgumentError, "Invalid option :html for menu item '#{name}'" if options[:html] && !options[:html].is_a?(Hash) |
|
394 | 396 | raise ArgumentError, "Cannot set the :parent to be the same as this item" if options[:parent] == name.to_sym |
|
395 | 397 | raise ArgumentError, "Invalid option :children for menu item '#{name}'" if options[:children] && !options[:children].respond_to?(:call) |
|
396 | 398 | @name = name |
|
397 | 399 | @url = url |
|
398 | 400 | @condition = options[:if] |
|
399 | 401 | @param = options[:param] || :id |
|
400 | 402 | @caption = options[:caption] |
|
401 | 403 | @html_options = options[:html] || {} |
|
402 | 404 | # Adds a unique class to each menu item based on its name |
|
403 | 405 | @html_options[:class] = [@html_options[:class], @name.to_s.dasherize].compact.join(' ') |
|
404 | 406 | @parent = options[:parent] |
|
405 | 407 | @child_menus = options[:children] |
|
406 | 408 | @last = options[:last] || false |
|
407 | 409 | super @name.to_sym |
|
408 | 410 | end |
|
409 | 411 | |
|
410 | 412 | def caption(project=nil) |
|
411 | 413 | if @caption.is_a?(Proc) |
|
412 | 414 | c = @caption.call(project).to_s |
|
413 | 415 | c = @name.to_s.humanize if c.blank? |
|
414 | 416 | c |
|
415 | 417 | else |
|
416 | 418 | if @caption.nil? |
|
417 | 419 | l_or_humanize(name, :prefix => 'label_') |
|
418 | 420 | else |
|
419 | 421 | @caption.is_a?(Symbol) ? l(@caption) : @caption |
|
420 | 422 | end |
|
421 | 423 | end |
|
422 | 424 | end |
|
423 | 425 | |
|
424 | 426 | def html_options(options={}) |
|
425 | 427 | if options[:selected] |
|
426 | 428 | o = @html_options.dup |
|
427 | 429 | o[:class] += ' selected' |
|
428 | 430 | o |
|
429 | 431 | else |
|
430 | 432 | @html_options |
|
431 | 433 | end |
|
432 | 434 | end |
|
433 | 435 | end |
|
434 | 436 | end |
|
435 | 437 | end |
@@ -1,182 +1,193 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2013 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | require File.expand_path('../../../../../test_helper', __FILE__) |
|
19 | 19 | |
|
20 | 20 | class Redmine::MenuManager::MapperTest < ActiveSupport::TestCase |
|
21 | 21 | context "Mapper#initialize" do |
|
22 | should "be tested" | |
|
22 | should "define a root MenuNode if menu is not present in items" do | |
|
23 | menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {}) | |
|
24 | node = menu_mapper.menu_items | |
|
25 | assert_not_nil node | |
|
26 | assert_equal :root, node.name | |
|
27 | end | |
|
28 | ||
|
29 | should "use existing MenuNode if present" do | |
|
30 | node = "foo" # just an arbitrary reference | |
|
31 | menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {:test_menu => node}) | |
|
32 | assert_equal node, menu_mapper.menu_items | |
|
33 | end | |
|
23 | 34 | end |
|
24 | 35 | |
|
25 | 36 | def test_push_onto_root |
|
26 | 37 | menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {}) |
|
27 | 38 | menu_mapper.push :test_overview, { :controller => 'projects', :action => 'show'}, {} |
|
28 | 39 | |
|
29 | 40 | menu_mapper.exists?(:test_overview) |
|
30 | 41 | end |
|
31 | 42 | |
|
32 | 43 | def test_push_onto_parent |
|
33 | 44 | menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {}) |
|
34 | 45 | menu_mapper.push :test_overview, { :controller => 'projects', :action => 'show'}, {} |
|
35 | 46 | menu_mapper.push :test_child, { :controller => 'projects', :action => 'show'}, {:parent => :test_overview} |
|
36 | 47 | |
|
37 | 48 | assert menu_mapper.exists?(:test_child) |
|
38 | 49 | assert_equal :test_child, menu_mapper.find(:test_child).name |
|
39 | 50 | end |
|
40 | 51 | |
|
41 | 52 | def test_push_onto_grandparent |
|
42 | 53 | menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {}) |
|
43 | 54 | menu_mapper.push :test_overview, { :controller => 'projects', :action => 'show'}, {} |
|
44 | 55 | menu_mapper.push :test_child, { :controller => 'projects', :action => 'show'}, {:parent => :test_overview} |
|
45 | 56 | menu_mapper.push :test_grandchild, { :controller => 'projects', :action => 'show'}, {:parent => :test_child} |
|
46 | 57 | |
|
47 | 58 | assert menu_mapper.exists?(:test_grandchild) |
|
48 | 59 | grandchild = menu_mapper.find(:test_grandchild) |
|
49 | 60 | assert_equal :test_grandchild, grandchild.name |
|
50 | 61 | assert_equal :test_child, grandchild.parent.name |
|
51 | 62 | end |
|
52 | 63 | |
|
53 | 64 | def test_push_first |
|
54 | 65 | menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {}) |
|
55 | 66 | menu_mapper.push :test_second, { :controller => 'projects', :action => 'show'}, {} |
|
56 | 67 | menu_mapper.push :test_third, { :controller => 'projects', :action => 'show'}, {} |
|
57 | 68 | menu_mapper.push :test_fourth, { :controller => 'projects', :action => 'show'}, {} |
|
58 | 69 | menu_mapper.push :test_fifth, { :controller => 'projects', :action => 'show'}, {} |
|
59 | 70 | menu_mapper.push :test_first, { :controller => 'projects', :action => 'show'}, {:first => true} |
|
60 | 71 | |
|
61 | 72 | root = menu_mapper.find(:root) |
|
62 | 73 | assert_equal 5, root.children.size |
|
63 | 74 | {0 => :test_first, 1 => :test_second, 2 => :test_third, 3 => :test_fourth, 4 => :test_fifth}.each do |position, name| |
|
64 | 75 | assert_not_nil root.children[position] |
|
65 | 76 | assert_equal name, root.children[position].name |
|
66 | 77 | end |
|
67 | 78 | |
|
68 | 79 | end |
|
69 | 80 | |
|
70 | 81 | def test_push_before |
|
71 | 82 | menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {}) |
|
72 | 83 | menu_mapper.push :test_first, { :controller => 'projects', :action => 'show'}, {} |
|
73 | 84 | menu_mapper.push :test_second, { :controller => 'projects', :action => 'show'}, {} |
|
74 | 85 | menu_mapper.push :test_fourth, { :controller => 'projects', :action => 'show'}, {} |
|
75 | 86 | menu_mapper.push :test_fifth, { :controller => 'projects', :action => 'show'}, {} |
|
76 | 87 | menu_mapper.push :test_third, { :controller => 'projects', :action => 'show'}, {:before => :test_fourth} |
|
77 | 88 | |
|
78 | 89 | root = menu_mapper.find(:root) |
|
79 | 90 | assert_equal 5, root.children.size |
|
80 | 91 | {0 => :test_first, 1 => :test_second, 2 => :test_third, 3 => :test_fourth, 4 => :test_fifth}.each do |position, name| |
|
81 | 92 | assert_not_nil root.children[position] |
|
82 | 93 | assert_equal name, root.children[position].name |
|
83 | 94 | end |
|
84 | 95 | |
|
85 | 96 | end |
|
86 | 97 | |
|
87 | 98 | def test_push_after |
|
88 | 99 | menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {}) |
|
89 | 100 | menu_mapper.push :test_first, { :controller => 'projects', :action => 'show'}, {} |
|
90 | 101 | menu_mapper.push :test_second, { :controller => 'projects', :action => 'show'}, {} |
|
91 | 102 | menu_mapper.push :test_third, { :controller => 'projects', :action => 'show'}, {} |
|
92 | 103 | menu_mapper.push :test_fifth, { :controller => 'projects', :action => 'show'}, {} |
|
93 | 104 | menu_mapper.push :test_fourth, { :controller => 'projects', :action => 'show'}, {:after => :test_third} |
|
94 | 105 | |
|
95 | 106 | root = menu_mapper.find(:root) |
|
96 | 107 | assert_equal 5, root.children.size |
|
97 | 108 | {0 => :test_first, 1 => :test_second, 2 => :test_third, 3 => :test_fourth, 4 => :test_fifth}.each do |position, name| |
|
98 | 109 | assert_not_nil root.children[position] |
|
99 | 110 | assert_equal name, root.children[position].name |
|
100 | 111 | end |
|
101 | 112 | |
|
102 | 113 | end |
|
103 | 114 | |
|
104 | 115 | def test_push_last |
|
105 | 116 | menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {}) |
|
106 | 117 | menu_mapper.push :test_first, { :controller => 'projects', :action => 'show'}, {} |
|
107 | 118 | menu_mapper.push :test_second, { :controller => 'projects', :action => 'show'}, {} |
|
108 | 119 | menu_mapper.push :test_third, { :controller => 'projects', :action => 'show'}, {} |
|
109 | 120 | menu_mapper.push :test_fifth, { :controller => 'projects', :action => 'show'}, {:last => true} |
|
110 | 121 | menu_mapper.push :test_fourth, { :controller => 'projects', :action => 'show'}, {} |
|
111 | 122 | |
|
112 | 123 | root = menu_mapper.find(:root) |
|
113 | 124 | assert_equal 5, root.children.size |
|
114 | 125 | {0 => :test_first, 1 => :test_second, 2 => :test_third, 3 => :test_fourth, 4 => :test_fifth}.each do |position, name| |
|
115 | 126 | assert_not_nil root.children[position] |
|
116 | 127 | assert_equal name, root.children[position].name |
|
117 | 128 | end |
|
118 | 129 | |
|
119 | 130 | end |
|
120 | 131 | |
|
121 | 132 | def test_exists_for_child_node |
|
122 | 133 | menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {}) |
|
123 | 134 | menu_mapper.push :test_overview, { :controller => 'projects', :action => 'show'}, {} |
|
124 | 135 | menu_mapper.push :test_child, { :controller => 'projects', :action => 'show'}, {:parent => :test_overview } |
|
125 | 136 | |
|
126 | 137 | assert menu_mapper.exists?(:test_child) |
|
127 | 138 | end |
|
128 | 139 | |
|
129 | 140 | def test_exists_for_invalid_node |
|
130 | 141 | menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {}) |
|
131 | 142 | menu_mapper.push :test_overview, { :controller => 'projects', :action => 'show'}, {} |
|
132 | 143 | |
|
133 | 144 | assert !menu_mapper.exists?(:nothing) |
|
134 | 145 | end |
|
135 | 146 | |
|
136 | 147 | def test_find |
|
137 | 148 | menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {}) |
|
138 | 149 | menu_mapper.push :test_overview, { :controller => 'projects', :action => 'show'}, {} |
|
139 | 150 | |
|
140 | 151 | item = menu_mapper.find(:test_overview) |
|
141 | 152 | assert_equal :test_overview, item.name |
|
142 | 153 | assert_equal({:controller => 'projects', :action => 'show'}, item.url) |
|
143 | 154 | end |
|
144 | 155 | |
|
145 | 156 | def test_find_missing |
|
146 | 157 | menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {}) |
|
147 | 158 | menu_mapper.push :test_overview, { :controller => 'projects', :action => 'show'}, {} |
|
148 | 159 | |
|
149 | 160 | item = menu_mapper.find(:nothing) |
|
150 | 161 | assert_equal nil, item |
|
151 | 162 | end |
|
152 | 163 | |
|
153 | 164 | def test_delete |
|
154 | 165 | menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {}) |
|
155 | 166 | menu_mapper.push :test_overview, { :controller => 'projects', :action => 'show'}, {} |
|
156 | 167 | assert_not_nil menu_mapper.delete(:test_overview) |
|
157 | 168 | |
|
158 | 169 | assert_nil menu_mapper.find(:test_overview) |
|
159 | 170 | end |
|
160 | 171 | |
|
161 | 172 | def test_delete_missing |
|
162 | 173 | menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {}) |
|
163 | 174 | assert_nil menu_mapper.delete(:test_missing) |
|
164 | 175 | end |
|
165 | 176 | |
|
166 | 177 | test 'deleting all items' do |
|
167 | 178 | # Exposed by deleting :last items |
|
168 | 179 | Redmine::MenuManager.map :test_menu do |menu| |
|
169 | 180 | menu.push :not_last, Redmine::Info.help_url |
|
170 | 181 | menu.push :administration, { :controller => 'projects', :action => 'show'}, {:last => true} |
|
171 | 182 | menu.push :help, Redmine::Info.help_url, :last => true |
|
172 | 183 | end |
|
173 | 184 | |
|
174 | 185 | assert_nothing_raised do |
|
175 | 186 | Redmine::MenuManager.map :test_menu do |menu| |
|
176 | 187 | menu.delete(:administration) |
|
177 | 188 | menu.delete(:help) |
|
178 | 189 | menu.push :test_overview, { :controller => 'projects', :action => 'show'}, {} |
|
179 | 190 | end |
|
180 | 191 | end |
|
181 | 192 | end |
|
182 | 193 | end |
General Comments 0
You need to be logged in to leave comments.
Login now