Class: Kitestrings::Menu::Item
- Inherits:
-
Object
- Object
- Kitestrings::Menu::Item
- Defined in:
- lib/kitestrings/menu/item.rb
Overview
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
Instance Attribute Summary collapse
-
#active ⇒ Object
Returns the value of attribute active.
-
#resources ⇒ Object
readonly
Returns the value of attribute resources.
Instance Method Summary collapse
-
#active? ⇒ Boolean
Active is set on the last item in the Menu::ItemCollection based on the assumption that this is the active item in the collection.
- #collection? ⇒ Boolean
-
#hidden? ⇒ Boolean
Should the item be hidden in the menus? If so, menu_link_to will not generate any output.
-
#index_item ⇒ Object
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 #>.
-
#initialize(resources) ⇒ Item
constructor
A new instance of Item.
-
#link_id ⇒ Object
a string to use as a link id in the menu link.
-
#name ⇒ Object
return the menu display name for the object.
-
#object ⇒ Object
fetch the last object in the chain.
- #parent_item ⇒ Object
-
#partial_name ⇒ Object
return the name of a menu partial to use for contextual menus relating to this item.
-
#path ⇒ Object
the path of the item.
-
#persisted? ⇒ Boolean
true if the object is persisted.
-
#sub_item(*extras) ⇒ Object
return a new item with the given extra resources added to the resource list.
Constructor Details
#initialize(resources) ⇒ Item
Returns a new instance of Item.
10 11 12 |
# File 'lib/kitestrings/menu/item.rb', line 10 def initialize(resources) @resources = resources end |
Instance Attribute Details
#active ⇒ Object
Returns the value of attribute active.
8 9 10 |
# File 'lib/kitestrings/menu/item.rb', line 8 def active @active end |
#resources ⇒ Object (readonly)
Returns the value of attribute resources.
7 8 9 |
# File 'lib/kitestrings/menu/item.rb', line 7 def resources @resources end |
Instance Method Details
#active? ⇒ Boolean
Active is set on the last item in the Menu::ItemCollection based on the assumption that this is the active item in the collection.
63 64 65 |
# File 'lib/kitestrings/menu/item.rb', line 63 def active? @active end |
#collection? ⇒ Boolean
75 76 77 |
# File 'lib/kitestrings/menu/item.rb', line 75 def collection? object.is_a?(Class) end |
#hidden? ⇒ Boolean
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.
69 70 71 72 73 |
# File 'lib/kitestrings/menu/item.rb', line 69 def hidden? if object.respond_to? :menu_hidden object. end end |
#index_item ⇒ Object
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 #>
81 82 83 84 85 86 87 |
# 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 |
#link_id ⇒ Object
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
49 50 51 52 53 54 55 56 57 58 59 |
# 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 |
#name ⇒ Object
return the menu display name for the object. This defaults to the .name method of an instance, and the localised human name for classes.
30 31 32 |
# File 'lib/kitestrings/menu/item.rb', line 30 def name object.try(:menu_display_name) end |
#object ⇒ Object
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.
43 44 45 |
# File 'lib/kitestrings/menu/item.rb', line 43 def object @resources.last end |
#parent_item ⇒ Object
24 25 26 |
# File 'lib/kitestrings/menu/item.rb', line 24 def parent_item Item.new(resources[0..-2]) end |
#partial_name ⇒ Object
return the name of a menu partial to use for contextual menus relating to this item.
35 36 37 38 39 |
# 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 ⇒ Object
the path of the item. eg: /companies/1/projects/2/audits/3
15 16 17 |
# File 'lib/kitestrings/menu/item.rb', line 15 def path @path ||= polymorphic_path @resources end |
#persisted? ⇒ Boolean
true if the object is persisted. Classes are not persisted.
90 91 92 |
# File 'lib/kitestrings/menu/item.rb', line 90 def persisted? object.respond_to?(:persisted?) && object.persisted? end |