Class: Redmine::MenuManager::MenuItem

Inherits:
MenuNode
  • Object
show all
Includes:
I18n
Defined in:
lib/redmine/menu_manager.rb

Overview

Since:

  • 0.6.0

Instance Attribute Summary collapse

Attributes inherited from MenuNode

#last_items_count

Instance Method Summary collapse

Methods included from I18n

#current_language, #day_letter, #day_name, #find_language, #format_date, #format_hours, #format_time, #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

Raises:

  • (ArgumentError)


416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# File 'lib/redmine/menu_manager.rb', line 416

def initialize(name, url, options={})
  raise ArgumentError, "Invalid option :if for menu item '#{name}'" if options[:if] && !options[:if].respond_to?(:call)
  raise ArgumentError, "Invalid option :html for menu item '#{name}'" if options[:html] && !options[:html].is_a?(Hash)
  raise ArgumentError, "Cannot set the :parent to be the same as this item" if options[:parent] == name.to_sym
  raise ArgumentError, "Invalid option :children for menu item '#{name}'" if options[:children] && !options[:children].respond_to?(:call)
  @name = name
  @url = url
  @condition = options[:if]
  @permission = options[:permission]
  @permission ||= false if options.key?(:permission)
  @param = options[:param] || :id
  @caption = options[:caption]
  @html_options = 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 = options[:parent]
  @child_menus = options[:children]
  @last = options[:last] || false
  super @name.to_sym
end

Instance Attribute Details

#child_menusObject (readonly)

Returns the value of attribute child_menus

Since:

  • 0.9.0



414
415
416
# File 'lib/redmine/menu_manager.rb', line 414

def child_menus
  @child_menus
end

#conditionObject (readonly)

Returns the value of attribute condition



414
415
416
# File 'lib/redmine/menu_manager.rb', line 414

def condition
  @condition
end

#lastObject (readonly)

Returns the value of attribute last

Since:

  • 0.9.3



414
415
416
# File 'lib/redmine/menu_manager.rb', line 414

def last
  @last
end

#nameObject (readonly)

Returns the value of attribute name



414
415
416
# File 'lib/redmine/menu_manager.rb', line 414

def name
  @name
end

#paramObject (readonly)

Returns the value of attribute param



414
415
416
# File 'lib/redmine/menu_manager.rb', line 414

def param
  @param
end

#parentObject (readonly)

Returns the value of attribute parent

Since:

  • 0.9.0



414
415
416
# File 'lib/redmine/menu_manager.rb', line 414

def parent
  @parent
end

#permissionObject (readonly)

Returns the value of attribute permission

Since:

  • 3.0.0



414
415
416
# File 'lib/redmine/menu_manager.rb', line 414

def permission
  @permission
end

#urlObject (readonly)

Returns the value of attribute url



414
415
416
# File 'lib/redmine/menu_manager.rb', line 414

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

Returns:

  • (Boolean)

Since:

  • 3.0.0



465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
# File 'lib/redmine/menu_manager.rb', line 465

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 += child_menus.call(project) if child_menus
    return false unless all_children.detect{|child| child.allowed?(user, project) }
  elsif user && project
    if permission
      unless user.allowed_to?(permission, project)
        return false
      end
    elsif permission.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

Since:

  • 0.7.0



437
438
439
440
441
442
443
444
445
446
447
448
449
# File 'lib/redmine/menu_manager.rb', line 437

def caption(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

Since:

  • 0.7.0



451
452
453
454
455
456
457
458
459
# File 'lib/redmine/menu_manager.rb', line 451

def html_options(options={})
  if options[:selected]
    o = @html_options.dup
    o[:class] += ' selected'
    o
  else
    @html_options
  end
end