Class SimpleNavigation::Item
In: lib/simple_navigation/core/item.rb
lib/simple_navigation/rails_controller_methods.rb
Parent: Object

Represents an item in your navigation. Gets generated by the item method in the config-file.

Methods

Attributes

highlights_on  [R] 
html_options  [W] 
key  [R] 
method  [R] 
name  [R] 
sub_navigation  [R] 
url  [R] 

Public Class methods

see ItemContainer#item

The subnavigation (if any) is either provided by a block or passed in directly as items

[Source]

# File lib/simple_navigation/core/item.rb, line 11
    def initialize(container, key, name, url, options, items=nil, &sub_nav_block)
      @container = container
      @key = key
      @method = options.delete(:method)
      @name = name
      @url = url
      @highlights_on = options.delete(:highlights_on)
      @html_options = options
      if sub_nav_block || items
        @sub_navigation = ItemContainer.new(@container.level + 1)
        sub_nav_block.call @sub_navigation if sub_nav_block
        @sub_navigation.items = items if items
      end
    end

Public Instance methods

Returns the html-options hash for the item, i.e. the options specified for this item in the config-file. It also adds the ‘selected’ class to the list of classes if necessary.

[Source]

# File lib/simple_navigation/core/item.rb, line 39
    def html_options
      default_options = self.autogenerate_item_ids? ? {:id => autogenerated_item_id} : {}
      options = default_options.merge(@html_options)
      options[:class] = [@html_options[:class], self.selected_class].flatten.compact.join(' ')
      options.delete(:class) if options[:class].nil? || options[:class] == ''
      options
    end

Returns true if this navigation item should be rendered as ‘selected’. An item is selected if

  • it has been explicitly selected in a controller or
  • it has a subnavigation and one of its subnavigation items is selected or
  • its url matches the url of the current request (auto highlighting)

[Source]

# File lib/simple_navigation/core/item.rb, line 33
    def selected?
      @selected = @selected || selected_by_config? || selected_by_subnav? || selected_by_url?
    end

[Source]

# File lib/simple_navigation/rails_controller_methods.rb, line 128
    def selected_by_config?
      key == SimpleNavigation.current_navigation_for(@container.level)
    end

Returns the configured selected_class if the item is selected, nil otherwise

[Source]

# File lib/simple_navigation/core/item.rb, line 49
    def selected_class
      selected? ? SimpleNavigation.config.selected_class : nil
    end

Protected Instance methods

Return true if auto_highlight is on for this item.

[Source]

# File lib/simple_navigation/core/item.rb, line 92
    def auto_highlight?
      SimpleNavigation.config.auto_highlight && @container.auto_highlight
    end

Returns true if the item‘s id should be added to the rendered output.

[Source]

# File lib/simple_navigation/core/item.rb, line 82
    def autogenerate_item_ids?
      SimpleNavigation.config.autogenerate_item_ids
    end

Returns the item‘s id which is added to the rendered output.

[Source]

# File lib/simple_navigation/core/item.rb, line 87
    def autogenerated_item_id
      SimpleNavigation.config.id_generator.call(key)
    end

Returns true if both the item‘s url and the request‘s url are root_path

[Source]

# File lib/simple_navigation/core/item.rb, line 77
    def root_path_match?
      url == '/' && SimpleNavigation.request_path == '/'
    end

[Source]

# File lib/simple_navigation/core/item.rb, line 60
    def selected_by_config?
      false
    end

Returns true if item has a subnavigation and the sub_navigation is selected

[Source]

# File lib/simple_navigation/core/item.rb, line 56
    def selected_by_subnav?
      sub_navigation && sub_navigation.selected?
    end

Returns true if the item‘s url matches the request‘s current url.

[Source]

# File lib/simple_navigation/core/item.rb, line 65
    def selected_by_url?
      if highlights_on
        raise ArgumentError, ':highlights_on must be a regexp' unless highlights_on.instance_of?(Regexp)
        SimpleNavigation.request_uri =~ highlights_on
      elsif auto_highlight?
        !!(root_path_match? || SimpleNavigation.current_page?(url))
      else
        false
      end
    end

[Validate]