Class: Decidim::Menu

Inherits:
Object
  • Object
show all
Defined in:
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 '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.



74
75
76
77
# File 'lib/decidim/menu.rb', line 74

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
# File 'lib/decidim/menu.rb', line 18

def build_for(context)
  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.



46
47
48
49
# File 'lib/decidim/menu.rb', line 46

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



103
104
105
106
107
# File 'lib/decidim/menu.rb', line 103

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



79
80
81
82
83
84
85
86
87
# File 'lib/decidim/menu.rb', line 79

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



96
97
98
# File 'lib/decidim/menu.rb', line 96

def remove_item(item)
  @removed_items << item
end