Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 6fed6ee08b1a0fa46d93681c0431418900db621d Daniel Haran committed Oct 24, 2008
Showing with 190 additions and 0 deletions.
  1. +20 −0 MIT-LICENSE
  2. +30 −0 README
  3. +22 −0 Rakefile
  4. +2 −0 init.rb
  5. +1 −0 install.rb
  6. +21 −0 lib/menu_helper.rb
  7. +49 −0 lib/semantic_menu.rb
  8. +4 −0 tasks/semantic_menu_tasks.rake
  9. +40 −0 test/semantic_menu_test.rb
  10. +1 −0 uninstall.rb
@@ -0,0 +1,20 @@
+Copyright (c) 2008 [name of plugin creator]
+
+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.
30 README
@@ -0,0 +1,30 @@
+SemanticMenu
+============
+
+A plugin to make menus easier to write. Does not support nesting yet
+
+TODO:
+ -fix rdoc
+ -add nesting
+ -add css + task for copying it over
+
+Example
+=======
+
+<%= tabbed_menu do |root|
+ root.add "overview", root_path
+ root.add "comments", comments_path
+end %>
+
+Assuming you are on /comments, the output would be:
+
+<ul class="menu">
+ <li>
+ <a href="/">overview</a>
+ </li>
+ <li class="active">
+ <a href="/comments">comments</a>
+ </li>
+</ul>
+
+Copyright (c) 2008 Daniel Haran, released under the MIT license
@@ -0,0 +1,22 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the semantic_menu plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the semantic_menu plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'SemanticMenu'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
@@ -0,0 +1,2 @@
+# Include hook code here
+ActionView::Base.send :include, MenuHelper
@@ -0,0 +1 @@
+# Install hook code here
@@ -0,0 +1,21 @@
+# Use as so:
+# <%= tabbed_menu do |root|
+# root.add "overview", root_path
+# root.add "comments", comments_path
+# end %>
+#
+# Assuming you are on /comments, the output would be:
+#
+# <ul class="menu">
+# <li>
+# <a href="/">overview</a>
+# </li>
+# <li class="active">
+# <a href="/comments">comments</a>
+# </li>
+# </ul>
+module MenuHelper
+ def tabbed_menu(opts={}, &block)
+ SemanticMenu.new(@controller.request.request_uri, opts, &block).to_s
+ end
+end
@@ -0,0 +1,49 @@
+require 'rubygems'
+require 'action_view'
+require 'active_support'
+
+class MenuItem
+ include ActionView::Helpers::TagHelper,
+ ActionView::Helpers::UrlHelper
+
+ cattr_accessor :current_page
+
+ def initialize(title, link)
+ @title, @link = title, link
+ end
+
+ def to_s
+ opts = active? ? {:class => 'active'} : {}
+ content_tag :li, link_to(@title, @link), opts
+ end
+
+ def active?
+ on_current_path?
+ end
+
+ # NB: not the same as current_page? which assumes options (rather than named route / string)
+ # also, this takes a path only, not a full url. In a menu, links are expected to be relative.
+ def on_current_path?
+ @link == CGI.escapeHTML(@@current_page)
+ end
+end
+
+class SemanticMenu < MenuItem
+
+ attr_accessor :children
+ def initialize(current_page, opts={},&block)
+ @@current_page = current_page
+
+ @opts = {:class => 'menu'}.merge opts
+ @children = []
+ yield self
+ end
+
+ def to_s
+ content_tag(:ul, @children.collect(&:to_s).join, @opts)
+ end
+
+ def add(title, link)
+ @children << MenuItem.new(title, link)
+ end
+end
@@ -0,0 +1,4 @@
+# desc "Explaining what the task does"
+# task :semantic_menu do
+# # Task goes here
+# end
@@ -0,0 +1,40 @@
+require 'test/unit'
+require 'rubygems'
+require 'active_support'
+require 'active_support/test_case'
+require 'action_controller' # grr, action_view now breaks without it
+require 'semantic_menu'
+
+class SemanticMenuTest < ActiveSupport::TestCase
+ def test_menu_to_s
+ assert_equal SemanticMenu.new(nil) {}.to_s, '<ul class="menu"></ul>'
+ end
+
+ def test_menu_item_to_s
+ assert_equal MenuItem.new('title', 'link').to_s, '<li><a href="link">title</a></li>'
+ end
+
+ def test_menu_item_with_one_child
+ assert_equal '<ul class="mymenu"><li><a href="link">title</a></li></ul>', default_menu.to_s
+ end
+
+ def test_menu_item_with_two_children
+ menu = default_menu
+ menu.add 'title2', 'link2'
+ assert_equal '<ul class="mymenu"><li><a href="link">title</a></li>' +
+ '<li><a href="link2">title2</a></li></ul>', menu.to_s
+ end
+
+ def test_menu_item_shows_active_if_on_current_page
+ item = MenuItem.new("title", "link")
+ item.stubs(:active?).returns(true)
+ assert_equal '<li class="active"><a href="link">title</a></li>', item.to_s
+ end
+
+ protected
+ def default_menu
+ SemanticMenu.new nil, :class => 'mymenu' do |root|
+ root.add 'title', 'link'
+ end
+ end
+end
@@ -0,0 +1 @@
+# Uninstall hook code here

0 comments on commit 6fed6ee

Please sign in to comment.