Class: Wizardly::Wizard::Configuration

Inherits:
Object
  • Object
show all
Includes:
TextHelpers
Defined in:
lib/wizardly/wizard/configuration.rb,
lib/wizardly/wizard/configuration/methods.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller, opts) ⇒ Configuration

enum_attr :persistance, %w(sandbox session database)

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/wizardly/wizard/configuration.rb', line 15

def initialize(controller, opts) #completed_redirect = nil, canceled_redirect = nil)
  @controller_class_name = controller.to_s.camelcase
  @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[: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] || :per_page
  @form_data = opts[:form_data] || :session
  raise(ArgumentError, ":persist_model option must be one of :once or :per_page", caller) unless [:once, :per_page].include?(@persist_model)
  raise(ArgumentError, ":form_data option must be one of :sandbox or :session", caller) unless [:sandbox, :session].include?(@form_data)
  @page_order = []
  @pages = {}
  @buttons = nil
  @default_buttons = Hash[*[:next, :back, :cancel, :finish, :skip].collect {|default| [default, Button.new(default)] }.flatten]
end

Instance Attribute Details

#canceled_redirectObject (readonly)

Returns the value of attribute canceled_redirect.



11
12
13
# File 'lib/wizardly/wizard/configuration.rb', line 11

def canceled_redirect
  @canceled_redirect
end

#completed_redirectObject (readonly)

Returns the value of attribute completed_redirect.



11
12
13
# File 'lib/wizardly/wizard/configuration.rb', line 11

def completed_redirect
  @completed_redirect
end

#controller_class_nameObject (readonly)

Returns the value of attribute controller_class_name.



11
12
13
# File 'lib/wizardly/wizard/configuration.rb', line 11

def controller_class_name
  @controller_class_name
end

#controller_nameObject (readonly)

Returns the value of attribute controller_name.



11
12
13
# File 'lib/wizardly/wizard/configuration.rb', line 11

def controller_name
  @controller_name
end

#controller_pathObject (readonly)

Returns the value of attribute controller_path.



11
12
13
# File 'lib/wizardly/wizard/configuration.rb', line 11

def controller_path
  @controller_path
end

#page_orderObject (readonly)

Returns the value of attribute page_order.



11
12
13
# File 'lib/wizardly/wizard/configuration.rb', line 11

def page_order
  @page_order
end

#pagesObject (readonly)

Returns the value of attribute pages.



11
12
13
# File 'lib/wizardly/wizard/configuration.rb', line 11

def pages
  @pages
end

Class Method Details

.create(controller_name, model_name, opts = {}, &block) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/wizardly/wizard/configuration.rb', line 63

def self.create(controller_name, model_name, opts={}, &block)
  # controller_name = controller_name.to_s.underscore.sub(/_controller$/, '').to_sym
  model_name = model_name.to_s.underscore.to_sym
  config = Wizardly::Wizard::Configuration.new(controller_name, opts)
  config.inspect_model!(model_name)
  Wizardly::Wizard::DSL.new(config).instance_eval(&block) if block_given?
  config
end

Instance Method Details

#_change_button(name) ⇒ Object



129
130
131
132
133
134
# File 'lib/wizardly/wizard/configuration.rb', line 129

def _change_button(name)
  raise(WizardConfigurationError, "Button :#{name} in _change_button() call does not exist", caller) unless self.buttons.key?(name)
  _buttons = self.buttons
  @buttons = nil # clear the buttons for regeneration after change in next line
  _buttons[name]
end

#_create_button(name, opts) ⇒ Object



135
136
137
138
139
140
# File 'lib/wizardly/wizard/configuration.rb', line 135

def _create_button(name, opts)
  id = opts[:id] || button_name_to_symbol(name)
  raise(WizardConfigurationError, "Button '#{name}' with id :#{id} cannot be created. The ID already exists.", caller) if self.buttons.key?(id)
  @buttons=nil
  @default_buttons[id] = UserDefinedButton.new(id, name)
end

#_mask_passwords(passwords) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/wizardly/wizard/configuration.rb', line 142

def _mask_passwords(passwords)
  case passwords
  when String
    passwords = [passwords.to_sym]
  when Symbol
    passwords = [passwords]
  when Array
  else
    raise(WizardlyConfigurationError, "mask_passwords method only accepts string, symbol or array of password fields")
  end
  @password_fields.push(*passwords).uniq!
end

#_set_page(name) ⇒ Object



141
# File 'lib/wizardly/wizard/configuration.rb', line 141

def _set_page(name); @pages[name]; end

#_when_canceled_redirect_to(redir) ⇒ Object



128
# File 'lib/wizardly/wizard/configuration.rb', line 128

def _when_canceled_redirect_to(redir); @canceled_redirect = redir; end

#_when_completed_redirect_to(redir) ⇒ Object

internal DSL method handlers



127
# File 'lib/wizardly/wizard/configuration.rb', line 127

def _when_completed_redirect_to(redir); @completed_redirect = redir; end

#button_for_function(name) ⇒ Object



56
# File 'lib/wizardly/wizard/configuration.rb', line 56

def button_for_function(name); @default_buttons[name]; end

#buttonsObject



57
58
59
60
61
# File 'lib/wizardly/wizard/configuration.rb', line 57

def buttons
  return @buttons if @buttons
  # reduce buttons
  @buttons = Hash[*@default_buttons.collect{|k,v|[v.id, v]}.flatten]
end

#first_page?(name) ⇒ Boolean

Returns:

  • (Boolean)


44
# File 'lib/wizardly/wizard/configuration.rb', line 44

def first_page?(name); @page_order.first == name; end

#form_data_keep_in_session?Boolean

Returns:

  • (Boolean)


38
# File 'lib/wizardly/wizard/configuration.rb', line 38

def form_data_keep_in_session?; @form_data == :session; end

#guard?Boolean

Returns:

  • (Boolean)


36
# File 'lib/wizardly/wizard/configuration.rb', line 36

def guard?; @guard_entry; end

#initial_referer_keyObject



74
75
76
# File 'lib/wizardly/wizard/configuration/methods.rb', line 74

def initial_referer_key
  @initial_referer_key ||= "#{self.controller_path.sub(/\//, '')}_irk".to_sym
end

#inspect_model!(model) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/wizardly/wizard/configuration.rb', line 73

def inspect_model!(model)
  # first examine the model symbol, transform and see if the constant
  # exists
  begin
    @wizard_model_sym = model.to_sym
    @wizard_model_class_name = model.to_s.camelize
    @wizard_model_const = @wizard_model_class_name.constantize
  rescue Exception=>e
    raise ModelNotFoundError, "Cannot convert :#{@wizard_model_sym} to model constant for #{@wizard_model_class_name}: " + e.message, caller
  end

  begin
    @page_order = @wizard_model_const.validation_group_order 
  rescue Exception => e
    raise ValidationGroupError, "Unable to read validation groups from #{@wizard_model_class_name}: " + e.message, caller
  end
  raise(ValidationGroupError, "No validation groups defined for model #{@wizard_model_class_name}", caller) unless (@page_order && !@page_order.empty?)

  begin
    groups = @wizard_model_const.validation_groups
    enum_attrs = @wizard_model_const.respond_to?(:enumerated_attributes) ? @wizard_model_const.enumerated_attributes.collect {|k,v| k } : []
    model_inst = @wizard_model_const.new
    last_index = @page_order.size-1
    @page_order.each_with_index do |p, index|
      fields = groups[p].map do |f|
        column = model_inst.column_for_attribute(f)
        type = case
        when enum_attrs.include?(f) then :enum
        when (@password_fields && @password_fields.include?(f)) then :password
        else
          column ? column.type : :string
        end
        PageField.new(f, type)
      end
      page = Page.new(self, p, fields)

      # default button settings based on order, can be altered by
      # set_page(@id).buttons_to []
      buttons = []
      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 (@include_skip_button && index != last_index)
      buttons << @default_buttons[:cancel] if (@include_cancel_button)
      page.buttons = buttons
      @pages[page.id] = page
    end
  rescue Exception => e
    raise ValidationGroupError, "Failed to configure wizard from #{@wizard_model_class_name} validation groups: " + e.message, caller
  end
end

#last_page?(name) ⇒ Boolean

Returns:

  • (Boolean)


45
# File 'lib/wizardly/wizard/configuration.rb', line 45

def last_page?(name); @page_order.last == name; end

#modelObject



39
# File 'lib/wizardly/wizard/configuration.rb', line 39

def model; @wizard_model_sym; end

#model_class_nameObject



41
# File 'lib/wizardly/wizard/configuration.rb', line 41

def model_class_name; @wizard_model_class_name; end

#model_constObject



42
# File 'lib/wizardly/wizard/configuration.rb', line 42

def model_const; @wizard_model_const; end

#model_instance_variableObject



40
# File 'lib/wizardly/wizard/configuration.rb', line 40

def model_instance_variable; "@#{@wizard_model_sym.to_s}"; end

#next_page(name) ⇒ Object



46
47
48
49
50
# File 'lib/wizardly/wizard/configuration.rb', line 46

def next_page(name)
  index = @page_order.index(name)
  index += 1 unless self.last_page?(name)
  @page_order[index]
end

#persist_keyObject



77
78
79
# File 'lib/wizardly/wizard/configuration/methods.rb', line 77

def persist_key;
  @persist_key ||= "#{self.controller_path.sub(/\//, '')}_dat".to_sym 
end

#persist_model_per_page?Boolean

Returns:

  • (Boolean)


37
# File 'lib/wizardly/wizard/configuration.rb', line 37

def persist_model_per_page?; @persist_model == :per_page; end

#previous_page(name) ⇒ Object



51
52
53
54
55
# File 'lib/wizardly/wizard/configuration.rb', line 51

def previous_page(name)
  index = @page_order.index(name)
  index -= 1 unless self.first_page?(name)
  @page_order[index]
end


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/wizardly/wizard/configuration/methods.rb', line 5

def print_callback_macros
  macros = [ 
    %w(on_post _on_post_%s_form),
    %w(on_get _on_get_%s_form),
    %w(on_errors _on_invalid_%s_form)
  ]
  self.buttons.each do |id, button|
    macros << ['on_'+ id.to_s, '_on_%s_form_'+ id.to_s ]
  end
  mb = StringIO.new
  macros.each do |macro|
    mb << <<-MACRO
  def self.#{macro.first}(*args, &block)
    self._define_action_callback_macro('#{macro.first}', '#{macro.last}', *args, &block)
  end
MACRO
  end
  mb << <<-DEFMAC
  def self._define_action_callback_macro(macro_first, macro_last, *args, &block)
    return if args.empty?
    all_forms = #{page_order.inspect}
    if args.include?(:all)
forms = all_forms
    else
forms = args.map do |fa|
  unless all_forms.include?(fa)
    raise(ArgumentError, ":"+fa.to_s+" in callback '" + macro_first + "' is not a form defined for the wizard", caller)
  end
  fa
end
    end
    forms.each do |form|
self.send(:define_method, sprintf(macro_last, form.to_s), &block )
hide_action macro_last.to_sym
    end
  end

DEFMAC
  
  [
    %w(on_completed _after_wizard_save)
  ].each do |macro|
    mb << <<-EVENTS
  def self.#{macro.first}(&block)
    self.send(:define_method, :#{macro.last}, &block )
  end
EVENTS
  end
  mb.string
end


153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/wizardly/wizard/configuration/methods.rb', line 153

def print_callbacks
  finish = self.button_for_function(:finish).id
  skip = self.button_for_function(:skip).id
  back = self.button_for_function(:back).id
  cancel = self.button_for_function(:cancel).id
<<-CALLBACKS 
  protected
  def _on_wizard_#{finish}
    return if @wizard_completed_flag
    @#{self.model}.save_without_validation! if @#{self.model}.changed?
    @wizard_completed_flag = true
    reset_wizard_form_data
    _wizard_final_redirect_to(:completed)
  end
  def _on_wizard_#{skip}
    self.progression = self.progression - [@step]
    redirect_to(:action=>wizard_config.next_page(@step)) unless self.performed?
  end
  def _on_wizard_#{back} 
    redirect_to(:action=>(previous_in_progression_from(@step) || :#{self.page_order.first})) unless self.performed?
  end
  def _on_wizard_#{cancel}
    _wizard_final_redirect_to(:canceled)
  end
  def _wizard_final_redirect_to(type)
    init = (type == :canceled && wizard_config.form_data_keep_in_session?) ?
self.initial_referer :
reset_wizard_session_vars
    unless self.performed?
redir = (type == :canceled ? wizard_config.canceled_redirect : wizard_config.completed_redirect) || init
return redirect_to(redir) if redir
raise Wizardly::RedirectNotDefinedError, "No redirect was defined for completion or canceling the wizard.  Use :completed and :canceled options to define redirects.", caller
    end
  end
  hide_action :_on_wizard_#{finish}, :_on_wizard_#{skip}, :_on_wizard_#{back}, :_on_wizard_#{cancel}, :_wizard_final_redirect_to
CALLBACKS
end


155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/wizardly/wizard/configuration.rb', line 155

def print_config
  io = StringIO.new
  class_name = controller_name.to_s.camelize
  class_name += 'Controller' unless class_name =~ /Controller$/
  io.puts "#{class_name} wizard configuration"
  io.puts
  io.puts "model: #{model_class_name}"
  io.puts "instance: @#{model}"
  io.puts
  io.puts "pages:"
  self.page_order.each do |pid|
    page = pages[pid]
    # io.puts "  #{index+1}. '#{page.title}' page (:#{page.id}) has"
    io.puts "  '#{page.title}' page (:#{page.id}) has"
    io.puts "    --fields: #{page.fields.inject([]){|a, f| a << '"'+f.name.to_s.titleize+'" [:'+f.column_type.to_s+']'}.join(', ')}"
    io.puts "    --buttons: #{page.buttons.inject([]){|a, b| a << b.name.to_s }.join(', ')}"
  end
  io.puts
  io.puts "redirects:"
  io.puts "  when completed: #{completed_redirect ? completed_redirect.inspect : 'redirects to initial referer by default (specify :completed=>url to override)'}"
  io.puts "  when canceled: #{canceled_redirect ? canceled_redirect.inspect : 'redirects to initial referer by default (specify :canceled=>url to override)'}"
  io.puts   
  io.puts "buttons:"
  self.buttons.each do |k, b|
    bs = StringIO.new
    bs << "  #{b.name} (:#{b.id}) "
    if (b.user_defined?)
      bs << "-- user defined button and function"
    else
      dk = @default_buttons.index(b)
      bs << "-- used for internal <#{dk}> functionality"
    end
    io.puts bs.string
  end
  io.puts 
  io.string      
end


191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# File 'lib/wizardly/wizard/configuration/methods.rb', line 191

def print_helpers
  next_id = self.button_for_function(:next).id
  finish_id = self.button_for_function(:finish).id
  first_page = self.page_order.first
  finish_button = self.button_for_function(:finish).id        
  guard_line = self.guard? ? '' : 'return check_progression #guard entry disabled'
  mb = StringIO.new
  mb << <<-PROGRESSION
  protected
  def do_not_complete; @do_not_complete = true; end
  def previous_in_progression_from(step)
    po = #{self.page_order.inspect}
    p = self.progression
    p -= po[po.index(step)..-1]
    self.progression = p
    p.last
  end
  def check_progression
    p = self.progression
    a = params[:action].to_sym
    return if p.last == a
    po = #{self.page_order.inspect}
    return unless (ai = po.index(a))
    p -= po[ai..-1]
    p << a
    self.progression = p
  end        
PROGRESSION
  if self.form_data_keep_in_session?
    mb << <<-SESSION
  # for :form_data=>:session
  def guard_entry 
    if (r = request.env['HTTP_REFERER'])
begin
  h = ::ActionController::Routing::Routes.recognize_path(URI.parse(r).path, {:method=>:get})
rescue
else
  return check_progression if (h[:controller]||'') == '#{self.controller_path}'
  self.initial_referer = h unless self.initial_referer
end
    end
    # coming from outside the controller or no route for GET
    #{guard_line}
    if (params[:action] == '#{first_page}' || params[:action] == 'index')
return check_progression
    elsif self.wizard_form_data
p = self.progression
return check_progression if p.include?(params[:action].to_sym)
return redirect_to(:action=>(p.last||:#{first_page}))
    end
    redirect_to :action=>:#{first_page}
  end
  hide_action :guard_entry

SESSION
  else
    mb << <<-SANDBOX
  # for :form_data=>:sandbox            
  def guard_entry 
    if (r = request.env['HTTP_REFERER'])
begin
  h = ::ActionController::Routing::Routes.recognize_path(URI.parse(r).path, {:method=>:get})
rescue
else
  return check_progression if (h[:controller]||'') == '#{self.controller_path}'
  self.initial_referer = h
end
    else
self.initial_referer = nil 
    end
    # coming from outside the controller
    reset_wizard_form_data 
    #{guard_line}
    return redirect_to(:action=>:#{first_page}) unless (params[:action] || '') == '#{first_page}'
    check_progression
  end
  hide_action :guard_entry

SANDBOX
  end
  mb << <<-HELPERS
  def render_and_return
    return if callback_performs_action?('_on_get_'[email protected]_s+'_form')
    render_wizard_form
    render unless self.performed?
  end

  def complete_wizard(redirect = nil)
    unless @wizard_completed_flag
@#{self.model}.save_without_validation!
callback_performs_action?(:_after_wizard_save)
    end
    redirect_to redirect if (redirect && !self.performed?)
    return if @wizard_completed_flag
    _on_wizard_#{finish_button}
    redirect_to(#{Utils.formatted_redirect(self.completed_redirect)}) unless self.performed?
  end
  def _build_wizard_model
    if self.wizard_config.persist_model_per_page?
h = self.wizard_form_data
if (h && model_id = h['id'])
    _model = #{self.model_class_name}.find(model_id)
    _model.attributes = params[:#{self.model}]||{}
    @#{self.model} = _model
    return
end
@#{self.model} = #{self.model_class_name}.new(params[:#{self.model}])
    else # persist data in session or flash
h = (self.wizard_form_data||{}).merge(params[:#{self.model}] || {})
@#{self.model} = #{self.model_class_name}.new(h)
    end
  end
  def _preserve_wizard_model
    return unless (@#{self.model} && !@wizard_completed_flag)
    if self.wizard_config.persist_model_per_page?
@#{self.model}.save_without_validation!
if request.get?
  @#{self.model}.errors.clear
else
  @#{self.model}.reject_non_validation_group_errors
end
self.wizard_form_data = {'id'=>@#{self.model}.id}
    else
self.wizard_form_data = @#{self.model}.attributes
    end
  end
  hide_action :_build_wizard_model, :_preserve_wizard_model

  def initial_referer
    session[:#{self.initial_referer_key}]
  end
  def initial_referer=(val)
    session[:#{self.initial_referer_key}] = val
  end
  def progression=(array)
    session[:#{self.progression_key}] = array
  end
  def progression
    session[:#{self.progression_key}]||[]
  end
  hide_action :progression, :progression=, :initial_referer, :initial_referer=

  def wizard_form_data=(hash)
    if wizard_config.form_data_keep_in_session?
session[:#{self.persist_key}] = hash
    else
if hash
  flash[:#{self.persist_key}] = hash
else
  flash.discard(:#{self.persist_key})
end
    end
  end

  def reset_wizard_form_data; self.wizard_form_data = nil; end
  def wizard_form_data
    wizard_config.form_data_keep_in_session? ? session[:#{self.persist_key}] : flash[:#{self.persist_key}]
  end
  hide_action :wizard_form_data, :wizard_form_data=, :reset_wizard_form_data

  def render_wizard_form
  end
  hide_action :render_wizard_form

  def performed?; super; end
  hide_action :performed?

  def underscore_button_name(value)
    value.to_s.strip.squeeze(' ').gsub(/ /, '_').downcase
  end
  hide_action :underscore_button_name

  def reset_wizard_session_vars
    self.progression = nil
    init = self.initial_referer
    self.initial_referer = nil
    init
  end
  hide_action :reset_wizard_session_vars

  def check_action_for_button
    button_id = nil
    case 
    when params[:commit]
button_name = params[:commit]
button_id = underscore_button_name(button_name).to_sym
    when ((b_ar = self.wizard_config.buttons.find{|k,b| params[k]}) && params[b_ar.first] == b_ar.last.name)
button_name = b_ar.last.name
button_id = b_ar.first
    end
    if button_id
unless [:#{next_id}, :#{finish_id}].include?(button_id) 
  action_method_name = "_on_" + params[:action].to_s + "_form_" + button_id.to_s
  callback_performs_action?(action_method_name)
  unless ((btn_obj = self.wizard_config.buttons[button_id]) == nil || btn_obj.user_defined?)
    method_name = "_on_wizard_" + button_id.to_s
    if (self.method(method_name))
      self.__send__(method_name)
    else
      raise MissingCallbackError, "Callback method either '" + action_method_name + "' or '" + method_name + "' not defined", caller
    end
  end
end
    end
    button_id
  end
  hide_action :check_action_for_button

  @wizard_callbacks ||= []
  def self.wizard_callbacks; @wizard_callbacks; end

  def callback_performs_action?(methId)
    cache = self.class.wizard_callbacks
    return false if cache.include?(methId)

    if self.respond_to?(methId, true)
self.send(methId)
    else
cache << methId
return false
    end
    
    self.performed?
  end
  hide_action :callback_performs_action?

HELPERS
  mb.string
end


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/wizardly/wizard/configuration/methods.rb', line 84

def print_page_action_method(id)
  page = @pages[id]
  finish_button = self.button_for_function(:finish).id
  next_button = self.button_for_function(:next).id

  (mb = StringIO.new) << <<-ONE
  def #{page.name}
    begin
@step = :#{id}
@wizard = wizard_config
@title = '#{page.title}'
@description = '#{page.description}'
_build_wizard_model
if request.post? && callback_performs_action?(:_on_post_#{id}_form)
  raise CallbackError, "render or redirect not allowed in :on_post(:#{id}) callback", caller
end
button_id = check_action_for_button
return if performed?
if request.get?
  return if callback_performs_action?(:_on_get_#{id}_form)
  render_wizard_form
  return
end

# @#{self.model}.enable_validation_group :#{id}
unless @#{self.model}.valid?(:#{id})
  return if callback_performs_action?(:_on_invalid_#{id}_form)
  render_wizard_form
  return
end

@do_not_complete = false
  ONE
  if self.last_page?(id)
    mb << <<-TWO
callback_performs_action?(:_on_#{id}_form_#{finish_button})
complete_wizard unless @do_not_complete
  TWO
  elsif self.first_page?(id)
    mb << <<-THREE
if button_id == :#{finish_button}
  callback_performs_action?(:_on_#{id}_form_#{finish_button})
  complete_wizard unless @do_not_complete
  return
end
return if callback_performs_action?(:_on_#{id}_form_#{next_button})
redirect_to :action=>:#{self.next_page(id)}
  THREE
  else 
    mb << <<-FOUR
if button_id == :#{finish_button}
  callback_performs_action?(:_on_#{id}_form_#{finish_button})
  complete_wizard unless @do_not_complete
  return
end
return if callback_performs_action?(:_on_#{id}_form_#{next_button})
redirect_to :action=>:#{self.next_page(id)}
  FOUR
  end
  
  mb << <<-ENSURE
    ensure
_preserve_wizard_model
    end
  end        
ENSURE
  mb.string
end


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/wizardly/wizard/configuration/methods.rb', line 56

def print_page_action_methods
  mb = StringIO.new
  self.pages.each do |id, p|
    mb << <<-COMMENT

  # #{id} action method
#{self.print_page_action_method(id)}
    COMMENT
  end
  mb << <<-INDEX
  def index
    redirect_to :action=>:#{self.page_order.first}
  end

  INDEX
  mb.string
end

#progression_keyObject



80
81
82
# File 'lib/wizardly/wizard/configuration/methods.rb', line 80

def progression_key
  @progression_key ||= "#{self.controller_path.sub(/\//, '')}_prg".to_sym
end