Permalink
Browse files

first commit of tab_menu

  • Loading branch information...
0 parents commit a3b8325ba52462de30446b20113f187df7f68ca9 Dan Pickett committed Oct 15, 2008
@@ -0,0 +1,50 @@
+= TabMenu
+
+TabMenu is an easy way to create tabs for your Rails Application.
+
+== Examples
+=======
+
+<%- tab_menu "user_menu" do |t| -%>
+ <%= t.tab "Profile", "/account"
+ <%= t.tab "Change Settings"," /account/edit" %>
+<%- end -%>
+
+This will render:
+
+<ul class="user_menu">
+ <li class="active"><a href="/account">Profile</a></li>
+ <li><a href="/account/edit">Change Settings</li>
+</ul>
+
+note the active class is designated for the current url
+
+== TODO
+* Eliminate concat calls once defects are fixed in Rails 2.2
+* Abstract TabBuilder to support AJAX loading of tabs (different types of builders)
+*
+
+== LICENSED UNDER MIT
+
+====================================================================
+==== Dan Pickett
+Copyright (c) 2008 Enlight Solutions, Inc.
+====================================================================
+
+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.
@@ -0,0 +1,11 @@
+require 'rake'
+require 'spec/rake/spectask'
+
+desc 'Default: run specs.'
+task :default => :spec
+
+desc 'Run the specs'
+Spec::Rake::SpecTask.new(:spec) do |t|
+ t.spec_opts = ['--colour --format progress --loadby mtime --reverse']
+ t.spec_files = FileList['spec/**/*_spec.rb']
+end
@@ -0,0 +1,2 @@
+# Include hook code here
+require "tab_menu"
@@ -0,0 +1 @@
+# Install hook code here
@@ -0,0 +1,13 @@
+require "tab_menu/tab_builder"
+require "tab_menu/view_helpers"
+
+module TabMenu
+ def self.enable
+ ActionView::Base.send :include, TabMenu::ViewHelpers
+ end
+end
+
+#thanks will_paginate for this snippet
+if defined?(Rails) and defined?(ActionController)
+ TabMenu.enable
+end
@@ -0,0 +1,18 @@
+module TabMenu
+ class TabBuilder
+ include ActionView::Helpers::TagHelper
+ include ActionView::Helpers::CaptureHelper
+
+ def initialize(controller)
+ @controller = controller
+ end
+
+ def tab(name, url, html_options = {})
+ #append current to the contentli class attribute if its the current page
+ html_options[:class] = [html_options[:class], "current"].join if @controller.current_page?(url)
+ content_tag(:li, name, html_options) do
+ @controller.link_to name, url
+ end
+ end
+ end
+end
@@ -0,0 +1,27 @@
+require File.dirname(__FILE__) + '/tab_builder'
+
+module TabMenu
+ module ViewHelpers
+ include ActionView::Helpers::TagHelper
+ include ActionView::Helpers::UrlHelper
+
+ def tab_menu(tab_name = "", html_options = {}, &block)
+ html_options[:class] ||= tab_name.blank? ? nil : tab_name.gsub(" ", "_").underscore
+
+ # unsure of performance here but nested content_tags were causing issues refer to -
+ # refer to - http://rails.lighthouseapp.com/projects/8994/tickets/386-nested-content_tag-calls-causes-the-layout-to-render-multiple-times
+ # will fix in 2.2 after double render gets fixed
+ if eval("defined? _erbout", block.binding)
+ concat("<ul>", block.binding)
+ yield(TabBuilder.new(self))
+ concat("</ul>", block.binding)
+ else
+ content_tag(:ul, html_options) do
+ yield(TabBuilder.new(self))
+ end
+ end
+
+ end
+
+ end
+end
@@ -0,0 +1 @@
+# Logfile created on Wed Oct 15 11:53:48 -0400 2008 by /
@@ -0,0 +1,10 @@
+begin
+ require File.dirname(__FILE__) + '/../../../../spec/spec_helper'
+rescue LoadError
+ puts "You need to install rspec in your base app"
+ exit
+end
+
+plugin_spec_dir = File.dirname(__FILE__)
+ActiveRecord::Base.logger = Logger.new(plugin_spec_dir + "/debug.log")
+
@@ -0,0 +1,48 @@
+require File.dirname(__FILE__) + '/../spec_helper'
+require 'action_controller'
+require 'action_controller/test_process'
+require 'net/http'
+require 'net/https'
+
+class TabMenuController < ActionController::Base
+ include TabMenu
+
+ def index
+ render :text => "index"
+ end
+
+ def show
+ render :text => "show"
+ end
+end
+
+describe TabMenu::TabBuilder, :type => :helper do
+ before(:each) do
+ @controller = TabMenuController.new
+
+ @builder = TabMenu::TabBuilder.new(helper)
+ end
+
+ it "should create an li" do
+ @builder.tab("Test", "/").should =~ /^<li/
+ @builder.tab("Test", "/").should =~ /<\/li>$/
+ end
+
+ it "should create an li with the first argument as the text" do
+ @builder.tab("Test", "/").should =~ />Test</
+ end
+
+ it "should create an li with a link inside it" do
+ @builder.tab("Test", "/").should =~ /<a/
+ @builder.tab("Test", "/").should =~ /<\/a>/
+ end
+
+ it "should create an a with an href to the specified url" do
+ @builder.tab("Test", "/").should =~ /href=\"\/\"/
+ end
+
+ it "should set a tab as current if it is the current url" do
+ helper.should_receive(:current_page?).with("/").once.and_return true
+ @builder.tab("Test", "/").should =~ /class=\"current\"/
+ end
+end
@@ -0,0 +1,27 @@
+require File.dirname(__FILE__) + '/../spec_helper'
+
+require File.dirname(__FILE__) + "/../../lib/tab_menu"
+
+describe TabMenu::ViewHelpers, :type => :helper do
+ include ActionView::Helpers::UrlHelper
+ include ActionView::Helpers::TextHelper
+ include ActionView::Helpers::TagHelper
+ include TabMenu::ViewHelpers
+
+ it "should create an unordered list" do
+ menu = helper.tab_menu do |t|
+ t.tab "Test", "/"
+ end
+
+ menu.should =~ /^<ul>/
+ menu.should =~ /<\/ul>$/
+ end
+
+ it "should optionally take a name which will create a subsequent class for the ul" do
+ menu = tab_menu("test") do |t|
+ t.tab "Test", "/"
+ end
+
+ menu.should =~ /<ul class=\"test\">/
+ end
+end
@@ -0,0 +1,4 @@
+# desc "Explaining what the task does"
+# task :tab_menu do
+# # Task goes here
+# end
@@ -0,0 +1 @@
+# Uninstall hook code here

0 comments on commit a3b8325

Please sign in to comment.