Project

General

Profile

Patch #4250 » 0003-Updated-menus-from-JPL-s-feedback.patch

Eric Davis, 2009-11-23 21:26

View differences:

lib/redmine/menu_manager.rb
313 313
      #   * a String
314 314
      #   * a Proc that can take the project as argument
315 315
      # * before, after: specify where the menu item should be inserted (eg. :after => :activity)
316
      # * parent: menu item will be added as a child of another named menu (eg. :parent => :issues)
317
      # * 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.
318
      #   eg. :children => Proc.new {|project| [Redmine::MenuManager::MenuItem.new(...)] }
316 319
      # * last: menu item will stay at the end (eg. :last => true)
317 320
      # * html_options: a hash of html options that are passed to link_to
318 321
      def push(name, url, options={})
319 322
        options = options.dup
320 323

  
321
        if options[:parent_menu]
322
          subtree = self.find(options[:parent_menu])
324
        if options[:parent]
325
          subtree = self.find(options[:parent])
323 326
          if subtree
324 327
            target_root = subtree
325 328
          else
......
383 386
    
384 387
    class MenuItem < Tree::TreeNode
385 388
      include Redmine::I18n
386
      attr_reader :name, :url, :param, :condition, :parent_menu, :child_menus
389
      attr_reader :name, :url, :param, :condition, :parent, :child_menus
387 390
      
388 391
      def initialize(name, url, options)
389 392
        raise ArgumentError, "Invalid option :if for menu item '#{name}'" if options[:if] && !options[:if].respond_to?(:call)
390 393
        raise ArgumentError, "Invalid option :html for menu item '#{name}'" if options[:html] && !options[:html].is_a?(Hash)
391
        raise ArgumentError, "Cannot set the :parent_menu to be the same as this item" if options[:parent_menu] == name.to_sym
392
        raise ArgumentError, "Invalid option :child_menus for menu item '#{name}'" if options[:child_menus] && !options[:child_menus].respond_to?(:call)
394
        raise ArgumentError, "Cannot set the :parent to be the same as this item" if options[:parent] == name.to_sym
395
        raise ArgumentError, "Invalid option :children for menu item '#{name}'" if options[:children] && !options[:children].respond_to?(:call)
393 396
        @name = name
394 397
        @url = url
395 398
        @condition = options[:if]
......
398 401
        @html_options = options[:html] || {}
399 402
        # Adds a unique class to each menu item based on its name
400 403
        @html_options[:class] = [@html_options[:class], @name.to_s.dasherize].compact.join(' ')
401
        @parent_menu = options[:parent_menu]
402
        @child_menus = options[:child_menus]
404
        @parent = options[:parent]
405
        @child_menus = options[:children]
403 406
        super @name.to_sym
404 407
      end
405 408
      
test/unit/lib/redmine/menu_manager/mapper_test.rb
32 32
  def test_push_onto_parent
33 33
    menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {})
34 34
    menu_mapper.push :test_overview, { :controller => 'projects', :action => 'show'}, {}
35
    menu_mapper.push :test_child, { :controller => 'projects', :action => 'show'}, {:parent_menu => :test_overview}
35
    menu_mapper.push :test_child, { :controller => 'projects', :action => 'show'}, {:parent => :test_overview}
36 36

  
37 37
    assert menu_mapper.exists?(:test_child)
38 38
    assert_equal :test_child, menu_mapper.find(:test_child).name
......
41 41
  def test_push_onto_grandparent
42 42
    menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {})
43 43
    menu_mapper.push :test_overview, { :controller => 'projects', :action => 'show'}, {}
44
    menu_mapper.push :test_child, { :controller => 'projects', :action => 'show'}, {:parent_menu => :test_overview}
45
    menu_mapper.push :test_grandchild, { :controller => 'projects', :action => 'show'}, {:parent_menu => :test_child}
44
    menu_mapper.push :test_child, { :controller => 'projects', :action => 'show'}, {:parent => :test_overview}
45
    menu_mapper.push :test_grandchild, { :controller => 'projects', :action => 'show'}, {:parent => :test_child}
46 46

  
47 47
    assert menu_mapper.exists?(:test_grandchild)
48 48
    grandchild = menu_mapper.find(:test_grandchild)
49 49
    assert_equal :test_grandchild, grandchild.name
50
    assert_equal :test_child, grandchild.parent_menu
50
    assert_equal :test_child, grandchild.parent.name
51 51
  end
52 52

  
53 53
  def test_push_first
......
122 122
  def test_exists_for_child_node
123 123
    menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {})
124 124
    menu_mapper.push :test_overview, { :controller => 'projects', :action => 'show'}, {}
125
    menu_mapper.push :test_child, { :controller => 'projects', :action => 'show'}, {:parent_menu => :test_overview }
125
    menu_mapper.push :test_child, { :controller => 'projects', :action => 'show'}, {:parent => :test_overview }
126 126

  
127 127
    assert menu_mapper.exists?(:test_child)
128 128
  end
test/unit/lib/redmine/menu_manager/menu_helper_test.rb
101 101
    
102 102
  end
103 103

  
104
  def test_render_menu_node_with_child_menus
104
  def test_render_menu_node_with_children
105 105
    User.current = User.find(2)
106 106
    
107 107
    parent_node = Redmine::MenuManager::MenuItem.new(:parent_node,
108 108
                                                     '/test',
109 109
                                                     {
110
                                                       :child_menus => Proc.new {|p|
111
                                                         child_menus = []
110
                                                       :children => Proc.new {|p|
111
                                                         children = []
112 112
                                                         3.times do |time|
113
                                                           child_menus << Redmine::MenuManager::MenuItem.new("test_child_#{time}",
113
                                                           children << Redmine::MenuManager::MenuItem.new("test_child_#{time}",
114 114
                                                                                                             {:controller => 'issues', :action => 'index'},
115 115
                                                                                                             {})
116 116
                                                         end
117
                                                         child_menus
117
                                                         children
118 118
                                                       }
119 119
                                                     })
120 120
    @response.body = render_menu_node(parent_node, Project.find(1))
......
129 129
    end
130 130
  end
131 131

  
132
  def test_render_menu_node_with_nested_items_and_child_menus
132
  def test_render_menu_node_with_nested_items_and_children
133 133
    User.current = User.find(2)
134 134

  
135 135
    parent_node = Redmine::MenuManager::MenuItem.new(:parent_node,
136 136
                                                     '/test',
137 137
                                                     {
138
                                                       :child_menus => Proc.new {|p|
139
                                                         child_menus = []
138
                                                       :children => Proc.new {|p|
139
                                                         children = []
140 140
                                                         3.times do |time|
141
                                                           child_menus << Redmine::MenuManager::MenuItem.new("test_child_#{time}", {:controller => 'issues', :action => 'index'}, {})
141
                                                           children << Redmine::MenuManager::MenuItem.new("test_child_#{time}", {:controller => 'issues', :action => 'index'}, {})
142 142
                                                         end
143
                                                         child_menus
143
                                                         children
144 144
                                                       }
145 145
                                                     })
146 146

  
147 147
    parent_node << Redmine::MenuManager::MenuItem.new(:child_node,
148 148
                                                     '/test',
149 149
                                                     {
150
                                                       :child_menus => Proc.new {|p|
151
                                                         child_menus = []
150
                                                       :children => Proc.new {|p|
151
                                                         children = []
152 152
                                                         6.times do |time|
153
                                                            child_menus << Redmine::MenuManager::MenuItem.new("test_dynamic_child_#{time}", {:controller => 'issues', :action => 'index'}, {})
153
                                                            children << Redmine::MenuManager::MenuItem.new("test_dynamic_child_#{time}", {:controller => 'issues', :action => 'index'}, {})
154 154
                                                         end
155
                                                         child_menus
155
                                                         children
156 156
                                                       }
157 157
                                                     })
158 158

  
......
177 177
    end
178 178
  end
179 179

  
180
  def test_render_menu_node_with_child_menus_without_an_array
180
  def test_render_menu_node_with_children_without_an_array
181 181
    parent_node = Redmine::MenuManager::MenuItem.new(:parent_node,
182 182
                                                     '/test',
183 183
                                                     {
184
                                                       :child_menus => Proc.new {|p| Redmine::MenuManager::MenuItem.new("test_child", "/testing", {})}
184
                                                       :children => Proc.new {|p| Redmine::MenuManager::MenuItem.new("test_child", "/testing", {})}
185 185
                                                     })
186 186

  
187
    assert_raises Redmine::MenuManager::MenuError, ":child_menus must be an array of MenuItems" do
187
    assert_raises Redmine::MenuManager::MenuError, ":children must be an array of MenuItems" do
188 188
      @response.body = render_menu_node(parent_node, Project.find(1))
189 189
    end
190 190
  end
191 191
    
192
  def test_render_menu_node_with_incorrect_child_menus
192
  def test_render_menu_node_with_incorrect_children
193 193
    parent_node = Redmine::MenuManager::MenuItem.new(:parent_node,
194 194
                                                     '/test',
195 195
                                                     {
196
                                                       :child_menus => Proc.new {|p| ["a string"] }
196
                                                       :children => Proc.new {|p| ["a string"] }
197 197
                                                     })
198 198

  
199
    assert_raises Redmine::MenuManager::MenuError, ":child_menus must be an array of MenuItems" do
199
    assert_raises Redmine::MenuManager::MenuError, ":children must be an array of MenuItems" do
200 200
      @response.body = render_menu_node(parent_node, Project.find(1))
201 201
    end
202 202

  
test/unit/lib/redmine/menu_manager/menu_item_test.rb
28 28
  include RedmineMenuTestHelper
29 29

  
30 30
  Redmine::MenuManager.map :test_menu do |menu|
31
    menu.push(:parent_menu, '/test', { })
32
    menu.push(:child_menu, '/test', { :parent_menu => :parent_menu})
33
    menu.push(:child2_menu, '/test', { :parent_menu => :parent_menu})
31
    menu.push(:parent, '/test', { })
32
    menu.push(:child_menu, '/test', { :parent => :parent})
33
    menu.push(:child2_menu, '/test', { :parent => :parent})
34 34
  end
35 35
  
36 36
  context "MenuItem#caption" do
......
92 92
                                              })
93 93
  end
94 94

  
95
  def test_new_menu_item_should_require_a_proc_to_use_the_child_menus_option
95
  def test_new_menu_item_should_require_a_proc_to_use_the_children_option
96 96
    assert_raises ArgumentError do
97 97
      Redmine::MenuManager::MenuItem.new(:test_error, '/test',
98 98
                                         {
99
                                           :child_menus => ['not_a_proc']
99
                                           :children => ['not_a_proc']
100 100
                                         })
101 101
    end
102 102

  
103
    assert Redmine::MenuManager::MenuItem.new(:test_good_child_menus, '/test',
103
    assert Redmine::MenuManager::MenuItem.new(:test_good_children, '/test',
104 104
                                              {
105
                                                :child_menus => Proc.new{}
105
                                                :children => Proc.new{}
106 106
                                              })
107 107
  end
108 108

  
109
  def test_new_should_not_allow_setting_the_parent_menu_item_to_the_current_item
109
  def test_new_should_not_allow_setting_the_parent_item_to_the_current_item
110 110
    assert_raises ArgumentError do
111
      Redmine::MenuManager::MenuItem.new(:test_error, '/test', { :parent_menu => :test_error })
111
      Redmine::MenuManager::MenuItem.new(:test_error, '/test', { :parent => :test_error })
112 112
    end
113 113
  end
114 114

  
115 115
  def test_has_children
116
    parent_item = get_menu_item(:test_menu, :parent_menu)
116
    parent_item = get_menu_item(:test_menu, :parent)
117 117
    assert parent_item.hasChildren?
118 118
    assert_equal 2, parent_item.children.size
119 119
    assert_equal get_menu_item(:test_menu, :child_menu), parent_item.children[0]
(3-3/3)