Sha256: 9ef0103e60e0e180ff9b28e43df1a37c40dc1567a9348825aab9471bf80e8fac

Contents?: true

Size: 1.14 KB

Versions: 4

Compression:

Stored size: 1.14 KB

Contents

module Navi
  module Renderers
    class SimpleNavigation < Navi::Renderers::Base

      def render(collection, *args)
        options = Hash[*args]
        options[:expand_all] ||= true
        options[:items] ||= create_dynamic_items(collection)
        @template.render_navigation options
      end

      private

      # Create an array of hashes that can be easily fed into SimpleNavigation
      # like in the following like:
      # https://github.com/andi/simple-navigation/wiki/Dynamic-Navigation-Items
      def create_dynamic_items(collection)
        nav = []
        collection.each do |nav_item|
          item = {}
          item[:key] = @template.dom_id(nav_item).to_sym
          item[:name] = nav_item.label
          if nav_item.url.is_a?(String)
            item[:url] = nav_item.url
          else # it's a database record then!
            item[:url] = @template.polymorphic_path(nav_item.url)
          end
          item[:options] = {:title => nav_item.title, :class => nav_item.class.name.underscore}
          item[:items] = create_dynamic_items(nav_item.children)

          nav << item
        end
        nav
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
navi-0.1.4 lib/navi/renderers/simple_navigation.rb
navi-0.1.3 lib/navi/renderers/simple_navigation.rb
navi-0.0.17 lib/navi/renderers/simple_navigation.rb
navi-0.0.16 lib/navi/renderers/simple_navigation.rb