summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJean-Philippe Lang <jp_lang@yahoo.fr>2008-07-13 12:12:58 +0000
committerJean-Philippe Lang <jp_lang@yahoo.fr>2008-07-13 12:12:58 +0000
commit7b8a4fc28bd22e8e463cc6c40be99766f0cc1410 (patch)
treebeb5e7dd675396ea7d312c20523ab9cf22f38bb2
parentc4eef6314e0e7030d002a2f40f1d4cee993afae6 (diff)
downloadredmine-7b8a4fc28bd22e8e463cc6c40be99766f0cc1410.tar.gz
redmine-7b8a4fc28bd22e8e463cc6c40be99766f0cc1410.zip
Menu mapper: add support for :before, :after and :last options to #push method and add #delete method.
git-svn-id: http://redmine.rubyforge.org/svn/trunk@1660 e93f8b46-1217-0410-a6f0-8f06a7374b81
-rw-r--r--lib/redmine.rb2
-rw-r--r--lib/redmine/menu_manager.rb38
-rw-r--r--test/functional/projects_controller_test.rb29
3 files changed, 61 insertions, 8 deletions
diff --git a/lib/redmine.rb b/lib/redmine.rb
index 84eda9f72..6db32340f 100644
--- a/lib/redmine.rb
+++ b/lib/redmine.rb
@@ -130,5 +130,5 @@ Redmine::MenuManager.map :project_menu do |menu|
menu.push :files, { :controller => 'projects', :action => 'list_files' }, :caption => :label_attachment_plural
menu.push :repository, { :controller => 'repositories', :action => 'show' },
:if => Proc.new { |p| p.repository && !p.repository.new_record? }
- menu.push :settings, { :controller => 'projects', :action => 'settings' }
+ menu.push :settings, { :controller => 'projects', :action => 'settings' }, :last => true
end
diff --git a/lib/redmine/menu_manager.rb b/lib/redmine/menu_manager.rb
index 01c14083e..11b7a72e8 100644
--- a/lib/redmine/menu_manager.rb
+++ b/lib/redmine/menu_manager.rb
@@ -92,11 +92,9 @@ module Redmine
class << self
def map(menu_name)
- mapper = Mapper.new
- yield mapper
@items ||= {}
- @items[menu_name.to_sym] ||= []
- @items[menu_name.to_sym] += mapper.items
+ mapper = Mapper.new(menu_name.to_sym, @items)
+ yield mapper
end
def items(menu_name)
@@ -109,6 +107,14 @@ module Redmine
end
class Mapper
+ def initialize(menu, items)
+ items[menu] ||= []
+ @menu = menu
+ @menu_items = items[menu]
+ end
+
+ @@last_items_count = Hash.new {|h,k| h[k] = 0}
+
# Adds an item at the end of the menu. Available options:
# * param: the parameter name that is used for the project id (default is :id)
# * if: a Proc that is called before rendering the item, the item is displayed only if it returns true
@@ -116,13 +122,31 @@ module Redmine
# * a localized string Symbol
# * a String
# * a Proc that can take the project as argument
+ # * before, after: specify where the menu item should be inserted (eg. :after => :activity)
+ # * last: menu item will stay at the end (eg. :last => true)
# * html_options: a hash of html options that are passed to link_to
def push(name, url, options={})
- items << MenuItem.new(name, url, options)
+ options = options.dup
+
+ # menu item position
+ if before = options.delete(:before)
+ position = @menu_items.index {|i| i.name == before}
+ elsif after = options.delete(:after)
+ position = @menu_items.index {|i| i.name == after}
+ position += 1 unless position.nil?
+ elsif options.delete(:last)
+ position = @menu_items.size
+ @@last_items_count[@menu] += 1
+ end
+ # default position
+ position ||= @menu_items.size - @@last_items_count[@menu]
+
+ @menu_items.insert(position, MenuItem.new(name, url, options))
end
- def items
- @items ||= []
+ # Removes a menu item
+ def delete(name)
+ @menu_items.delete_if {|i| i.name == name}
end
end
diff --git a/test/functional/projects_controller_test.rb b/test/functional/projects_controller_test.rb
index f5f1c6733..4f0a2f179 100644
--- a/test/functional/projects_controller_test.rb
+++ b/test/functional/projects_controller_test.rb
@@ -312,4 +312,33 @@ class ProjectsControllerTest < Test::Unit::TestCase
assert_redirected_to 'admin/projects'
assert Project.find(1).active?
end
+
+ def test_project_menu
+ assert_no_difference 'Redmine::MenuManager.items(:project_menu).size' do
+ Redmine::MenuManager.map :project_menu do |menu|
+ menu.push :foo, { :controller => 'projects', :action => 'show' }, :cation => 'Foo'
+ menu.push :bar, { :controller => 'projects', :action => 'show' }, :before => :activity
+ menu.push :hello, { :controller => 'projects', :action => 'show' }, :caption => Proc.new {|p| p.name.upcase }, :after => :bar
+ end
+
+ get :show, :id => 1
+ assert_tag :div, :attributes => { :id => 'main-menu' },
+ :descendant => { :tag => 'li', :child => { :tag => 'a', :content => 'Foo' } }
+
+ assert_tag :div, :attributes => { :id => 'main-menu' },
+ :descendant => { :tag => 'li', :child => { :tag => 'a', :content => 'Bar' },
+ :before => { :tag => 'li', :child => { :tag => 'a', :content => 'ECOOKBOOK' } } }
+
+ assert_tag :div, :attributes => { :id => 'main-menu' },
+ :descendant => { :tag => 'li', :child => { :tag => 'a', :content => 'ECOOKBOOK' },
+ :before => { :tag => 'li', :child => { :tag => 'a', :content => 'Activity' } } }
+
+ # Remove the menu items
+ Redmine::MenuManager.map :project_menu do |menu|
+ menu.delete :foo
+ menu.delete :bar
+ menu.delete :hello
+ end
+ end
+ end
end