class Kitestrings::Menu::Item

A class to represent an item in the menu. It is capable of looking up menu item paths and names and a link id, all the required data to construct links, include links to subpaths

Attributes

active[RW]
resources[R]

Public Class Methods

new(resources) click to toggle source
# File lib/kitestrings/menu/item.rb, line 10
def initialize(resources)
  @resources = resources
end

Public Instance Methods

active?() click to toggle source

Active is set on the last item in the Menu::ItemCollection based on the assumption that this is the active item in the collection.

# File lib/kitestrings/menu/item.rb, line 63
def active?
  @active
end
collection?() click to toggle source
# File lib/kitestrings/menu/item.rb, line 75
def collection?
  object.is_a?(Class)
end
hidden?() click to toggle source

Should the item be hidden in the menus? If so, menu_link_to will not generate any output. Implement a menu_hidden instance or class method to hide that instance or class from the menu.

# File lib/kitestrings/menu/item.rb, line 69
def hidden?
  if object.respond_to? :menu_hidden
    object.menu_hidden
  end
end
index_item() click to toggle source

if this item is for a resource instance (eg: companies/1 => <# Company id:1 #>, then make a new item representing the collection of that class, eg: companies/ => <# Company class #>

# File lib/kitestrings/menu/item.rb, line 81
def index_item
  if @resources.last && @resources.last.is_a?(ActiveRecord::Base)
    index_resources = @resources.dup
    index_resources << index_resources.pop.class
    Item.new index_resources
  end
end
name() click to toggle source

return the menu display name for the object. This defaults to the .name method of an instance, and the localised human name for classes.

# File lib/kitestrings/menu/item.rb, line 30
def name
  object.try(:menu_display_name)
end
object() click to toggle source

fetch the last object in the chain. This is the resource that is actually being rendered. If this is an index action, this will be the class representing the type to be shown in the list.

# File lib/kitestrings/menu/item.rb, line 43
def object
  @resources.last
end
parent_item() click to toggle source
# File lib/kitestrings/menu/item.rb, line 24
def parent_item
  Item.new(resources[0..-2])
end
partial_name() click to toggle source

return the name of a menu partial to use for contextual menus relating to this item.

# File lib/kitestrings/menu/item.rb, line 35
def partial_name
  last = @resources.last
  klass = last.is_a?(Class) ? last : last.class
  klass.model_name.underscore
end
path() click to toggle source

the path of the item. eg: /companies/1/projects/2/audits/3

# File lib/kitestrings/menu/item.rb, line 15
def path
  @path ||= polymorphic_path @resources
end
persisted?() click to toggle source

true if the object is persisted. Classes are not persisted.

# File lib/kitestrings/menu/item.rb, line 90
def persisted?
  object.respond_to?(:persisted?) && object.persisted?
end
sub_item(*extras) click to toggle source

return a new item with the given extra resources added to the resource list.

# File lib/kitestrings/menu/item.rb, line 20
def sub_item(*extras)
  Item.new([*@resources, *extras].compact)
end