Index: test/unit/lib/redmine/menu_manager/menu_item_test.rb =================================================================== --- test/unit/lib/redmine/menu_manager/menu_item_test.rb (リビジョン 8213) +++ test/unit/lib/redmine/menu_manager/menu_item_test.rb (リビジョン 8214) @@ -114,7 +114,7 @@ def test_has_children parent_item = get_menu_item(:test_menu, :parent) - assert parent_item.hasChildren? + assert parent_item.children.present? assert_equal 2, parent_item.children.size assert_equal get_menu_item(:test_menu, :child_menu), parent_item.children[0] assert_equal get_menu_item(:test_menu, :child2_menu), parent_item.children[1] Index: config/environment.rb =================================================================== --- config/environment.rb (リビジョン 8213) +++ config/environment.rb (リビジョン 8214) @@ -54,7 +54,6 @@ # It will automatically turn deliveries on config.action_mailer.perform_deliveries = false - config.gem 'rubytree', :lib => 'tree' config.gem 'coderay', :version => '~>1.0.0' # Load any local configuration that is kept out of source control Index: lib/redmine/menu_manager.rb =================================================================== --- lib/redmine/menu_manager.rb (リビジョン 8213) +++ lib/redmine/menu_manager.rb (リビジョン 8214) @@ -15,93 +15,6 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -require 'tree' # gem install rubytree - -# Monkey patch the TreeNode to add on a few more methods :nodoc: -module TreeNodePatch - def self.included(base) - base.class_eval do - attr_reader :last_items_count - - alias :old_initilize :initialize - def initialize(name, content = nil) - old_initilize(name, content) - @childrenHash ||= {} - @last_items_count = 0 - extend(InstanceMethods) - end - end - end - - module InstanceMethods - # Adds the specified child node to the receiver node. The child node's - # parent is set to be the receiver. The child is added as the first child in - # the current list of children for the receiver node. - def prepend(child) - raise "Child already added" if @childrenHash.has_key?(child.name) - - @childrenHash[child.name] = child - @children = [child] + @children - child.parent = self - return child - - end - - # Adds the specified child node to the receiver node. The child node's - # parent is set to be the receiver. The child is added at the position - # into the current list of children for the receiver node. - def add_at(child, position) - raise "Child already added" if @childrenHash.has_key?(child.name) - - @childrenHash[child.name] = child - @children = @children.insert(position, child) - child.parent = self - return child - - end - - def add_last(child) - raise "Child already added" if @childrenHash.has_key?(child.name) - - @childrenHash[child.name] = child - @children << child - @last_items_count += 1 - child.parent = self - return child - - end - - # Adds the specified child node to the receiver node. The child node's - # parent is set to be the receiver. The child is added as the last child in - # the current list of children for the receiver node. - def add(child) - raise "Child already added" if @childrenHash.has_key?(child.name) - - @childrenHash[child.name] = child - position = @children.size - @last_items_count - @children.insert(position, child) - child.parent = self - return child - - 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 - self.parent.children.index(self) - end - end -end -Tree::TreeNode.send(:include, TreeNodePatch) - module Redmine module MenuManager class MenuError < StandardError #:nodoc: @@ -169,7 +82,7 @@ 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 + Redmine::MenuManager.items(menu_name).children.present? end def render_menu(menu, project=nil) @@ -181,7 +94,7 @@ end def render_menu_node(node, project=nil) - if node.hasChildren? || !node.child_menus.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) @@ -306,13 +219,13 @@ end def items(menu_name) - @items[menu_name.to_sym] || Tree::TreeNode.new(:root, {}) + @items[menu_name.to_sym] || MenuNode.new(:root, {}) end end class Mapper def initialize(menu, items) - items[menu] ||= Tree::TreeNode.new(:root, {}) + items[menu] ||= MenuNode.new(:root, {}) @menu = menu @menu_items = items[menu] end @@ -398,7 +311,102 @@ end end - class MenuItem < Tree::TreeNode + class MenuNode + include Enumerable + attr_accessor :parent + attr_reader :last_items_count, :name + + def initialize(name, content = nil) + @name = name + @childrenHash ||= {} + @children = [] + @last_items_count = 0 + end + + def children + if block_given? + @children.each {|child| yield child} + else + @children + end + end + + # Returns the number of descendants + 1 + def size + @children.inject(1) {|sum, node| sum + node.size} + end + + def each &block + yield self + children { |child| child.each(&block) } + end + + # Adds a child at first position + def prepend(child) + raise "Child already added" if @childrenHash.has_key?(child.name) + + @childrenHash[child.name] = child + @children = [child] + @children + child.parent = self + return child + end + + # Adds a child at given position + def add_at(child, position) + raise "Child already added" if @childrenHash.has_key?(child.name) + + @childrenHash[child.name] = child + @children = @children.insert(position, child) + child.parent = self + return child + end + + # Adds a child as last child + def add_last(child) + raise "Child already added" if @childrenHash.has_key?(child.name) + + @childrenHash[child.name] = child + @children << child + @last_items_count += 1 + child.parent = self + return child + end + + # Adds a child + def add(child) + raise "Child already added" if @childrenHash.has_key?(child.name) + + @childrenHash[child.name] = child + position = @children.size - @last_items_count + @children.insert(position, child) + child.parent = self + return child + end + alias :<< :add + + # Removes a child + def remove!(child) + @childrenHash.delete(child.name) + @children.delete(child) + @last_items_count -= +1 if child && child.last + child.parent = nil + child + end + + # Returns the position for this node in it's parent + def position + self.parent.children.index(self) + end + + # Returns the root for this node + def root + root = self + root = root.parent while root.parent + root + end + end + + class MenuItem < MenuNode include Redmine::I18n attr_reader :name, :url, :param, :condition, :parent, :child_menus, :last