annotate .svn/pristine/67/67cbbb936bbd8a3a195700edbb70375a0e5e221d.svn-base @ 1433:cfa80f738847 bibliography_testing

Fixed assertion error message.
author luisf <luis.figueira@eecs.qmul.ac.uk>
date Tue, 08 Oct 2013 17:29:47 +0100
parents 038ba2d95de8
children
rev   line source
Chris@1296 1 # Redmine - project management software
Chris@1296 2 # Copyright (C) 2006-2012 Jean-Philippe Lang
Chris@1296 3 #
Chris@1296 4 # This program is free software; you can redistribute it and/or
Chris@1296 5 # modify it under the terms of the GNU General Public License
Chris@1296 6 # as published by the Free Software Foundation; either version 2
Chris@1296 7 # of the License, or (at your option) any later version.
Chris@1296 8 #
Chris@1296 9 # This program is distributed in the hope that it will be useful,
Chris@1296 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1296 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1296 12 # GNU General Public License for more details.
Chris@1296 13 #
Chris@1296 14 # You should have received a copy of the GNU General Public License
Chris@1296 15 # along with this program; if not, write to the Free Software
Chris@1296 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1296 17
Chris@1296 18 module Redmine
Chris@1296 19 module MenuManager
Chris@1296 20 class MenuError < StandardError #:nodoc:
Chris@1296 21 end
Chris@1296 22
Chris@1296 23 module MenuController
Chris@1296 24 def self.included(base)
Chris@1296 25 base.extend(ClassMethods)
Chris@1296 26 end
Chris@1296 27
Chris@1296 28 module ClassMethods
Chris@1296 29 @@menu_items = Hash.new {|hash, key| hash[key] = {:default => key, :actions => {}}}
Chris@1296 30 mattr_accessor :menu_items
Chris@1296 31
Chris@1296 32 # Set the menu item name for a controller or specific actions
Chris@1296 33 # Examples:
Chris@1296 34 # * menu_item :tickets # => sets the menu name to :tickets for the whole controller
Chris@1296 35 # * menu_item :tickets, :only => :list # => sets the menu name to :tickets for the 'list' action only
Chris@1296 36 # * menu_item :tickets, :only => [:list, :show] # => sets the menu name to :tickets for 2 actions only
Chris@1296 37 #
Chris@1296 38 # The default menu item name for a controller is controller_name by default
Chris@1296 39 # Eg. the default menu item name for ProjectsController is :projects
Chris@1296 40 def menu_item(id, options = {})
Chris@1296 41 if actions = options[:only]
Chris@1296 42 actions = [] << actions unless actions.is_a?(Array)
Chris@1296 43 actions.each {|a| menu_items[controller_name.to_sym][:actions][a.to_sym] = id}
Chris@1296 44 else
Chris@1296 45 menu_items[controller_name.to_sym][:default] = id
Chris@1296 46 end
Chris@1296 47 end
Chris@1296 48 end
Chris@1296 49
Chris@1296 50 def menu_items
Chris@1296 51 self.class.menu_items
Chris@1296 52 end
Chris@1296 53
Chris@1296 54 # Returns the menu item name according to the current action
Chris@1296 55 def current_menu_item
Chris@1296 56 @current_menu_item ||= menu_items[controller_name.to_sym][:actions][action_name.to_sym] ||
Chris@1296 57 menu_items[controller_name.to_sym][:default]
Chris@1296 58 end
Chris@1296 59
Chris@1296 60 # Redirects user to the menu item of the given project
Chris@1296 61 # Returns false if user is not authorized
Chris@1296 62 def redirect_to_project_menu_item(project, name)
Chris@1296 63 item = Redmine::MenuManager.items(:project_menu).detect {|i| i.name.to_s == name.to_s}
Chris@1296 64 if item && User.current.allowed_to?(item.url, project) && (item.condition.nil? || item.condition.call(project))
Chris@1296 65 redirect_to({item.param => project}.merge(item.url))
Chris@1296 66 return true
Chris@1296 67 end
Chris@1296 68 false
Chris@1296 69 end
Chris@1296 70 end
Chris@1296 71
Chris@1296 72 module MenuHelper
Chris@1296 73 # Returns the current menu item name
Chris@1296 74 def current_menu_item
Chris@1296 75 controller.current_menu_item
Chris@1296 76 end
Chris@1296 77
Chris@1296 78 # Renders the application main menu
Chris@1296 79 def render_main_menu(project)
Chris@1296 80 render_menu((project && !project.new_record?) ? :project_menu : :application_menu, project)
Chris@1296 81 end
Chris@1296 82
Chris@1296 83 def display_main_menu?(project)
Chris@1296 84 menu_name = project && !project.new_record? ? :project_menu : :application_menu
Chris@1296 85 Redmine::MenuManager.items(menu_name).children.present?
Chris@1296 86 end
Chris@1296 87
Chris@1296 88 def render_menu(menu, project=nil)
Chris@1296 89 links = []
Chris@1296 90 menu_items_for(menu, project) do |node|
Chris@1296 91 links << render_menu_node(node, project)
Chris@1296 92 end
Chris@1296 93 links.empty? ? nil : content_tag('ul', links.join("\n").html_safe)
Chris@1296 94 end
Chris@1296 95
Chris@1296 96 def render_menu_node(node, project=nil)
Chris@1296 97 if node.children.present? || !node.child_menus.nil?
Chris@1296 98 return render_menu_node_with_children(node, project)
Chris@1296 99 else
Chris@1296 100 caption, url, selected = extract_node_details(node, project)
Chris@1296 101 return content_tag('li',
Chris@1296 102 render_single_menu_node(node, caption, url, selected))
Chris@1296 103 end
Chris@1296 104 end
Chris@1296 105
Chris@1296 106 def render_menu_node_with_children(node, project=nil)
Chris@1296 107 caption, url, selected = extract_node_details(node, project)
Chris@1296 108
Chris@1296 109 html = [].tap do |html|
Chris@1296 110 html << '<li>'
Chris@1296 111 # Parent
Chris@1296 112 html << render_single_menu_node(node, caption, url, selected)
Chris@1296 113
Chris@1296 114 # Standard children
Chris@1296 115 standard_children_list = "".html_safe.tap do |child_html|
Chris@1296 116 node.children.each do |child|
Chris@1296 117 child_html << render_menu_node(child, project)
Chris@1296 118 end
Chris@1296 119 end
Chris@1296 120
Chris@1296 121 html << content_tag(:ul, standard_children_list, :class => 'menu-children') unless standard_children_list.empty?
Chris@1296 122
Chris@1296 123 # Unattached children
Chris@1296 124 unattached_children_list = render_unattached_children_menu(node, project)
Chris@1296 125 html << content_tag(:ul, unattached_children_list, :class => 'menu-children unattached') unless unattached_children_list.blank?
Chris@1296 126
Chris@1296 127 html << '</li>'
Chris@1296 128 end
Chris@1296 129 return html.join("\n").html_safe
Chris@1296 130 end
Chris@1296 131
Chris@1296 132 # Returns a list of unattached children menu items
Chris@1296 133 def render_unattached_children_menu(node, project)
Chris@1296 134 return nil unless node.child_menus
Chris@1296 135
Chris@1296 136 "".html_safe.tap do |child_html|
Chris@1296 137 unattached_children = node.child_menus.call(project)
Chris@1296 138 # Tree nodes support #each so we need to do object detection
Chris@1296 139 if unattached_children.is_a? Array
Chris@1296 140 unattached_children.each do |child|
Chris@1296 141 child_html << content_tag(:li, render_unattached_menu_item(child, project))
Chris@1296 142 end
Chris@1296 143 else
Chris@1296 144 raise MenuError, ":child_menus must be an array of MenuItems"
Chris@1296 145 end
Chris@1296 146 end
Chris@1296 147 end
Chris@1296 148
Chris@1296 149 def render_single_menu_node(item, caption, url, selected)
Chris@1296 150 link_to(h(caption), url, item.html_options(:selected => selected))
Chris@1296 151 end
Chris@1296 152
Chris@1296 153 def render_unattached_menu_item(menu_item, project)
Chris@1296 154 raise MenuError, ":child_menus must be an array of MenuItems" unless menu_item.is_a? MenuItem
Chris@1296 155
Chris@1296 156 if User.current.allowed_to?(menu_item.url, project)
Chris@1296 157 link_to(h(menu_item.caption),
Chris@1296 158 menu_item.url,
Chris@1296 159 menu_item.html_options)
Chris@1296 160 end
Chris@1296 161 end
Chris@1296 162
Chris@1296 163 def menu_items_for(menu, project=nil)
Chris@1296 164 items = []
Chris@1296 165 Redmine::MenuManager.items(menu).root.children.each do |node|
Chris@1296 166 if allowed_node?(node, User.current, project)
Chris@1296 167 if block_given?
Chris@1296 168 yield node
Chris@1296 169 else
Chris@1296 170 items << node # TODO: not used?
Chris@1296 171 end
Chris@1296 172 end
Chris@1296 173 end
Chris@1296 174 return block_given? ? nil : items
Chris@1296 175 end
Chris@1296 176
Chris@1296 177 def extract_node_details(node, project=nil)
Chris@1296 178 item = node
Chris@1296 179 url = case item.url
Chris@1296 180 when Hash
Chris@1296 181 project.nil? ? item.url : {item.param => project}.merge(item.url)
Chris@1296 182 when Symbol
Chris@1296 183 send(item.url)
Chris@1296 184 else
Chris@1296 185 item.url
Chris@1296 186 end
Chris@1296 187 caption = item.caption(project)
Chris@1296 188 return [caption, url, (current_menu_item == item.name)]
Chris@1296 189 end
Chris@1296 190
Chris@1296 191 # Checks if a user is allowed to access the menu item by:
Chris@1296 192 #
Chris@1296 193 # * Checking the conditions of the item
Chris@1296 194 # * Checking the url target (project only)
Chris@1296 195 def allowed_node?(node, user, project)
Chris@1296 196 if node.condition && !node.condition.call(project)
Chris@1296 197 # Condition that doesn't pass
Chris@1296 198 return false
Chris@1296 199 end
Chris@1296 200
Chris@1296 201 if project
Chris@1296 202 return user && user.allowed_to?(node.url, project)
Chris@1296 203 else
Chris@1296 204 # outside a project, all menu items allowed
Chris@1296 205 return true
Chris@1296 206 end
Chris@1296 207 end
Chris@1296 208 end
Chris@1296 209
Chris@1296 210 class << self
Chris@1296 211 def map(menu_name)
Chris@1296 212 @items ||= {}
Chris@1296 213 mapper = Mapper.new(menu_name.to_sym, @items)
Chris@1296 214 if block_given?
Chris@1296 215 yield mapper
Chris@1296 216 else
Chris@1296 217 mapper
Chris@1296 218 end
Chris@1296 219 end
Chris@1296 220
Chris@1296 221 def items(menu_name)
Chris@1296 222 @items[menu_name.to_sym] || MenuNode.new(:root, {})
Chris@1296 223 end
Chris@1296 224 end
Chris@1296 225
Chris@1296 226 class Mapper
Chris@1296 227 def initialize(menu, items)
Chris@1296 228 items[menu] ||= MenuNode.new(:root, {})
Chris@1296 229 @menu = menu
Chris@1296 230 @menu_items = items[menu]
Chris@1296 231 end
Chris@1296 232
Chris@1296 233 # Adds an item at the end of the menu. Available options:
Chris@1296 234 # * param: the parameter name that is used for the project id (default is :id)
Chris@1296 235 # * if: a Proc that is called before rendering the item, the item is displayed only if it returns true
Chris@1296 236 # * caption that can be:
Chris@1296 237 # * a localized string Symbol
Chris@1296 238 # * a String
Chris@1296 239 # * a Proc that can take the project as argument
Chris@1296 240 # * before, after: specify where the menu item should be inserted (eg. :after => :activity)
Chris@1296 241 # * parent: menu item will be added as a child of another named menu (eg. :parent => :issues)
Chris@1296 242 # * 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.
Chris@1296 243 # eg. :children => Proc.new {|project| [Redmine::MenuManager::MenuItem.new(...)] }
Chris@1296 244 # * last: menu item will stay at the end (eg. :last => true)
Chris@1296 245 # * html_options: a hash of html options that are passed to link_to
Chris@1296 246 def push(name, url, options={})
Chris@1296 247 options = options.dup
Chris@1296 248
Chris@1296 249 if options[:parent]
Chris@1296 250 subtree = self.find(options[:parent])
Chris@1296 251 if subtree
Chris@1296 252 target_root = subtree
Chris@1296 253 else
Chris@1296 254 target_root = @menu_items.root
Chris@1296 255 end
Chris@1296 256
Chris@1296 257 else
Chris@1296 258 target_root = @menu_items.root
Chris@1296 259 end
Chris@1296 260
Chris@1296 261 # menu item position
Chris@1296 262 if first = options.delete(:first)
Chris@1296 263 target_root.prepend(MenuItem.new(name, url, options))
Chris@1296 264 elsif before = options.delete(:before)
Chris@1296 265
Chris@1296 266 if exists?(before)
Chris@1296 267 target_root.add_at(MenuItem.new(name, url, options), position_of(before))
Chris@1296 268 else
Chris@1296 269 target_root.add(MenuItem.new(name, url, options))
Chris@1296 270 end
Chris@1296 271
Chris@1296 272 elsif after = options.delete(:after)
Chris@1296 273
Chris@1296 274 if exists?(after)
Chris@1296 275 target_root.add_at(MenuItem.new(name, url, options), position_of(after) + 1)
Chris@1296 276 else
Chris@1296 277 target_root.add(MenuItem.new(name, url, options))
Chris@1296 278 end
Chris@1296 279
Chris@1296 280 elsif options[:last] # don't delete, needs to be stored
Chris@1296 281 target_root.add_last(MenuItem.new(name, url, options))
Chris@1296 282 else
Chris@1296 283 target_root.add(MenuItem.new(name, url, options))
Chris@1296 284 end
Chris@1296 285 end
Chris@1296 286
Chris@1296 287 # Removes a menu item
Chris@1296 288 def delete(name)
Chris@1296 289 if found = self.find(name)
Chris@1296 290 @menu_items.remove!(found)
Chris@1296 291 end
Chris@1296 292 end
Chris@1296 293
Chris@1296 294 # Checks if a menu item exists
Chris@1296 295 def exists?(name)
Chris@1296 296 @menu_items.any? {|node| node.name == name}
Chris@1296 297 end
Chris@1296 298
Chris@1296 299 def find(name)
Chris@1296 300 @menu_items.find {|node| node.name == name}
Chris@1296 301 end
Chris@1296 302
Chris@1296 303 def position_of(name)
Chris@1296 304 @menu_items.each do |node|
Chris@1296 305 if node.name == name
Chris@1296 306 return node.position
Chris@1296 307 end
Chris@1296 308 end
Chris@1296 309 end
Chris@1296 310 end
Chris@1296 311
Chris@1296 312 class MenuNode
Chris@1296 313 include Enumerable
Chris@1296 314 attr_accessor :parent
Chris@1296 315 attr_reader :last_items_count, :name
Chris@1296 316
Chris@1296 317 def initialize(name, content = nil)
Chris@1296 318 @name = name
Chris@1296 319 @children = []
Chris@1296 320 @last_items_count = 0
Chris@1296 321 end
Chris@1296 322
Chris@1296 323 def children
Chris@1296 324 if block_given?
Chris@1296 325 @children.each {|child| yield child}
Chris@1296 326 else
Chris@1296 327 @children
Chris@1296 328 end
Chris@1296 329 end
Chris@1296 330
Chris@1296 331 # Returns the number of descendants + 1
Chris@1296 332 def size
Chris@1296 333 @children.inject(1) {|sum, node| sum + node.size}
Chris@1296 334 end
Chris@1296 335
Chris@1296 336 def each &block
Chris@1296 337 yield self
Chris@1296 338 children { |child| child.each(&block) }
Chris@1296 339 end
Chris@1296 340
Chris@1296 341 # Adds a child at first position
Chris@1296 342 def prepend(child)
Chris@1296 343 add_at(child, 0)
Chris@1296 344 end
Chris@1296 345
Chris@1296 346 # Adds a child at given position
Chris@1296 347 def add_at(child, position)
Chris@1296 348 raise "Child already added" if find {|node| node.name == child.name}
Chris@1296 349
Chris@1296 350 @children = @children.insert(position, child)
Chris@1296 351 child.parent = self
Chris@1296 352 child
Chris@1296 353 end
Chris@1296 354
Chris@1296 355 # Adds a child as last child
Chris@1296 356 def add_last(child)
Chris@1296 357 add_at(child, -1)
Chris@1296 358 @last_items_count += 1
Chris@1296 359 child
Chris@1296 360 end
Chris@1296 361
Chris@1296 362 # Adds a child
Chris@1296 363 def add(child)
Chris@1296 364 position = @children.size - @last_items_count
Chris@1296 365 add_at(child, position)
Chris@1296 366 end
Chris@1296 367 alias :<< :add
Chris@1296 368
Chris@1296 369 # Removes a child
Chris@1296 370 def remove!(child)
Chris@1296 371 @children.delete(child)
Chris@1296 372 @last_items_count -= +1 if child && child.last
Chris@1296 373 child.parent = nil
Chris@1296 374 child
Chris@1296 375 end
Chris@1296 376
Chris@1296 377 # Returns the position for this node in it's parent
Chris@1296 378 def position
Chris@1296 379 self.parent.children.index(self)
Chris@1296 380 end
Chris@1296 381
Chris@1296 382 # Returns the root for this node
Chris@1296 383 def root
Chris@1296 384 root = self
Chris@1296 385 root = root.parent while root.parent
Chris@1296 386 root
Chris@1296 387 end
Chris@1296 388 end
Chris@1296 389
Chris@1296 390 class MenuItem < MenuNode
Chris@1296 391 include Redmine::I18n
Chris@1296 392 attr_reader :name, :url, :param, :condition, :parent, :child_menus, :last
Chris@1296 393
Chris@1296 394 def initialize(name, url, options)
Chris@1296 395 raise ArgumentError, "Invalid option :if for menu item '#{name}'" if options[:if] && !options[:if].respond_to?(:call)
Chris@1296 396 raise ArgumentError, "Invalid option :html for menu item '#{name}'" if options[:html] && !options[:html].is_a?(Hash)
Chris@1296 397 raise ArgumentError, "Cannot set the :parent to be the same as this item" if options[:parent] == name.to_sym
Chris@1296 398 raise ArgumentError, "Invalid option :children for menu item '#{name}'" if options[:children] && !options[:children].respond_to?(:call)
Chris@1296 399 @name = name
Chris@1296 400 @url = url
Chris@1296 401 @condition = options[:if]
Chris@1296 402 @param = options[:param] || :id
Chris@1296 403 @caption = options[:caption]
Chris@1296 404 @html_options = options[:html] || {}
Chris@1296 405 # Adds a unique class to each menu item based on its name
Chris@1296 406 @html_options[:class] = [@html_options[:class], @name.to_s.dasherize].compact.join(' ')
Chris@1296 407 @parent = options[:parent]
Chris@1296 408 @child_menus = options[:children]
Chris@1296 409 @last = options[:last] || false
Chris@1296 410 super @name.to_sym
Chris@1296 411 end
Chris@1296 412
Chris@1296 413 def caption(project=nil)
Chris@1296 414 if @caption.is_a?(Proc)
Chris@1296 415 c = @caption.call(project).to_s
Chris@1296 416 c = @name.to_s.humanize if c.blank?
Chris@1296 417 c
Chris@1296 418 else
Chris@1296 419 if @caption.nil?
Chris@1296 420 l_or_humanize(name, :prefix => 'label_')
Chris@1296 421 else
Chris@1296 422 @caption.is_a?(Symbol) ? l(@caption) : @caption
Chris@1296 423 end
Chris@1296 424 end
Chris@1296 425 end
Chris@1296 426
Chris@1296 427 def html_options(options={})
Chris@1296 428 if options[:selected]
Chris@1296 429 o = @html_options.dup
Chris@1296 430 o[:class] += ' selected'
Chris@1296 431 o
Chris@1296 432 else
Chris@1296 433 @html_options
Chris@1296 434 end
Chris@1296 435 end
Chris@1296 436 end
Chris@1296 437 end
Chris@1296 438 end