module RocketNavigation::Helpers

View helpers to render the navigation.

Use render_navigation as following to render your navigation:

For example, you could use render_navigation(level: 1) to render your primary navigation as tabs and render_navigation(level: 2..3) to render the rest of the navigation as a tree in a sidebar.

Examples (using Haml)

#primary_navigation= render_navigation(level: 1)

#sub_navigation= render_navigation(level: 2)

#nested_navigation= render_navigation

#top_navigation= render_navigation(level: 1..2)
#sidebar_navigation= render_navigation(level: 3)

Public Instance Methods

render_navigation(options = {}) { |container| ... } click to toggle source

Renders the navigation according to the specified options-hash.

The following options are supported:

  • :level - defaults to :all which renders the the sub_navigation for an active primary_navigation inside that active primary_navigation item. Specify a specific level to only render that level of navigation (e.g. level: 1 for primary_navigation, etc). Specifiy a Range of levels to render only those specific levels (e.g. level: 1..2 to render both your first and second levels, maybe you want to render your third level somewhere else on the page)

  • :expand_all - defaults to false. If set to true the all specified levels will be rendered as a fully expanded tree (always open). This is useful for javascript menus like Superfish.

  • :items - you can specify the items directly (e.g. if items are dynamically generated from database).

  • :renderer - specify the renderer to be used for rendering the navigation. Either provide the Class or a symbol matching a registered renderer. Defaults to :list (html list renderer).

Instead of using the :items option, a block can be passed to specify the items dynamically

Examples

render_navigation do |menu|
  menu.item :posts, "Posts", posts_path
end
# File lib/rocket_navigation/helpers.rb, line 55
def render_navigation(options = {}, &block)
  options[:level] = options.delete(:levels) if options[:levels]

  first_level = 1
  unless options[:level].nil?
    case options[:level]
    when :all
      fist_level = 1
    when Integer
      first_level = options[:level]
      options[:level] = [options[:level]]
    when Array, Range
      options[:level] = options[:level].to_a
      first_level = options[:level].first
    end
  end

  container = ItemContainer.new(1, options)
  container.view_context = view_context
  if block_given?
    yield container
  end

  if first_level == 1
    container.render(options)
  else
    # need to render not from first level
    # drop down active tree to find a subtree to render
    level = 1
    while level < 50 #guard
      selected_item = container.selected_item
      if selected_item.nil?
        return "".html_safe
      end
      if selected_item.level == first_level - 1
        if selected_item.sub_navigation.nil?
          return "".html_safe
        end
        return selected_item.sub_navigation.render(options)
      end

      level += 1
      container = selected_item.sub_navigation
    end
  end
end