Skip to content

Instantly share code, notes, and snippets.

@spieker
Created March 16, 2012 16:51
Show Gist options
  • Save spieker/2051071 to your computer and use it in GitHub Desktop.
Save spieker/2051071 to your computer and use it in GitHub Desktop.
nav-composer
*.gem
*.rbc
.bundle
.config
.yardoc
Gemfile.lock
InstalledFiles
_yardoc
coverage
doc/
lib/bundler/man
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp

Nav::Composer

Nav composer is a quite simple gem, wich supports an easy interface to define simple, flat navigations.

Installation

Add this line to your application's Gemfile:

gem 'nav-composer', :git => 'git://gist.github.com/2051071.git'

And then execute:

$ bundle

Usage

First you have to define navigation groups which are available in your application. The navigation groups are uniq and you can use them to define navigation sections within your application.

NavComposer::Base.define_group('crm')

Then you can define different navigation items within your application.

NavComposer::Base.add('crm', 'orders', orders_path)

The navigation structure you can then get from the gem.

NavComposer::Base.add('crm', 'orders', root_path)

Creating a navigation structure could look like this

<% NavComposer::Base.all.each do |g| %>
  <li class="dropdown">
    <a href="#" class="dropdown-toggle" data-toggle="dropdown"><%= t g[:gid], :scope => [:nav_composer, :groups] %> <b class="caret"></b></a>
    <ul class="dropdown-menu">
      <% g[:nodes].each do |n| %>
        <li><%= link_to(t(n[:nid], :scope => [:nav_composer, :nodes]), n[:url]) %></li>
      <% end %>
    </ul>
  </li>
<% end %>
module NavComposer
class Base
class <<self
def define_group(gid)
@gids ||= []
@gids << gid
@gids = @gids.uniq
@groups ||= {}
@groups[gid.to_sym] = []
end#def
# Add new menu item
#
# +gid+ Group ID
# +nid+ Node ID
#
def add(gid, nid, url)
@nids ||= {}
@nids[gid.to_sym] ||= {}
@groups[gid.to_sym] ||= []
@groups[gid.to_sym] << nid.to_s
@nids[gid.to_sym][nid.to_sym] = url
end#def
def all
@nids ||= {}
@gids ||= []
result = []
@gids.each do |g|
@nids[g.to_sym] ||= {}
result << {
:gid => g,
:nodes => @groups[g.to_sym].map { |n| {
:nid => n,
:url => @nids[g.to_sym][n.to_sym]
} }
}
end#do
result
end#def
end#class
end#class
end#module
source 'https://rubygems.org'
# Specify your gem's dependencies in nav-composer.gemspec
gemspec
Copyright (c) 2012 Paul Spieker
MIT License
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# -*- encoding: utf-8 -*-
require File.expand_path('../lib/nav-composer/version', __FILE__)
Gem::Specification.new do |gem|
gem.authors = ["Paul Spieker"]
gem.email = ["p.spieker@duenos.de"]
gem.description = %q{TODO: Write a gem description}
gem.summary = %q{TODO: Write a gem summary}
gem.homepage = ""
gem.files = `git ls-files`.split($\)
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.name = "nav-composer"
gem.require_paths = ["lib"]
gem.version = NavComposer::VERSION
end
require "nav-composer/version"
require "nav-composer/base"
module NavComposer
end
#!/usr/bin/env rake
require "bundler/gem_tasks"
module NavComposer
VERSION = "0.0.1"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment