Class: Decidim::Menu
- Inherits:
-
Object
- Object
- Decidim::Menu
- Defined in:
- lib/decidim/menu.rb
Overview
This class handles all logic regarding registering menus
Instance Method Summary collapse
-
#add_item(identifier, label, url, options = {}) ⇒ Object
Public: Registers a new item for the menu.
-
#build_for(context) ⇒ Object
Evaluates the registered configurations for this menu in a view context.
-
#initialize(name) ⇒ Menu
constructor
A new instance of Menu.
-
#item(label, url, options = {}) ⇒ Object
Public: Registers a new item for the menu.
-
#items ⇒ Object
The weighted list of items in the menu.
- #move(element, after: nil, before: nil) ⇒ Object
-
#remove_item(item) ⇒ Object
Public: Registers a new item for the menu.
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
76 77 78 79 |
# File 'lib/decidim/menu.rb', line 76 def add_item(identifier, label, url, = {}) = { position: (1 + @items.length) }.merge() @items << MenuItem.new(label, url, identifier, ) end |
#build_for(context) ⇒ Object
Evaluates the registered configurations for this menu in a view context
18 19 20 21 22 23 24 |
# File '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
48 49 50 51 |
# File 'lib/decidim/menu.rb', line 48 def item(label, url, = {}) ActiveSupport::Deprecation.warn("Using menu.item in #{@name} context is deprecated. Use menu.add_item") add_item(nil, label, url, ) end |
#items ⇒ Object
The weighted list of items in the menu
105 106 107 108 109 |
# File '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 '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
98 99 100 |
# File 'lib/decidim/menu.rb', line 98 def remove_item(item) @removed_items << item end |