Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

initial import

  • Loading branch information...
commit ae43b387849a1d8a180d6f1ac89c8233271677b8 0 parents
Ivan Torres authored
2  .gitignore
@@ -0,0 +1,2 @@
+pkg
+doc
5 Manifest
@@ -0,0 +1,5 @@
+Manifest
+README.rdoc
+Rakefile
+init.rb
+lib/simple_navigation.rb
125 README.rdoc
@@ -0,0 +1,125 @@
+= Simple Navigation
+
+Ruby on Rails gem/plugin for drop down/tabbed navigation.
+
+== Requirements
+
+* Rails 2.3.x (may work with older versions, not tested)
+
+== Install
+
+Add the following line to your /config/environment.rb file:
+
+ config.gem "mexpolk-simple_navigation",
+ :lib => "simple_navigation",
+ :source => "http://gems.github.com"
+
+And from the command line run:
+
+ rake gems:install
+
+===Install as a Plugin
+
+If your rather prefer to install it as a plugin, from your application
+directory simply run:
+
+ script/plugin install git://github.com/mexpolk/simple_navigation.git
+
+And you're done!
+
+== Usage
+
+To create your menus create config/initializers/simple_navigation.rb file like
+this:
+
+ SimpleNavigation::Builder.config do |map|
+ map.navigation :default do |navigation|
+
+ # Root menu without child elements (menus) that points to /dashboard
+ navigation.menu :home, :url => { :controller => "home", :action => "index"}
+
+ # Root menu with child menus without anchor link
+ navigation.menu :contacts do |contacts|
+
+ # Child menu with many possible urls (or many controllers and actions)
+ contacts.menu :list, :url => { :controller => "contacts", :action => "index" } do |contact_list|
+
+ # This menu will marked as current when you're on the following
+ # controllers and actions (including the controller and action
+ # specified in the :url option):
+ contact_list.connect :controller => "contacts" # ...current on any action from this controller
+ contact_list.connect :controller => "people", :except => "new"
+ contact_list.connect :controller => "companies", :except => "new"
+
+ end
+
+ # Another submenu that points to /person/new
+ contacts.menu :new_person, :url => { :controller => "people", :action => "new" }
+
+ # Another submenu that points to /company/new
+ contacts.menu :new_company, :url => { :controller => "companies", :action => "new" }
+
+ end
+
+ # Another root menu with nested submenus
+ navigation.tab :admin, :url => { :controller => "users", :action => "index" } do |admin|
+ admin.menu :users, :url => { :controller => "users", :action => "index" } do |users|
+ users.menu :reports, :url => { :controller => "user_reports", :action => "index" } do |reports|
+ reports.menu :activity, :url => { :controller => "user_reports", :action => "activity" }
+ reports.menu :login_atempts, :url => { :controller => "user_reports", :action => "login_atempts" }
+ end
+ users.menu :new_user, :url => { :controller => "users", :action => "new" }
+ end
+ end
+
+ end
+ end
+
+To render you newly created menu called :default, in your default layout
+(layout/application.erb):
+
+ <%= simple_navigation :default %>
+
+==Internationalization (i18n)
+
+If you want to use internationalization, set the option :i18n => true like this:
+
+ SimpleNavigation::Builder.config do |map|
+ map.navigation :default, :i18n => true do |navigation|
+ ...
+ end
+ end
+
+And add to your config/locales files (e.g. es-MX.yml) the following:
+
+ es-MX:
+ simple_navigation:
+ default: # The name of your navigation menu
+ home: # The name of your root menu
+ title: "Inicio" # The translated title for your root menu
+ menus:
+ index: "Panel de Control" # The title for index action child menu
+ new: "Nueva Página" # The title for new action child menu
+
+== License
+
+Copyright (c) 2008 Iván "Mexpolk" Torres
+
+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.
12 Rakefile
@@ -0,0 +1,12 @@
+require 'rubygems'
+require 'rake'
+require 'echoe'
+
+Echoe.new('simple_navigation', '1.0') do |e|
+ e.description = "Easy navigation menu gem for Ruby on Rails"
+ e.url = "http://github.com/mexpolk/simple_navigation"
+ e.author = "Ivan Torres"
+ e.email = "mexpolk@gmail.com"
+ e.ignore_pattern = ["tmp/*", "script/*"]
+ e.development_dependencies = []
+end
1  init.rb
@@ -0,0 +1 @@
+require "simple_navigation"
206 lib/simple_navigation.rb
@@ -0,0 +1,206 @@
+module SimpleNavigation
+
+ module Helper
+
+ attr_accessor :current_menu_id
+
+ def simple_navigation(name)
+ # Load navigation hash
+ navigation = SimpleNavigation::Builder.navigation[name.to_sym]
+ # Reset current menu
+ self.current_menu_id = nil
+ # Render root menus
+ content_tag(:ul,
+ navigation[:menus].map{ |menu| render_menu(menu) },
+ :id => navigation[:id],
+ :class => 'simple_navigation')
+ end # simple_navigation(name)
+
+ def render_menu(menu)
+ class_name = 'menu'
+ # Render submenus first so we can detect if current menu
+ # is between child menu's
+ menus = ''
+ menus = content_tag(:ul,
+ menu[:menus].map{ |child| render_menu(child) },
+ :id => [menu[:id], 'menus'].join('_')) if menu.has_key?(:menus)
+ # Is this menu is the current?
+ if current_menu?(menu)
+ class_name << ' current'
+ self.current_menu_id = menu[:id]
+ # Is the current menu under this menu?
+ elsif self.current_menu_id
+ class_name << ' current_child' if self.current_menu_id.to_s.match(/^#{menu[:id]}/)
+ end
+ # Render menu
+ content_tag(:li,
+ render_menu_title(menu) + menus,
+ :id => menu[:id],
+ :class => class_name)
+ end # render_menu(menu)
+
+ def render_menu_title(menu)
+ title = ''
+ if menu[:options][:i18n]
+ if menu.has_key?(:title)
+ title = t(menu[:translation], :default => menu[:title])
+ else
+ title = t(menu[:translation], :default => menu[:name].to_s.titleize)
+ end
+ else
+ if menu.has_key?(:title)
+ title = menu[:title]
+ else
+ title = menu[:name].to_s.titleize
+ end
+ end
+ title = link_to(title, url_for(menu[:url])) if menu.has_key?(:url)
+ title
+ end # render_menu_title(menu)
+
+ protected
+
+ def current_menu?(menu)
+ current = (controller.params[:controller] == menu[:url][:controller].gsub(/^\//, "")) &&
+ (controller.params[:action] == menu[:url][:action])
+ if menu.has_key?(:urls)
+ (menu[:urls].is_a?(Array) ? menu[:urls] : [menu[:urls]]).each do |controllers|
+ (controllers.is_a?(Array) ? controllers : [controllers]).each do |c|
+ current |= controller.params[:controller] == c[:controller].gsub(/^\//, "")
+ if c.has_key?(:only)
+ current &= (c[:only].is_a?(Array) ? c[:only] : [c[:only]]).include?(controller.params[:action])
+ end
+ if c.has_key?(:except)
+ current &= !((c[:except].is_a?(Array) ? c[:except] : [c[:except]]).include?(controller.params[:action]))
+ end
+ end
+ end
+ end
+ current
+ end # current_menu?
+ end # Helper
+
+ class Configuration
+
+ attr_accessor :navigation
+
+ def initialize
+ self.navigation = {}
+ end
+
+ def config(&block)
+ builder = Builder.new
+ yield builder
+ builder.navigations.each { |tmp| self.navigation[tmp[:name]] = tmp }
+ end
+
+ class Builder
+
+ attr_accessor :navigations, :prefix
+
+ def initialize
+ self.navigations = []
+ end
+
+ # Create a new navigation
+ def navigation(name, options = {}, &block)
+ options.merge!(:i18n => false) unless options.has_key?(:i18n)
+ navigation = Navigation.new(name, options)
+ yield navigation
+ self.navigations << navigation.build
+ end
+
+ # Render new navigation
+ def build
+ { :navigations => navigations }
+ end
+
+ class Navigation
+
+ attr_accessor :id, :menus, :name, :options, :translation
+
+ def initialize(name, options = {})
+ options.merge!(:i18n => false) unless options.has_key?(:i18n)
+ self.translation = name
+ self.id = ['simple_navigation', name].join('_')
+ self.menus = []
+ self.name = name
+ self.options = options
+ end
+
+ # Create a new root menu
+ def menu(name, *args, &block)
+ title = args.first.is_a?(String) ? args.first : nil
+ options = args.last.is_a?(::Hash) ? args.last : {}
+ options.merge!(:i18n => self.options[:i18n])
+ options.merge!(:translation => [self.translation, 'menus'].join('.'))
+ options.merge!(:prefix => [self.id, 'menus'].join('_'))
+ menu = Menu.new(name, title, options)
+ yield menu if block
+ self.menus << menu.build
+ end
+
+ # render menu
+ def build
+ { :id => self.id.to_sym,
+ :name => self.name.to_sym,
+ :menus => self.menus,
+ :options => self.options }
+ end
+
+ class Menu
+
+ attr_accessor :id, :menus, :name, :options, :title, :translation, :url, :urls
+
+ def initialize(name, title = nil, options = {})
+ self.id = [options[:prefix], name].join('_')
+ self.menus = []
+ self.name = name
+ self.title = title
+ self.translation = [options[:translation], name].join('.')
+ self.url = options[:url]
+ self.urls = []
+ options.delete(:translation)
+ options.delete(:url)
+ self.options = options
+ end
+
+ # Create a new child menu
+ def menu(name, *args, &block)
+ title = args.first.is_a?(String) ? args.first : nil
+ options = args.last.is_a?(::Hash) ? args.last : {}
+ options.merge!(:i18n => self.options[:i18n])
+ options.merge!(:translation => [self.translation, 'menus'].join('.'))
+ options.merge!(:prefix => [self.id, 'menus'].join('_'))
+ menu = Menu.new(name, title, options)
+ yield menu if block
+ self.menus << menu.build
+ end
+
+ def connect(options = {})
+ options[:controller] = self.url[:controller] unless options.has_key?(:controller)
+ self.urls << options
+ end
+
+ def build
+ menu = { :id => self.id.to_sym, :name => self.name.to_sym,
+ :options => self.options }
+ # Add keys with values only:
+ menu.merge!(:menus => self.menus) unless self.menus.empty?
+ menu.merge!(:title => self.title) unless self.title.nil?
+ menu.merge!(:translation => [self.translation, 'title'].join('.')) if self.options[:i18n] == true
+ menu.merge!(:url => self.url) unless self.url.nil?
+ menu.merge!(:urls => self.urls) unless self.urls.empty?
+ # Return menu hash
+ menu
+ end
+ end # Menu
+ end # Navigation
+ end # Builder
+ end # Configuration
+
+ Builder = Configuration.new
+
+end # SimpleNavigation
+
+ActionView::Base.send :include, SimpleNavigation::Helper
30 simple_navigation.gemspec
@@ -0,0 +1,30 @@
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{simple_navigation}
+ s.version = "1.0"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
+ s.authors = ["Ivan Torres"]
+ s.date = %q{2009-08-06}
+ s.description = %q{Easy navigation menu gem for Ruby on Rails}
+ s.email = %q{mexpolk@gmail.com}
+ s.extra_rdoc_files = ["README.rdoc", "lib/simple_navigation.rb"]
+ s.files = ["Manifest", "README.rdoc", "Rakefile", "init.rb", "lib/simple_navigation.rb", "simple_navigation.gemspec"]
+ s.homepage = %q{http://github.com/mexpolk/simple_navigation}
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Simple_navigation", "--main", "README.rdoc"]
+ s.require_paths = ["lib"]
+ s.rubyforge_project = %q{simple_navigation}
+ s.rubygems_version = %q{1.3.5}
+ s.summary = %q{Easy navigation menu gem for Ruby on Rails}
+
+ if s.respond_to? :specification_version then
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
+ s.specification_version = 3
+
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
+ else
+ end
+ else
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.