Class: Coroutine::TinyNavigation::Data::Item

Inherits:
Navigation
  • Object
show all
Defined in:
lib/tiny_navigation/data/item.rb

Overview

This class represents a navigation item. It holds all the data for item and provides a number of convenience methods related to that item.

Instance Attribute Summary

Attributes inherited from Navigation

#items, #name

Instance Method Summary collapse

Methods inherited from Navigation

#selected

Constructor Details

#initialize(name, current_controller, options = {}, &block) ⇒ Item

This method creates a new instance of a navigation item.

name is the name of the navigation item. The name should be used for the label text when rendering the navigation item.

current_controller the currently loaded controller

options provide configuration options and custom properties to the nav item. Currently, the only configuration option is :to which is used to generate the URL of the navigation item. All other options provided to via the options hash will be treated as custom properties on the navigation item. These custom properties can be accessed as methods on the navigation item.

The block given to the navigation item is used to define sub-navigation items of this item.



27
28
29
30
31
# File 'lib/tiny_navigation/data/item.rb', line 27

def initialize(name, current_controller, options={}, &block)
  super name, current_controller, &block
  set_controller_and_action options.delete(:to)
  @extra_options = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object

This method uses the extra_options hash takes precendence when looking for the called method. Otherwise, we’ll let the super-class forward the method call to the current controller.



53
54
55
56
57
58
59
# File 'lib/tiny_navigation/data/item.rb', line 53

def method_missing(method_name, *args) #:nodoc:
  if @extra_options.has_key? method_name
    @extra_options[method_name]
  else
    super method_name, *args
  end
end

Instance Method Details

#selected?Boolean

This method indicates whether the navigation item is currently selected.

This takes into account any sub-nav items such that a parent item is selected if it has a selected child.

Returns:

  • (Boolean)


37
38
39
# File 'lib/tiny_navigation/data/item.rb', line 37

def selected?
  @controller_name == @current_controller.controller_name || @items.any?(&:selected?)
end

#urlObject

This method returns the URL to which the navigation item points. This should be used in a scenario where the navigation item represents a link and the URL is the href of that link.



45
46
47
# File 'lib/tiny_navigation/data/item.rb', line 45

def url
  @current_controller.url_for :controller => @controller_name, :action => @action_name
end