Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Chris Rankin
committed
Jan 22, 2009
0 parents
commit 639f637
Showing
10 changed files
with
182 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require 'wizard/application' | ||
require 'wizard/application_helper' | ||
|
||
ActionController::Base.send(:include, Wizard::ApplicationController) | ||
ActionView::Base.send(:include, Wizard::ApplicationHelper) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Install hook code here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# desc "Explaining what the task does" | ||
# task :wizard do | ||
# # Task goes here | ||
# end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Uninstall hook code here |