class SimpleNavigation::ItemContainer

Holds the Items for a navigation 'level'.

Attributes

auto_highlight[RW]
dom_attributes[W]
dom_class[RW]
dom_id[RW]
items[R]
level[R]
renderer[RW]
selected_class[RW]

Public Instance Methods

[](navi_key) click to toggle source

Returns the Item with the specified key, nil otherwise.

# File lib/simple_navigation/item_container.rb, line 80
def [](navi_key)
  items.find { |item| item.key == navi_key }
end
active_item_container_for(desired_level) click to toggle source

Returns the active item_container for the specified level (recursively looks up items in selected sub_navigation if level is deeper than this container's level).

# File lib/simple_navigation/item_container.rb, line 123
def active_item_container_for(desired_level)
  if level == desired_level
    self
  elsif selected_sub_navigation?
    selected_item.sub_navigation.active_item_container_for(desired_level)
  end
end
active_leaf_container() click to toggle source

Returns the deepest possible active item_container. (recursively searches in the sub_navigation if this container has a selected sub_navigation).

# File lib/simple_navigation/item_container.rb, line 134
def active_leaf_container
  if selected_sub_navigation?
    selected_item.sub_navigation.active_leaf_container
  else
    self
  end
end
dom_attributes() click to toggle source
# File lib/simple_navigation/item_container.rb, line 22
def dom_attributes
  # backward compability for #dom_id and #dom_class
  dom_id_and_class = {
    id: dom_id,
    class: dom_class
  }.reject { |_, v| v.nil? }

  @dom_attributes.merge(dom_id_and_class)
end
empty?() click to toggle source

Returns true if there are no items defined for this container.

# File lib/simple_navigation/item_container.rb, line 143
def empty?
  items.empty?
end
item(key, name, url = nil, options = {}, &block) click to toggle source

Creates a new navigation item.

The key is a symbol which uniquely defines your navigation item in the scope of the primary_navigation or the sub_navigation.

The name will be displayed in the rendered navigation. This can also be a call to your I18n-framework.

The url is the address that the generated item points to. You can also use url_helpers (named routes, restful routes helper, url_for, etc). url is optional - items without URLs should not be rendered as links.

The options can be used to specify the following things:

  • any html_attributes - will be included in the rendered navigation item (e.g. id, class etc.)

  • :if - Specifies a proc to call to determine if the item should be rendered (e.g. if: Proc.new { current_user.admin? }). The proc should evaluate to a true or false value and is evaluated in the context of the view.

  • :unless - Specifies a proc to call to determine if the item should not be rendered (e.g. unless: Proc.new { current_user.admin? }). The proc should evaluate to a true or false value and is evaluated in the context of the view.

  • :method - Specifies the http-method for the generated link - default is :get.

  • :highlights_on - if autohighlighting is turned off and/or you want to explicitly specify when the item should be highlighted, you can set a regexp which is matched againstthe current URI.

The block - if specified - will hold the item's sub_navigation.

# File lib/simple_navigation/item_container.rb, line 64
def item(key, name, url = nil, options = {}, &block)
  return unless should_add_item?(options)
  item = Item.new(self, key, name, url, options, &block)
  add_item item, options
end
items=(new_items) click to toggle source
# File lib/simple_navigation/item_container.rb, line 70
def items=(new_items)
  new_items.each do |item|
    item_adapter = ItemAdapter.new(item)
    next unless should_add_item?(item_adapter.options)
    add_item item_adapter.to_simple_navigation_item(self), item_adapter.options
  end
end
level_for_item(navi_key) click to toggle source

Returns the level of the item specified by navi_key. Recursively works its way down the item's sub_navigations if the desired item is not found directly in this container's items. Returns nil if item cannot be found.

# File lib/simple_navigation/item_container.rb, line 89
def level_for_item(navi_key)
  return level if self[navi_key]

  items.each do |item|
    next unless item.sub_navigation
    level = item.sub_navigation.level_for_item(navi_key)
    return level if level
  end
  return nil
end
render(options = {}) click to toggle source

Renders the items in this ItemContainer using the configured renderer.

The options are the same as in the view's render_navigation call (they get passed on)

# File lib/simple_navigation/item_container.rb, line 104
def render(options = {})
  renderer_instance(options).render(self)
end
selected?() click to toggle source

Returns true if any of this container's items is selected.

# File lib/simple_navigation/item_container.rb, line 110
def selected?
  items.any?(&:selected?)
end
selected_item() click to toggle source

Returns the currently selected item, nil if no item is selected.

# File lib/simple_navigation/item_container.rb, line 116
def selected_item
  items.find(&:selected?)
end

Private Instance Methods

add_item(item, options) click to toggle source
# File lib/simple_navigation/item_container.rb, line 149
def add_item(item, options)
  items << item
  modify_dom_attributes(options)
end
evaluate_method(method) click to toggle source
# File lib/simple_navigation/item_container.rb, line 184
def evaluate_method(method)
  case method
  when Proc, Method then method.call
  else fail(ArgumentError, ':if or :unless must be procs or lambdas')
  end
end
modify_dom_attributes(options) click to toggle source
# File lib/simple_navigation/item_container.rb, line 154
def modify_dom_attributes(options)
  return unless container_options = options[:container]
  self.dom_attributes = container_options.fetch(:attributes) { dom_attributes }
  self.dom_class = container_options.fetch(:class) { dom_class }
  self.dom_id = container_options.fetch(:id) { dom_id }
  self.selected_class = container_options.fetch(:selected_class) { selected_class }
end
renderer_instance(options) click to toggle source

FIXME: raise an exception if :rederer is a symbol and it is not registred

in SimpleNavigation.registered_renderers
# File lib/simple_navigation/item_container.rb, line 164
def renderer_instance(options)
  return renderer.new(options) unless options[:renderer]

  if options[:renderer].is_a?(Symbol)
    registered_renderer = SimpleNavigation.registered_renderers[options[:renderer]]
    registered_renderer.new(options)
  else
    options[:renderer].new(options)
  end
end
selected_sub_navigation?() click to toggle source
# File lib/simple_navigation/item_container.rb, line 175
def selected_sub_navigation?
  !!(selected_item && selected_item.sub_navigation)
end
should_add_item?(options) click to toggle source
# File lib/simple_navigation/item_container.rb, line 179
def should_add_item?(options)
  [options[:if]].flatten.compact.all? { |m| evaluate_method(m) } &&
  [options[:unless]].flatten.compact.none? { |m| evaluate_method(m) }
end