Class: Redmine::MenuManager::Mapper

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(menu, items) ⇒ Mapper

Returns a new instance of Mapper.



272
273
274
275
276
# File 'lib/redmine/menu_manager.rb', line 272

def initialize(menu, items)
  items[menu] ||= MenuNode.new(:root, {})
  @menu = menu
  @menu_items = items[menu]
end

Instance Attribute Details

Returns the value of attribute menu.



270
271
272
# File 'lib/redmine/menu_manager.rb', line 270

def menu
  @menu
end

Returns the value of attribute menu_items.



270
271
272
# File 'lib/redmine/menu_manager.rb', line 270

def menu_items
  @menu_items
end

Instance Method Details

#delete(name) ⇒ Object

Removes a menu item



331
332
333
334
335
# File 'lib/redmine/menu_manager.rb', line 331

def delete(name)
  if found = self.find(name)
    @menu_items.remove!(found)
  end
end

#exists?(name) ⇒ Boolean

Checks if a menu item exists

Returns:

  • (Boolean)


338
339
340
# File 'lib/redmine/menu_manager.rb', line 338

def exists?(name)
  @menu_items.any? {|node| node.name == name}
end

#find(name) ⇒ Object



342
343
344
# File 'lib/redmine/menu_manager.rb', line 342

def find(name)
  @menu_items.find {|node| node.name == name}
end

#position_of(name) ⇒ Object



346
347
348
349
350
351
352
# File 'lib/redmine/menu_manager.rb', line 346

def position_of(name)
  @menu_items.each do |node|
    if node.name == name
      return node.position
    end
  end
end

#push(name, url, options = {}) ⇒ Object

Adds an item at the end of the menu. Available options:

  • param: the parameter name that is used for the project id (default is :id)

  • if: a Proc that is called before rendering the item, the item is displayed only if it returns true

  • caption that can be:

    • a localized string Symbol

    • a String

    • a Proc that can take the project as argument

  • before, after: specify where the menu item should be inserted (eg. :after => :activity)

  • parent: menu item will be added as a child of another named menu (eg. :parent => :issues)

  • children: a Proc that is called before rendering the item. The Proc should return an array of MenuItems, which will be added as children to this item. eg. :children => Proc.new {|project| [Redmine::MenuManager::MenuItem.new(…)] }

  • last: menu item will stay at the end (eg. :last => true)

  • html_options: a hash of html options that are passed to link_to



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/redmine/menu_manager.rb', line 291

def push(name, url, options={})
  options = options.dup

  if options[:parent]
    subtree = self.find(options[:parent])
    target_root = subtree || @menu_items.root

  else
    target_root = @menu_items.root
  end

  target_root.children.reject! {|item| item.name == name}

  # menu item position
  if first = options.delete(:first)
    target_root.prepend(MenuItem.new(name, url, options))
  elsif before = options.delete(:before)

    if exists?(before)
      target_root.add_at(MenuItem.new(name, url, options), position_of(before))
    else
      target_root.add(MenuItem.new(name, url, options))
    end

  elsif after = options.delete(:after)

    if exists?(after)
      target_root.add_at(MenuItem.new(name, url, options), position_of(after) + 1)
    else
      target_root.add(MenuItem.new(name, url, options))
    end

  elsif options[:last] # don't delete, needs to be stored
    target_root.add_last(MenuItem.new(name, url, options))
  else
    target_root.add(MenuItem.new(name, url, options))
  end
end