diff --git a/lib/redmine/menu_manager.rb b/lib/redmine/menu_manager.rb index af7d67b77..34cea79fa 100644 --- a/lib/redmine/menu_manager.rb +++ b/lib/redmine/menu_manager.rb @@ -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 diff --git a/test/unit/lib/redmine/menu_manager/mapper_test.rb b/test/unit/lib/redmine/menu_manager/mapper_test.rb index 699c0cf2c..a23b624f7 100644 --- a/test/unit/lib/redmine/menu_manager/mapper_test.rb +++ b/test/unit/lib/redmine/menu_manager/mapper_test.rb @@ -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