Class: Decidim::Menu

Inherits:
Object
  • Object
show all
Defined in:
decidim-core/lib/decidim/menu.rb

Overview

This class handles all logic regarding registering menus

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Menu

Returns a new instance of Menu.



8
9
10
11
12
13
# File 'decidim-core/lib/decidim/menu.rb', line 8

def initialize(name)
  @name = name
  @items = []
  @removed_items = []
  @ordered_elements = []
end

Instance Method Details

#add_item(identifier, label, url, options = {}) ⇒ Object

Public: Registers a new item for the menu

Examples:


menu.add_item :resources, "My Resource", "/resources"
menu.add_item :meetings, I18n.t("menu.meetings"), decidim_meetings.root_path
menu.add_item :profile, current_user.username, decidim.profile_path(current_user.nickname)
menu.add_item :processes,"Gestor de Procesos", "/processes", active: :exact
menu.add_item :processes,"Gestor de Procesos", "/processes", if: admin?

Parameters:

  • identifier (String, Symbol)

    A compulsory identifier for the menu item

  • label (String, Symbol)

    A compulsory label for the menu item

  • url (String, Symbol)

    The URL this item will link to

  • options (Hash) (defaults to: {})

    The options for the menu item

Options Hash (options):

  • :position (Float)

    The lower the position, the earlier in the menu the item will be displayed. Default: Float::INFINITY

  • :if (Symbol, Proc)

    Decides whether the menu item will be displayed. Evaluated on each request.



76
77
78
79
# File 'decidim-core/lib/decidim/menu.rb', line 76

def add_item(identifier, label, url, options = {})
  options = { position: (1 + @items.length) }.merge(options)
  @items << MenuItem.new(label, url, identifier, options)
end

#build_for(context) ⇒ Object

Evaluates the registered configurations for this menu in a view context



18
19
20
21
22
23
24
# File 'decidim-core/lib/decidim/menu.rb', line 18

def build_for(context)
  raise "Menu #{@name} is not registered" if registry.blank?

  registry.configurations.each do |configuration|
    context.instance_exec(self, &configuration)
  end
end

#item(label, url, options = {}) ⇒ Object

Public: Registers a new item for the menu

Examples:


menu.item "My Resource", "/resources"
menu.item I18n.t("menu.meetings"), decidim_meetings.root_path
menu.item current_user.username, decidim.profile_path(current_user.nickname)
menu.item "Gestor de Procesos", "/processes", active: :exact
menu.item "Gestor de Procesos", "/processes", if: admin?

Parameters:

  • label (String, Symbol)

    A compulsory label for the menu item

  • url (String, Symbol)

    The URL this item will link to

  • options (Hash) (defaults to: {})

    The options for the menu item

Options Hash (options):

  • :position (Float)

    The lower the position, the earlier in the menu the item will be displayed. Default: Float::INFINITY

  • :if (Symbol, Proc)

    Decides whether the menu item will be displayed. Evaluated on each request.



48
49
50
51
# File 'decidim-core/lib/decidim/menu.rb', line 48

def item(label, url, options = {})
  ActiveSupport::Deprecation.warn("Using menu.item in #{@name} context is deprecated. Use menu.add_item")
  add_item(nil, label, url, options)
end

#itemsObject

The weighted list of items in the menu



105
106
107
108
109
# File 'decidim-core/lib/decidim/menu.rb', line 105

def items
  @items.reject! { |item| @removed_items.include?(item.identifier) }
  @ordered_elements.each { |item| move_element(**item) }
  @items.select(&:visible?).sort_by(&:position)
end

#move(element, after: nil, before: nil) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'decidim-core/lib/decidim/menu.rb', line 81

def move(element, after: nil, before: nil)
  if after.present?
    @ordered_elements << { movable: element, anchor: after, operation: :+ }
  elsif before.present?
    @ordered_elements << { movable: element, anchor: before, operation: :- }
  else
    raise ArgumentError, "The Decidim::Menu.move method has been called with invalid parameters"
  end
end

#remove_item(item) ⇒ Object

Public: Registers a new item for the menu

Examples:


menu.remove_item :root

Parameters:

  • identifier (String, Symbol)

    A compulsory label for the menu item



98
99
100
# File 'decidim-core/lib/decidim/menu.rb', line 98

def remove_item(item)
  @removed_items << item
end