Fix an IndexError if all the :last menu items are deleted from a menu. #4718

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@3447 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Eric Davis 2010-02-18 05:01:39 +00:00
parent 0fcc436f22
commit 28e9bc5d82
2 changed files with 29 additions and 3 deletions

View File

@ -84,6 +84,14 @@ module TreeNodePatch
end
# Wrapp remove! making sure to decrement the last_items counter if
# the removed child was a last item
def remove!(child)
@last_items_count -= +1 if child && child.last
super
end
# Will return the position (zero-based) of the current child in
# it's parent
def position
@ -352,7 +360,7 @@ module Redmine
target_root.add(MenuItem.new(name, url, options))
end
elsif options.delete(:last)
elsif options[:last] # don't delete, needs to be stored
target_root.add_last(MenuItem.new(name, url, options))
else
target_root.add(MenuItem.new(name, url, options))
@ -386,7 +394,7 @@ module Redmine
class MenuItem < Tree::TreeNode
include Redmine::I18n
attr_reader :name, :url, :param, :condition, :parent, :child_menus
attr_reader :name, :url, :param, :condition, :parent, :child_menus, :last
def initialize(name, url, options)
raise ArgumentError, "Invalid option :if for menu item '#{name}'" if options[:if] && !options[:if].respond_to?(:call)
@ -403,6 +411,7 @@ module Redmine
@html_options[:class] = [@html_options[:class], @name.to_s.dasherize].compact.join(' ')
@parent = options[:parent]
@child_menus = options[:children]
@last = options[:last] || false
super @name.to_sym
end

View File

@ -17,7 +17,7 @@
require File.dirname(__FILE__) + '/../../../../test_helper'
class Redmine::MenuManager::MapperTest < Test::Unit::TestCase
class Redmine::MenuManager::MapperTest < ActiveSupport::TestCase
context "Mapper#initialize" do
should "be tested"
end
@ -163,4 +163,21 @@ class Redmine::MenuManager::MapperTest < Test::Unit::TestCase
menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {})
assert_nil menu_mapper.delete(:test_missing)
end
test 'deleting all items' do
# Exposed by deleting :last items
Redmine::MenuManager.map :test_menu do |menu|
menu.push :not_last, Redmine::Info.help_url
menu.push :administration, { :controller => 'projects', :action => 'show'}, {:last => true}
menu.push :help, Redmine::Info.help_url, :last => true
end
assert_nothing_raised do
Redmine::MenuManager.map :test_menu do |menu|
menu.delete(:administration)
menu.delete(:help)
menu.push :test_overview, { :controller => 'projects', :action => 'show'}, {}
end
end
end
end