Project

General

Profile

Feature #15880 » 0001-introduce-virtual-MenuNodes-which-are-characterized-.patch

Jan from Planio www.plan.io, 2016-06-06 14:26

View differences:

lib/redmine/menu_manager.rb
147 147
      end
148 148

  
149 149
      def render_single_menu_node(item, caption, url, selected)
150
        link_to(h(caption), url, item.html_options(:selected => selected))
150
        options = item.html_options(:selected => selected)
151

  
152
        # virtual nodes are only there for their children to be displayed in the menu
153
        # and should not do anything on click, except if otherwise defined elsewhere
154
        if url.blank?
155
          url = '#'
156
          options.reverse_merge!(:onclick => 'return false;')
157
        end
158
        link_to(h(caption), url, options)
151 159
      end
152 160

  
153 161
      def render_unattached_menu_item(menu_item, project)
......
433 441
      # * Checking the permission or the url target (project only)
434 442
      # * Checking the conditions of the item
435 443
      def allowed?(user, project)
436
        if user && project
444
        if url.blank?
445
          # this is a virtual node that is only there for its children to be diplayed in the menu
446
          # it is considered an allowed node if at least one of the children is allowed
447
          all_children = children
448
          all_children += child_menus.call(project) if child_menus
449
          return true if all_children.detect{|child| child.allowed?(user, project) }
450
          return false
451
        elsif user && project
437 452
          if permission
438 453
            unless user.allowed_to?(permission, project)
439 454
              return false
test/unit/lib/redmine/menu_manager/menu_helper_test.rb
209 209
    end
210 210
  end
211 211

  
212
  def test_render_empty_virtual_menu_node_with_children
213

  
214
    # only empty item with no click target
215
    Redmine::MenuManager.map :menu1 do |menu|
216
      menu.push(:parent_node, nil, { })
217
    end
218

  
219
    # parent with unallowed unattached child
220
    Redmine::MenuManager.map :menu2 do |menu|
221
      menu.push(:parent_node, nil, {:children => Proc.new {|p|
222
         [Redmine::MenuManager::MenuItem.new("test_child_unallowed", {:controller => 'issues', :action => 'new'}, {})]
223
       } })
224
    end
225

  
226
    # parent with unallowed standard child
227
    Redmine::MenuManager.map :menu3 do |menu|
228
      menu.push(:parent_node, nil, {})
229
      menu.push(:test_child_unallowed, {:controller =>'issues', :action => 'new'}, {:parent => :parent_node})
230
    end
231

  
232
    # should not be displayed to anonymous
233
    User.current = User.find(6)
234
    assert_nil render_menu(:menu1, Project.find(1))
235
    assert_nil render_menu(:menu2, Project.find(1))
236
    assert_nil render_menu(:menu3, Project.find(1))
237

  
238
    # should be displayed to an admin
239
    User.current = User.find(1)
240
    @output_buffer = render_menu(:menu2, Project.find(1))
241
    assert_select("ul li a.parent-node", "Parent node")
242
    @output_buffer = render_menu(:menu3, Project.find(1))
243
    assert_select("ul li a.parent-node", "Parent node")
244

  
245
  end
246

  
212 247
  def test_render_menu_node_with_children_without_an_array
213 248
    parent_node = Redmine::MenuManager::MenuItem.new(:parent_node,
214 249
                                                     '/test',
(2-2/11)