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

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

Instance Method Details

#build_for(context) ⇒ Object

Evaluates the registered configurations for this menu in a view context



16
17
18
19
20
# File 'lib/decidim/menu.rb', line 16

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.



44
45
46
# File 'lib/decidim/menu.rb', line 44

def item(label, url, options = {})
  @items << MenuItem.new(label, url, options)
end

#itemsObject

The weighted list of items in the menu



51
52
53
# File 'lib/decidim/menu.rb', line 51

def items
  @items.select(&:visible?).sort_by(&:position)
end