class RocketNavigation::ItemContainer

Attributes

container_html[RW]
item_html[RW]
items[R]
level[R]
options[RW]
renderer[RW]
selected_class[RW]
view_context[RW]

Public Class Methods

new(level = 1, options = {}) click to toggle source
# File lib/rocket_navigation/item_container.rb, line 22
def initialize(level = 1, options = {})
  @level = level
  @items ||= []
  @options = options
  @renderer = RocketNavigation.config.renderer
  default_html_options
end

Public Instance Methods

[](navi_key) click to toggle source

Returns the Item with the specified key, nil otherwise.

# File lib/rocket_navigation/item_container.rb, line 53
def [](navi_key)
  items.find { |item| item.key == navi_key }
end
default_html_options() click to toggle source
# File lib/rocket_navigation/item_container.rb, line 8
def default_html_options
  if options[:no_default_classes]
    @container_html = {}
    @item_html = {}
    @link_html = {}
    @selected_class = {}
  else
    @container_html = {class: "nav"}
    @item_html = {class: 'nav-item'}
    @link_html = {class: 'nav-link'}
    @selected_class = {branch: "active-branch", item: "active", link: "active"}
  end
end
empty?() click to toggle source

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

# File lib/rocket_navigation/item_container.rb, line 94
def empty?
  items.empty?
end
inspect() click to toggle source
# File lib/rocket_navigation/item_container.rb, line 98
    def inspect
"#<RocketNavigation::ItemContainer:#{object_id}
  @renderer=#{@renderer.inspect}
  @options=#{@options.inspect}
  @items=#{@items.inspect}
  @level=#{@level.inspect}
  @container_html=#{@container_html.inspect}
  @item_html=#{@item_html.inspect}
  @link_html=#{@link_html.inspect}
  @selected_class=#{@selected_class.inspect}
  @view_context=#{view_context.nil? ? nil : "[rails view context, hidden from inspect]"}
>"
    end
item(key, name, url = nil, options = {}, &block) click to toggle source
# File lib/rocket_navigation/item_container.rb, line 36
def item(key, name, url = nil, options = {}, &block)
  return unless should_add_item?(options)
  key = url if key.nil?
  item = Item.new(self, key, name, url, options, &block)
  add_item item, options
end
items=(new_items) click to toggle source
# File lib/rocket_navigation/item_container.rb, line 43
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/rocket_navigation/item_container.rb, line 62
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
new_child() click to toggle source
# File lib/rocket_navigation/item_container.rb, line 30
def new_child
  child = ItemContainer.new(level + 1, options)
  child.view_context = view_context
  child
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/rocket_navigation/item_container.rb, line 77
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/rocket_navigation/item_container.rb, line 83
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/rocket_navigation/item_container.rb, line 89
def selected_item
  items.find(&:selected?)
end

Private Instance Methods

add_item(item, options) click to toggle source
# File lib/rocket_navigation/item_container.rb, line 114
def add_item(item, options)
  items << item
end
renderer_instance(options) click to toggle source
# File lib/rocket_navigation/item_container.rb, line 123
def renderer_instance(options)
  return renderer.new(self, options) unless options[:renderer]
  if options[:renderer].is_a?(Symbol)
    registered_renderer = RocketNavigation.registered_renderers[options[:renderer]]
    registered_renderer.new(self, options)
  else
    options[:renderer].new(self, options)
  end
end
should_add_item?(options) click to toggle source
# File lib/rocket_navigation/item_container.rb, line 118
def should_add_item?(options)
  [options[:if]].flatten.compact.all? { |m| evaluate_method(m) } &&
  [options[:unless]].flatten.compact.none? { |m| evaluate_method(m) }
end