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
Public Class Methods
# File lib/kitestrings/menu/item.rb, line 10 def initialize(resources) @resources = resources end
Public Instance Methods
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
# File lib/kitestrings/menu/item.rb, line 75 def collection? object.is_a?(Class) end
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
a string to use as a link id in the menu link. Eg: for the path /companies/1/projects/2, the link would be menu_companies_projects_link
# File lib/kitestrings/menu/item.rb, line 49 def link_id names = @resources.map do |resource| case resource when Class resource.name.pluralize.downcase else resource.class.name.downcase end end "menu_#{names.join('_')}_link" end
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
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
# File lib/kitestrings/menu/item.rb, line 24 def parent_item Item.new(resources[0..-2]) end
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
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
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
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