class SimpleNavigation::Item
Represents an item in your navigation. Gets generated by the item method in the config-file.
Attributes
Public Class Methods
see SimpleNavigation::ItemContainer#item
The subnavigation (if any) is either provided by a block or passed in
directly as items
# File lib/simple_navigation/core/item.rb, line 17 def initialize(container, key, name, url_or_options = {}, options_or_nil = {}, items = nil, &sub_nav_block) options = setup_url_and_options(url_or_options, options_or_nil) @key = key @method = options.delete(:method) @name = name @container = setup_container(container, options) setup_sub_navigation(items, &sub_nav_block) end
Public Instance Methods
Returns the configured #active_leaf_class if the item is the selected leaf, nil otherwise
# File lib/simple_navigation/core/item.rb, line 71 def active_leaf_class if !selected_by_subnav? && selected_by_condition? SimpleNavigation.config.active_leaf_class end end
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.
# File lib/simple_navigation/core/item.rb, line 58 def html_options options = @html_options options[:id] ||= autogenerated_item_id if autogenerate_item_ids? classes = [@html_options[:class], selected_class, active_leaf_class] classes = classes.flatten.compact.join(' ') options[:class] = classes unless classes.nil? || classes.empty? options end
Returns the item's name. If :apply_generator option is set to true (default), the name will be passed to the name_generator specified in the configuration.
# File lib/simple_navigation/core/item.rb, line 33 def name(options = {}) options = { apply_generator: true }.merge(options) if (options[:apply_generator]) SimpleNavigation.config.name_generator.call(@name, self) else @name end 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)
# File lib/simple_navigation/core/item.rb, line 49 def selected? @selected ||= selected_by_config? || selected_by_subnav? || selected_by_condition? end
Returns the configured #selected_class if the item is selected, nil otherwise
# File lib/simple_navigation/core/item.rb, line 79 def selected_class if selected? container.selected_class || SimpleNavigation.config.selected_class end end
Protected Instance Methods
Return true if auto_highlight is on for this item.
# File lib/simple_navigation/core/item.rb, line 118 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.
# File lib/simple_navigation/core/item.rb, line 108 def autogenerate_item_ids? SimpleNavigation.config.autogenerate_item_ids end
Returns the item's id which is added to the rendered output.
# File lib/simple_navigation/core/item.rb, line 113 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
# File lib/simple_navigation/core/item.rb, line 103 def root_path_match? url == '/' && SimpleNavigation.request_path == '/' end
Returns true if the item's url matches the request's current url.
# File lib/simple_navigation/core/item.rb, line 98 def selected_by_condition? highlights_on ? selected_by_highlights_on? : selected_by_autohighlight? end
# File lib/simple_navigation/core/item.rb, line 93 def selected_by_config? false end
# File lib/simple_navigation/core/item.rb, line 122 def url_without_anchor url && url.split('#').first end
Private Instance Methods
# File lib/simple_navigation/core/item.rb, line 134 def selected_by_autohighlight? auto_highlight? && (root_path_match? || (url_without_anchor && SimpleNavigation.current_page?(url_without_anchor))) end
# File lib/simple_navigation/core/item.rb, line 141 def selected_by_highlights_on? return false unless highlights_on case highlights_on when Regexp then SimpleNavigation.request_uri =~ highlights_on when Proc then highlights_on.call when :subpath escaped_url = Regexp.escape(url_without_anchor) !!(SimpleNavigation.request_uri =~ /^#{escaped_url}(\/|$|\?)/i) else fail ArgumentError, ':highlights_on must be a Regexp, Proc or :subpath' end end
# File lib/simple_navigation/core/item.rb, line 155 def setup_container(container, options = {}) if options[:container_class] container.dom_class = options.delete(:container_class) end if options[:container_id] container.dom_id = options.delete(:container_id) end container.dom_attributes = if options[:container_attributes] options.delete(:container_attributes) else {} end if options[:selected_class] container.selected_class = options.delete(:selected_class) end container end
# File lib/simple_navigation/core/item.rb, line 177 def setup_url_and_options(url_or_options, options_or_nil) case url_or_options when Hash then options = url_or_options # there is no url when Proc then self.url = url_or_options.call else self.url = url_or_options end options ||= options_or_nil self.highlights_on = options.delete(:highlights_on) self.html_options = options end