This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Expose wizardly_attributes to allow configuring non-DB persisted data.

  • Loading branch information...
ppg committed May 8, 2012
1 parent aff3636 commit 9768787e55bf8ab210dc95342cda7927b963e8c8
Showing with 49 additions and 32 deletions.
  1. +31 −31 app/controllers/generated_controller.rb
  2. +17 −0 lib/validation_group.rb
  3. +1 −1 lib/wizardly/wizard/configuration/methods.rb
@@ -41,80 +41,80 @@ def finish
end
- # init action method
- def init
+ # second action method
+ def second
begin
- @step = :init
+ @step = :second
@wizard = wizard_config
- @title = 'Init'
+ @title = 'Second'
@description = ''
_build_wizard_model
- if request.post? && callback_performs_action?(:_on_post_init_form)
- raise CallbackError, "render or redirect not allowed in :on_post(:init) callback", caller
+ if request.post? && callback_performs_action?(:_on_post_second_form)
+ raise CallbackError, "render or redirect not allowed in :on_post(:second) callback", caller
end
button_id = check_action_for_button
return if performed?
if request.get?
- return if callback_performs_action?(:_on_get_init_form)
+ return if callback_performs_action?(:_on_get_second_form)
render_wizard_form
return
end
- # @user.enable_validation_group :init
- unless @user.valid?(:init)
- return if callback_performs_action?(:_on_invalid_init_form)
+ # @user.enable_validation_group :second
+ unless @user.valid?(:second)
+ return if callback_performs_action?(:_on_invalid_second_form)
render_wizard_form
return
end
@do_not_complete = false
if button_id == :finish
- callback_performs_action?(:_on_init_form_finish)
+ callback_performs_action?(:_on_second_form_finish)
complete_wizard unless @do_not_complete
return
end
- return if callback_performs_action?(:_on_init_form_next)
- redirect_to :action=>:second
+ return if callback_performs_action?(:_on_second_form_next)
+ redirect_to :action=>:finish
ensure
_preserve_wizard_model
end
end
- # second action method
- def second
+ # init action method
+ def init
begin
- @step = :second
+ @step = :init
@wizard = wizard_config
- @title = 'Second'
+ @title = 'Init'
@description = ''
_build_wizard_model
- if request.post? && callback_performs_action?(:_on_post_second_form)
- raise CallbackError, "render or redirect not allowed in :on_post(:second) callback", caller
+ 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_second_form)
+ return if callback_performs_action?(:_on_get_init_form)
render_wizard_form
return
end
- # @user.enable_validation_group :second
- unless @user.valid?(:second)
- return if callback_performs_action?(:_on_invalid_second_form)
+ # @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_second_form_finish)
+ callback_performs_action?(:_on_init_form_finish)
complete_wizard unless @do_not_complete
return
end
- return if callback_performs_action?(:_on_second_form_next)
- redirect_to :action=>:finish
+ return if callback_performs_action?(:_on_init_form_next)
+ redirect_to :action=>:second
ensure
_preserve_wizard_model
end
@@ -183,7 +183,7 @@ def guard_entry
h = ::ActionController::Routing::Routes.recognize_path(URI.parse(r).path, {:method=>:get})
rescue
else
- return check_progression if (h[:controller]||'') == 'generated'
+ return check_progression if (h[:controller]||'') == controller_path
self.initial_referer = h unless self.initial_referer
end
end
@@ -242,7 +242,7 @@ def _preserve_wizard_model
end
self.wizard_form_data = {'id'=>@user.id}
else
- self.wizard_form_data = @user.attributes
+ self.wizard_form_data = @user.wizardly_attributes
end
end
hide_action :_build_wizard_model, :_preserve_wizard_model
@@ -365,12 +365,12 @@ def self.on_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.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_back(*args, &block)
+ self._define_action_callback_macro('on_back', '_on_%s_form_back', *args, &block)
+ end
def self._define_action_callback_macro(macro_first, macro_last, *args, &block)
return if args.empty?
all_forms = [:init, :second, :finish]
View
@@ -40,6 +40,16 @@ def validation_group(name, options={})
alias_method_chain :valid?, :validation_group
end
end
+
+ # TODO Consider making a more formal accessor for this class attribute
+ def wizardly_attribute(name)
+ attr_accessor name
+ wizardly_attributes << name
+ end
+
+ def wizardly_attributes
+ (@wizardly_attributes ||= [])
+ end
end
module InstanceMethods # included in every model which calls validation_group
@@ -100,6 +110,13 @@ def valid_with_validation_group?(group=nil)
self.enable_validation_group(group) if group
valid_without_validation_group?
end
+
+ # Exposes a hook to add more attributes to persist with wizardly
+ def wizardly_attributes
+ self.class.wizardly_attributes.inject(attributes) do |attrs, attr_name|
+ attrs.merge(attr_name.to_s => send(attr_name))
+ end
+ end
end
module Errors # included in ActiveRecord::Errors
@@ -318,7 +318,7 @@ def _preserve_wizard_model
end
self.wizard_form_data = {'id'=>@#{self.model}.id}
else
- self.wizard_form_data = @#{self.model}.attributes
+ self.wizard_form_data = @#{self.model}.wizardly_attributes
end
end
hide_action :_build_wizard_model, :_preserve_wizard_model

0 comments on commit 9768787

Please sign in to comment.