Permalink
Browse files

using anchors instead of span for menus without url

  • Loading branch information...
1 parent f942681 commit da064783a09402ec6d58047f752d91a925579f67 @mexpolk mexpolk committed Sep 23, 2010
Showing with 54 additions and 51 deletions.
  1. +1 −1 README.rdoc
  2. +1 −1 Rakefile
  3. +51 −48 lib/simple_navigation.rb
  4. +1 −1 simple_navigation.gemspec
View
@@ -76,7 +76,7 @@ this:
end
To render you newly created menu called :default, in your default layout
-(layout/application.erb):
+(layout/application.html.erb):
<%= simple_navigation :default %>
View
@@ -2,7 +2,7 @@ require 'rubygems'
require 'rake'
require 'echoe'
-Echoe.new('simple_navigation', '1.4') do |e|
+Echoe.new('simple_navigation', '1.4.1') do |e|
e.description = "Simple navigation menu gem for Ruby on Rails"
e.url = "http://github.com/mexpolk/simple_navigation"
e.author = "Ivan Torres"
View
@@ -1,9 +1,11 @@
module SimpleNavigation
+ # Simple Navigation helper methods module
module Helper
attr_accessor :current_menu_id
+ # Renders simple navigation menu by key name
def simple_navigation(name)
# Load navigation hash
@@ -20,60 +22,57 @@ def simple_navigation(name)
end # simple_navigation(name)
- def render_menu(menu, options = {})
-
- # Set default html attributes
- html_attrs = { :id => menu[:id] }
- html_attrs[:class] = menu[:class] if menu.has_key?(:class)
-
- # 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, options) }) if menu.has_key?(:menus)
-
- # Is this menu is the current?
- if current_menu?(menu)
- html_attrs[:class] << ' current'
- self.current_menu_id = menu[:id]
- # Is the current menu under this menu?
- elsif self.current_menu_id
- html_attrs[:class] << ' current' if
- self.current_menu_id.to_s.match(/^#{menu[:id]}/)
- end
+ protected
+
+ # Render menu element
+ def render_menu(menu, options = {})
+
+ # Set default html attributes
+ html_attrs = { :id => menu[:id] }
+ html_attrs[:class] = menu[:class] if menu.has_key?(:class)
+
+ # 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, options) }) if menu.has_key?(:menus)
+
+ # Is this menu is the current?
+ if current_menu?(menu)
+ html_attrs[:class] << ' current'
+ self.current_menu_id = menu[:id]
+ # If any of the children menus is the current
+ # mark parent menu as current too
+ elsif self.current_menu_id
+ html_attrs[:class] << ' current' if
+ self.current_menu_id.to_s.match(/^#{menu[:id]}/)
+ end
- # Render menu
- content_tag(:li,
- render_menu_title(menu) + menus,
- html_attrs)
+ # Render menu
+ content_tag(:li, render_menu_title(menu) + menus, html_attrs)
- end # render_menu(menu)
+ 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]
+ # Renders the menu title
+ #
+ # * If menu hash has the title key it is used as the title for the menu being rendered
+ # * If menu hash +:title+ key is not present then it formats +:name+ key as titleized string
+ # * If menu hash +:title+ key is not present and i18n is turned on, then it will use +:name+ key as translation key
+ def render_menu_title(menu)
+ title = if menu.has_key?(:title)
+ menu[:title]
else
- title = menu[:name].to_s.titleize
+ if menu[:options][:i18n]
+ t(menu[:translation], :default => menu[:name].to_s.titleize)
+ else
+ menu[:name].to_s.titleize
+ end
end
- end
- if menu.has_key?(:url)
- title = link_to(title, url_for(menu[:url]))
- else
- title = content_tag(:span, title)
- end
- title
- end # render_menu_title(menu)
-
- protected
+ link_to(title, (menu.has_key?(:url) ? url_for(menu[:url]) : "#" ))
+ end # render_menu_title(menu)
+ # Detects if the menu being rendered is the current
+ # (it also marks parent menus as current).
def current_menu?(menu)
return false unless menu.has_key?(:url)
current = (controller.params[:controller] == menu[:url][:controller].gsub(/^\//, "")) &&
@@ -96,6 +95,7 @@ def current_menu?(menu)
end # Helper
+ # Simple Navigation configuration class
class Configuration
attr_accessor :navigation
@@ -110,6 +110,7 @@ def config(&block)
builder.navigations.each { |tmp| self.navigation[tmp[:name]] = tmp }
end
+ # Simple Navigation configuration builder
class Builder
attr_accessor :navigations, :prefix
@@ -131,6 +132,7 @@ def build
{ :navigations => navigations }
end
+ # Hash structure containing simple navigation menu configuration
class Navigation
attr_accessor :id, :menus, :name, :options, :translation
@@ -164,6 +166,7 @@ def build
:options => self.options }
end
+ # Menu builder
class Menu
attr_accessor :id, :menus, :name, :options, :title, :translation, :url, :urls
@@ -2,7 +2,7 @@
Gem::Specification.new do |s|
s.name = %q{simple_navigation}
- s.version = "1.4"
+ s.version = "1.4.1"
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
s.authors = ["Ivan Torres"]

0 comments on commit da06478

Please sign in to comment.