Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 639f637459bfcf544d39b1d096283af335754bf3 Chris Rankin committed Jan 22, 2009
Showing with 182 additions and 0 deletions.
  1. +87 −0 README
  2. +22 −0 Rakefile
  3. +5 −0 init.rb
  4. +1 −0 install.rb
  5. +9 −0 lib/wizard/application.rb
  6. +25 −0 lib/wizard/application_helper.rb
  7. +20 −0 mit
  8. +4 −0 tasks/wizard_tasks.rake
  9. +8 −0 test/wizard_test.rb
  10. +1 −0 uninstall.rb
@@ -0,0 +1,87 @@
+Wizard
+======
+
+This plugin allows you to create wizards on the fly without manipulating your DB
+or worrying about transitions, states, etc. You can declare the previous and nexts steps
+for each action by simply naming the action of the corresponding step. It also allows you
+to use any model along with any wizard as many times as you want. All you have to do is
+create a template for each wizard action. Check at the example section to better understand how
+it works.
+
+I didn't comment any of my code. I'll try to get around to it, but I'm busy as hell. The code is very
+basic and anyone that understands ruby/rails please feel free to comment it for me.
+
+Thanks
+
+Christopher Rankin
+
+Example
+=======
+
+Create a new wizard controller for each wizard you wish to create.
+* Don't name your wizards like my_long_wizard_name_wizard. It will break the helpers.
+Name your wizard like so. mylongwizardname
+I like to name my wizards ending with the word wizard
+orderwizard becomes OrderWizard < ApplicationController ... if you dont like that sorry.
+
+So install the plugin into your vendor/plugins like normal
+
+# next generate a new controller wizard
+script/generate controller customwizard
+
+# Your Wizard controller should like something like this.
+class OrderWizard < ApplicationController
+
+ def index
+ # This is your wizards first page so get whatever objects you want
+ # to have available to your template.
+
+ # Let the template the wizard steps
+ # wizard (:current_step, :previous_step, :next_step)
+ wizard(:index, :index, :add_item)
+ end
+
+ def add_item
+ @items = Item.find(:all)
+ wizard(:add_item, :index) # There is no next step. You either submit the form or you go back.
+ end
+
+ # This method just saves the item from the submited form from add_item
+ def save_item
+ @item = Item.new(params[:item])
+ wizard(:save_item, :add_item, :complete_order) # Now you can go back and add another item or finish your order.
+ end
+
+ # The order is complete and saved. So what do we do for next page?
+ # Simple we can direct the user wherever we want. Because in the
+ # template we can change the name of the link like "Home" or "Add Item".
+ # So let's set previous_step to the start page of the wizard
+ # and keep next_step empty.
+ def complete_order
+ Order.complete(params[:id])
+ wizard(:complete_order, :index)
+ end
+
+end
+
+Now in our templates we can do something like
+views/orderwizard/index
+
+Some HTMl here...
+<%= link_to_next_step :order_wizard, :add_an_item %>
+
+this will build something like
+<a href="/orderwizard/add_item">Add an item</a>
+
+Here are all the available helpers
+
+<%= current_wizard_step %><br/> # prints the current step
+<%= next_wizard_step %><br/> # prints the next step
+<%= previous_wizard_step %><br/> # prints the previous step
+<%= link_to_next_step :wizard_name %><br/> # Link text defaults to "Next"
+<%= link_to_previous_step :wizard_name %><br/> # Link text defauts to "Previous"
+<%= link_to_next_step :wizard_name, :before_this %><br/> # Set link text to "Before this"
+<%= link_to_previous_step :wizard_name, :after_this %><br/># Set link text to "After this"
+
+
+Copyright (c) 2009 [name of plugin creator], 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 wizard plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the wizard plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'Wizard'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
@@ -0,0 +1,5 @@
+require 'wizard/application'
+require 'wizard/application_helper'
+
+ActionController::Base.send(:include, Wizard::ApplicationController)
+ActionView::Base.send(:include, Wizard::ApplicationHelper)
@@ -0,0 +1 @@
+# Install hook code here
@@ -0,0 +1,9 @@
+module Wizard #:nodoc
+ module ApplicationController
+ def wizard(current_step, previous_step, next_step)
+ session[:current_wizard_step] = current_step
+ session[:previous_wizard_step] = previous_step
+ session[:next_wizard_step] = next_step
+ end
+ end
+end
@@ -0,0 +1,25 @@
+module Wizard #:nodoc
+ module ApplicationHelper
+
+ def current_wizard_step
+ session[:current_wizard_step].to_s
+ end
+
+ def previous_wizard_step
+ session[:previous_wizard_step].to_s
+ end
+
+ def next_wizard_step
+ session[:next_wizard_step].to_s
+ end
+
+ def link_to_next_step(wizard_name, next_step_name=:next)
+ link_to next_step_name.to_s.humanize, :controller => wizard_name.to_s.camelize.downcase, :action => session[:next_wizard_step].to_s
+ end
+
+ def link_to_previous_step(wizard_name, previous_step_name=:previous)
+ link_to previous_step_name.to_s.humanize, :controller => wizard_name.to_s.camelize.downcase, :action => session[:previous_wizard_step].to_s
+ end
+
+ end
+end
@@ -0,0 +1,20 @@
+Copyright (c) 2009 [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.
@@ -0,0 +1,4 @@
+# desc "Explaining what the task does"
+# task :wizard do
+# # Task goes here
+# end
@@ -0,0 +1,8 @@
+require 'test/unit'
+
+class WizardTest < Test::Unit::TestCase
+ # Replace this with your real tests.
+ def test_this_plugin
+ flunk
+ end
+end
@@ -0,0 +1 @@
+# Uninstall hook code here

0 comments on commit 639f637

Please sign in to comment.