Skip to content

Commit

Permalink
adds :cancel option
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffp committed Aug 31, 2009
1 parent f6e1d76 commit b336473
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 55 deletions.
4 changes: 3 additions & 1 deletion README.rdoc
Expand Up @@ -186,12 +186,14 @@ Here's a list of options for the +act_wizardly_for+ controller macro
:completed => '/main/finished'
:canceled => {:controller=>'main', :action=>'canceled'}
:skip => true
:cancel => false
:guard => false
:mask_fields => [:password, :password_confirmation] (by default)
:persist_model => {:once|:per_page}
:form_data => {:sandbox|:session}

Setting the :skip option to +true+ tells the scaffold helpers to include a skip button on each page.
Setting the :skip option to +true+ tells the scaffold helpers to include a skip button on each page.
The :cancel option set to +false+ removes the 'cancel' button from the wizard views.
The :mask_fields options tells the scaffold generator which fields to generate as 'type=password' fields.
Remaining options are explained below.

Expand Down
98 changes: 49 additions & 49 deletions app/controllers/generated_controller.rb
Expand Up @@ -6,48 +6,6 @@ class GeneratedController < ApplicationController
before_filter :guard_entry


# init action method
def init
begin
@step = :init
@wizard = wizard_config
@title = 'Init'
@description = ''
h = (self.wizard_form_data||{}).merge(params[:user] || {})
@user = build_wizard_model(h)
if request.post? && callback_performs_action?(:_on_post_init_form)
raise CallbackError, "render or redirect not allowed in :on_post(:init) callback", caller
end
button_id = check_action_for_button
return if performed?
if request.get?
return if callback_performs_action?(:_on_get_init_form)
render_wizard_form
return
end

# @user.enable_validation_group :init
unless @user.valid?(:init)
return if callback_performs_action?(:_on_invalid_init_form)
render_wizard_form
return
end

@do_not_complete = false
if button_id == :finish
callback_performs_action?(:_on_init_form_finish)
complete_wizard unless @do_not_complete
return
end

return if callback_performs_action?(:_on_init_form_next)
redirect_to :action=>:second
ensure
self.wizard_form_data = h.merge(@user.attributes) if (@user && !@wizard_completed_flag)
end
end


# second action method
def second
begin
Expand Down Expand Up @@ -125,6 +83,48 @@ def finish
end
end


# init action method
def init
begin
@step = :init
@wizard = wizard_config
@title = 'Init'
@description = ''
h = (self.wizard_form_data||{}).merge(params[:user] || {})
@user = build_wizard_model(h)
if request.post? && callback_performs_action?(:_on_post_init_form)
raise CallbackError, "render or redirect not allowed in :on_post(:init) callback", caller
end
button_id = check_action_for_button
return if performed?
if request.get?
return if callback_performs_action?(:_on_get_init_form)
render_wizard_form
return
end

# @user.enable_validation_group :init
unless @user.valid?(:init)
return if callback_performs_action?(:_on_invalid_init_form)
render_wizard_form
return
end

@do_not_complete = false
if button_id == :finish
callback_performs_action?(:_on_init_form_finish)
complete_wizard unless @do_not_complete
return
end

return if callback_performs_action?(:_on_init_form_next)
redirect_to :action=>:second
ensure
self.wizard_form_data = h.merge(@user.attributes) if (@user && !@wizard_completed_flag)
end
end

def index
redirect_to :action=>:init
end
Expand Down Expand Up @@ -339,20 +339,20 @@ def self.on_get(*args, &block)
def self.on_errors(*args, &block)
self._define_action_callback_macro('on_errors', '_on_invalid_%s_form', *args, &block)
end
def self.on_next(*args, &block)
self._define_action_callback_macro('on_next', '_on_%s_form_next', *args, &block)
def self.on_finish(*args, &block)
self._define_action_callback_macro('on_finish', '_on_%s_form_finish', *args, &block)
end
def self.on_skip(*args, &block)
self._define_action_callback_macro('on_skip', '_on_%s_form_skip', *args, &block)
end
def self.on_back(*args, &block)
self._define_action_callback_macro('on_back', '_on_%s_form_back', *args, &block)
end
def self.on_cancel(*args, &block)
self._define_action_callback_macro('on_cancel', '_on_%s_form_cancel', *args, &block)
end
def self.on_finish(*args, &block)
self._define_action_callback_macro('on_finish', '_on_%s_form_finish', *args, &block)
end
def self.on_skip(*args, &block)
self._define_action_callback_macro('on_skip', '_on_%s_form_skip', *args, &block)
def self.on_next(*args, &block)
self._define_action_callback_macro('on_next', '_on_%s_form_next', *args, &block)
end
def self._define_action_callback_macro(macro_first, macro_last, *args, &block)
return if args.empty?
Expand Down
11 changes: 6 additions & 5 deletions lib/wizardly/wizard/configuration.rb
Expand Up @@ -17,9 +17,10 @@ def initialize(controller, opts) #completed_redirect = nil, canceled_redirect =
@controller_class_name += 'Controller' unless @controller_class_name =~ /Controller$/
@controller_path = @controller_class_name.sub(/Controller$/,'').underscore
@controller_name = @controller_class_name.demodulize.sub(/Controller$/,'').underscore
@completed_redirect = opts[:redirect] || opts[:completed] || opts[:when_completed] #format_redirect(completed_redirect)
@canceled_redirect = opts[:redirect] || opts[:canceled] || opts[:when_canceled]
@allow_skipping = opts[:skip] || opts[:allow_skip] || opts[:allow_skipping] || false
@completed_redirect = opts[:completed] || opts[:when_completed] || opts[:redirect] #format_redirect(completed_redirect)
@canceled_redirect = opts[:canceled] || opts[:when_canceled] || opts[:redirect]
@include_skip_button = opts[:skip] || opts[:allow_skip] || opts[:allow_skipping] || false
@include_cancel_button = opts.key?(:cancel) ? opts[:cancel] : true
@guard_entry = opts.key?(:guard) ? opts[:guard] : true
@password_fields = opts[:mask_fields] || opts[:mask_passwords] || [:password, :password_confirmation]
@persist_model = opts[:persist_model] || :once
Expand Down Expand Up @@ -111,8 +112,8 @@ def inspect_model!(model)
buttons << @default_buttons[:next] unless index >= last_index
buttons << @default_buttons[:finish] if index == last_index
buttons << @default_buttons[:back] unless index == 0
buttons << @default_buttons[:skip] if (@allow_skipping && index != last_index)
buttons << @default_buttons[:cancel]
buttons << @default_buttons[:skip] if (@include_skip_button && index != last_index)
buttons << @default_buttons[:cancel] if (@include_cancel_button)
page.buttons = buttons
@pages[page.id] = page
end
Expand Down

0 comments on commit b336473

Please sign in to comment.