Class: Redmine::MenuManager::MenuItem
- Includes:
- I18n
- Defined in:
- lib/redmine/menu_manager.rb
Instance Attribute Summary collapse
-
#child_menus ⇒ Object
readonly
Returns the value of attribute child_menus.
-
#condition ⇒ Object
readonly
Returns the value of attribute condition.
-
#last ⇒ Object
readonly
Returns the value of attribute last.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#param ⇒ Object
readonly
Returns the value of attribute param.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#permission ⇒ Object
readonly
Returns the value of attribute permission.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Attributes inherited from MenuNode
Instance Method Summary collapse
-
#allowed?(user, project) ⇒ Boolean
Checks if a user is allowed to access the menu item by:.
- #caption(project = nil) ⇒ Object
- #html_options(options = {}) ⇒ Object
-
#initialize(name, url, options = {}) ⇒ MenuItem
constructor
A new instance of MenuItem.
Methods included from I18n
#current_language, #day_letter, #day_name, #find_language, #format_date, #format_hours, #format_time, included, #l, #l_hours, #l_hours_short, #l_or_humanize, #languages_options, #ll, #lu, #month_name, #set_language_if_valid, #valid_languages
Methods inherited from MenuNode
#add, #add_at, #add_last, #children, #each, #position, #prepend, #remove!, #root, #size
Constructor Details
#initialize(name, url, options = {}) ⇒ MenuItem
Returns a new instance of MenuItem
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 |
# File 'lib/redmine/menu_manager.rb', line 407 def initialize(name, url, ={}) raise ArgumentError, "Invalid option :if for menu item '#{name}'" if [:if] && ![:if].respond_to?(:call) raise ArgumentError, "Invalid option :html for menu item '#{name}'" if [:html] && ![:html].is_a?(Hash) raise ArgumentError, "Cannot set the :parent to be the same as this item" if [:parent] == name.to_sym raise ArgumentError, "Invalid option :children for menu item '#{name}'" if [:children] && ![:children].respond_to?(:call) @name = name @url = url @condition = [:if] @permission = [:permission] @permission ||= false if .key?(:permission) @param = [:param] || :id @caption = [:caption] @html_options = [:html] || {} # Adds a unique class to each menu item based on its name @html_options[:class] = [@html_options[:class], @name.to_s.dasherize].compact.join(' ') @parent = [:parent] @child_menus = [:children] @last = [:last] || false super @name.to_sym end |
Instance Attribute Details
#child_menus ⇒ Object (readonly)
Returns the value of attribute child_menus
405 406 407 |
# File 'lib/redmine/menu_manager.rb', line 405 def @child_menus end |
#condition ⇒ Object (readonly)
Returns the value of attribute condition
405 406 407 |
# File 'lib/redmine/menu_manager.rb', line 405 def condition @condition end |
#last ⇒ Object (readonly)
Returns the value of attribute last
405 406 407 |
# File 'lib/redmine/menu_manager.rb', line 405 def last @last end |
#name ⇒ Object (readonly)
Returns the value of attribute name
405 406 407 |
# File 'lib/redmine/menu_manager.rb', line 405 def name @name end |
#param ⇒ Object (readonly)
Returns the value of attribute param
405 406 407 |
# File 'lib/redmine/menu_manager.rb', line 405 def param @param end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent
405 406 407 |
# File 'lib/redmine/menu_manager.rb', line 405 def parent @parent end |
#permission ⇒ Object (readonly)
Returns the value of attribute permission
405 406 407 |
# File 'lib/redmine/menu_manager.rb', line 405 def @permission end |
#url ⇒ Object (readonly)
Returns the value of attribute url
405 406 407 |
# File 'lib/redmine/menu_manager.rb', line 405 def url @url end |
Instance Method Details
#allowed?(user, project) ⇒ Boolean
Checks if a user is allowed to access the menu item by:
-
Checking the permission or the url target (project only)
-
Checking the conditions of the item
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 |
# File 'lib/redmine/menu_manager.rb', line 456 def allowed?(user, project) if url.blank? # this is a virtual node that is only there for its children to be diplayed in the menu # it is considered an allowed node if at least one of the children is allowed all_children = children all_children += .call(project) if return false unless all_children.detect{|child| child.allowed?(user, project) } elsif user && project if unless user.allowed_to?(, project) return false end elsif .nil? && url.is_a?(Hash) unless user.allowed_to?(url, project) return false end end end if condition && !condition.call(project) # Condition that doesn't pass return false end return true end |
#caption(project = nil) ⇒ Object
428 429 430 431 432 433 434 435 436 437 438 439 440 |
# File 'lib/redmine/menu_manager.rb', line 428 def (project=nil) if @caption.is_a?(Proc) c = @caption.call(project).to_s c = @name.to_s.humanize if c.blank? c else if @caption.nil? l_or_humanize(name, :prefix => 'label_') else @caption.is_a?(Symbol) ? l(@caption) : @caption end end end |
#html_options(options = {}) ⇒ Object
442 443 444 445 446 447 448 449 450 |
# File 'lib/redmine/menu_manager.rb', line 442 def (={}) if [:selected] o = @html_options.dup o[:class] += ' selected' o else @html_options end end |