jeffp-wizardly 0.1.8 → 0.1.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -43,7 +43,18 @@ module ValidationGroup
43
43
  end
44
44
 
45
45
  module InstanceMethods # included in every model which calls validation_group
46
-
46
+ #needs testing
47
+ # def reset_validation_group(group)
48
+ # group_classes = self.class.validation_group_classes
49
+ # found = ValidationGroup::Util.current_and_ancestors(self.class).find do |klass|
50
+ # group_classes[klass] && group_classes[klass].include?(group)
51
+ # end
52
+ # if found
53
+ # group_classes[found][group].each do |field|
54
+ # self[field] = nil
55
+ # end
56
+ # end
57
+ # end
47
58
  def enable_validation_group(group)
48
59
  # Check if given validation group is defined for current class or one of
49
60
  # its ancestors
@@ -32,18 +32,5 @@ module Wizardly
32
32
  public
33
33
  def wizard_config; self.class.wizard_config; end
34
34
 
35
- private
36
- def reset_wizard_session_vars
37
- session[:progression] = nil
38
- init = session[:initial_referer]
39
- session[:initial_referer] = nil
40
- init
41
- end
42
-
43
- # better name: underscore_button_name, changes submit_tag button names like
44
- # 'Next Step' ==> 'next_step', used to identify callback methods
45
- def methodize_button_name(value)
46
- value.to_s.strip.squeeze(' ').gsub(/ /, '_').downcase
47
- end
48
35
  end
49
36
  end
@@ -31,6 +31,7 @@ module Wizardly
31
31
  end
32
32
  end
33
33
 
34
+ def guard?; @guard_entry; end
34
35
  def persist_model_per_page?; @persist_model == :per_page; end
35
36
  def form_data_keep_in_session?; @form_data == :session; end
36
37
  def model; @wizard_model_sym; end
@@ -62,6 +62,8 @@ MACRO
62
62
  page = @pages[id]
63
63
  finish_button = self.button_for_function(:finish).id
64
64
  next_button = self.button_for_function(:next).id
65
+ model_persist_line = self.persist_model_per_page? ? 'save_wizard_model!' : ''
66
+
65
67
  (mb = StringIO.new) << <<-ONE
66
68
  def #{page.name}
67
69
  begin
@@ -104,8 +106,7 @@ MACRO
104
106
  redirect_to #{Utils.formatted_redirect(self.completed_redirect)} unless self.performed?
105
107
  return
106
108
  end
107
- save_wizard_model! if wizard_config.persist_model_per_page?
108
- # session[:progression] = [:#{id}]
109
+ #{model_persist_line}
109
110
  return if callback_performs_action?(:on_#{id}_form_#{next_button})
110
111
  redirect_to :action=>:#{self.next_page(id)}
111
112
  THREE
@@ -117,8 +118,7 @@ MACRO
117
118
  redirect_to #{Utils.formatted_redirect(self.completed_redirect)} unless self.performed?
118
119
  return
119
120
  end
120
- save_wizard_model! if wizard_config.persist_model_per_page?
121
- # session[:progression].push(:#{id})
121
+ #{model_persist_line}
122
122
  return if callback_performs_action?(:on_#{id}_form_#{next_button})
123
123
  redirect_to :action=>:#{self.next_page(id)}
124
124
  FOUR
@@ -126,7 +126,7 @@ MACRO
126
126
 
127
127
  mb << <<-ENSURE
128
128
  ensure
129
- self.wizard_form_data = h.merge(@#{self.model}.attributes) if (@#{self.model} && !@completed)
129
+ self.wizard_form_data = h.merge(@#{self.model}.attributes) if (@#{self.model} && !@wizard_completed_flag)
130
130
  end
131
131
  end
132
132
  ENSURE
@@ -142,25 +142,26 @@ ENSURE
142
142
  protected
143
143
  def _on_wizard_#{finish}
144
144
  @#{self.model}.save_without_validation!
145
- @completed = true
145
+ @wizard_completed_flag = true
146
146
  reset_wizard_form_data
147
147
  _wizard_final_redirect_to(:completed)
148
148
  end
149
149
  def _on_wizard_#{skip}
150
+ session[:progression] = (session[:progression]||[]) - [@step]
150
151
  redirect_to(:action=>wizard_config.next_page(@step)) unless self.performed?
151
152
  end
152
- def _on_wizard_#{back}
153
- # TODO: fix progression management
154
- # redirect_to(:action=>((session[:progression]||[]).pop || :#{self.page_order.first})) unless self.performed?
155
- redirect_to(:action=>wizard_config.previous_page(@step)) unless self.performed?
153
+ def _on_wizard_#{back}
154
+ redirect_to(:action=>(previous_in_progression_from(@step) || :#{self.page_order.first})) unless self.performed?
156
155
  end
157
156
  def _on_wizard_#{cancel}
158
157
  _wizard_final_redirect_to(:canceled)
159
158
  end
160
- def _wizard_final_redirect_to(which_redirect)
161
- initial_referer = reset_wizard_session_vars
159
+ def _wizard_final_redirect_to(type)
160
+ initial_referer = (type == :canceled && wizard_config.form_data_keep_in_session?) ?
161
+ session[:initial_referer] :
162
+ reset_wizard_session_vars
162
163
  unless self.performed?
163
- redir = (which_redirect == :completed ? wizard_config.completed_redirect : wizard_config.canceled_redirect) || initial_referer
164
+ redir = (type == :canceled ? wizard_config.canceled_redirect : wizard_config.completed_redirect) || initial_referer
164
165
  return redirect_to(redir) if redir
165
166
  raise Wizardly::RedirectNotDefinedError, "No redirect was defined for completion or canceling the wizard. Use :completed and :canceled options to define redirects.", caller
166
167
  end
@@ -173,27 +174,73 @@ ENSURE
173
174
  next_id = self.button_for_function(:next).id
174
175
  finish_id = self.button_for_function(:finish).id
175
176
  first_page = self.page_order.first
176
- guard_line = @guard_entry ? '' : 'return #guard entry disabled'
177
- <<-HELPERS
177
+ guard_line = self.guard? ? '' : 'return check_progression #guard entry disabled'
178
+ mb = StringIO.new
179
+ mb << <<-PROGRESSION
178
180
  protected
179
- def guard_entry
181
+ def previous_in_progression_from(step)
182
+ po = wizard_config.page_order
183
+ p = session[:progression]||[]
184
+ p -= po[po.index(step)..-1]
185
+ session[:progression] = p
186
+ p.last
187
+ end
188
+ def check_progression
189
+ p = session[:progression]||[]
190
+ a = params[:action].to_sym
191
+ return if p.last == a
192
+ po = wizard_config.page_order
193
+ return unless (ai = po.index(a))
194
+ p -= po[ai..-1]
195
+ p << a
196
+ session[:progression] = p
197
+ end
198
+ PROGRESSION
199
+ if self.form_data_keep_in_session?
200
+ mb << <<-SESSION
201
+ # for :form_data=>:session
202
+ def guard_entry
180
203
  if (r = request.env['HTTP_REFERER'])
181
204
  h = ::ActionController::Routing::Routes.recognize_path(URI.parse(r).path)
182
- return if (h[:controller]||'') == '#{self.controller_name}'
183
- session[:initial_referer] = h
184
- else
185
- session[:initial_referer] = nil
205
+ return check_progression if (h[:controller]||'') == '#{self.controller_name}'
206
+ session[:initial_referer] ||= h
186
207
  end
187
- if wizard_config.form_data_keep_in_session?
188
- return if self.wizard_form_data # if it has an id we've started a wizard
208
+ # coming from outside the controller
209
+ #{guard_line}
210
+ if (params[:action] == '#{first_page}' || params[:action] == 'index')
211
+ return check_progression
212
+ elsif self.wizard_form_data
213
+ p = session[:progression]||[]
214
+ return check_progression if p.include?(params[:action].to_sym)
215
+ return redirect_to(:action=>(p.last||:#{first_page}))
216
+ end
217
+ redirect_to :action=>:#{first_page}
218
+ end
219
+ hide_action :guard_entry
220
+
221
+ SESSION
222
+ else
223
+ mb << <<-SANDBOX
224
+ # for :form_data=>:sandbox
225
+ def guard_entry
226
+ if (r = request.env['HTTP_REFERER'])
227
+ h = ::ActionController::Routing::Routes.recognize_path(URI.parse(r).path)
228
+ return check_progression if (h[:controller]||'') == '#{self.controller_name}'
229
+ session[:initial_referer] = h
189
230
  else
190
- reset_wizard_form_data
231
+ session[:initial_referer] = nil
191
232
  end
233
+ # coming from outside the controller
234
+ reset_wizard_form_data
192
235
  #{guard_line}
193
- redirect_to :action=>:#{first_page} unless (params[:action] || '') == '#{first_page}'
236
+ return redirect_to(:action=>:#{first_page}) unless (params[:action] || '') == '#{first_page}'
237
+ check_progression
194
238
  end
195
239
  hide_action :guard_entry
196
240
 
241
+ SANDBOX
242
+ end
243
+ mb << <<-HELPERS
197
244
  def save_wizard_model!
198
245
  @#{self.model}.save_without_validation!
199
246
  if wizard_config.form_data_keep_in_session?
@@ -238,11 +285,24 @@ ENSURE
238
285
  def performed?; super; end
239
286
  hide_action :performed?
240
287
 
288
+ def underscore_button_name(value)
289
+ value.to_s.strip.squeeze(' ').gsub(/ /, '_').downcase
290
+ end
291
+ hide_action :underscore_button_name
292
+
293
+ def reset_wizard_session_vars
294
+ session[:progression] = nil
295
+ init = session[:initial_referer]
296
+ session[:initial_referer] = nil
297
+ init
298
+ end
299
+ hide_action :reset_wizard_session_vars
300
+
241
301
  def check_action_for_button
242
302
  button_id = nil
243
303
  #check if params[:commit] has returned a button from submit_tag
244
304
  unless (params[:commit] == nil)
245
- button_name = methodize_button_name(params[:commit])
305
+ button_name = underscore_button_name(params[:commit])
246
306
  unless [:#{next_id}, :#{finish_id}].include?(button_id = button_name.to_sym)
247
307
  action_method_name = "on_" + params[:action].to_s + "_form_" + button_name
248
308
  callback_performs_action?(action_method_name)
@@ -259,11 +319,6 @@ ENSURE
259
319
  end
260
320
  hide_action :check_action_for_button
261
321
 
262
- @wizard_callbacks ||= {}
263
- class << self
264
- attr_reader :wizard_callbacks
265
- end
266
-
267
322
  def callback_performs_action?(methId, arg=nil)
268
323
  return false unless self.methods.include?(methId.to_s)
269
324
  #self.__send__(methId)
@@ -273,6 +328,7 @@ ENSURE
273
328
  hide_action :callback_performs_action?
274
329
 
275
330
  HELPERS
331
+ mb.string
276
332
  end
277
333
  end
278
334
  end
@@ -13,19 +13,6 @@ class <%= controller_class_name %> < ApplicationController
13
13
 
14
14
  <%= callback_macro_methods %>
15
15
 
16
- private
17
- def methodize_button_name(value)
18
- value.to_s.strip.squeeze(' ').gsub(/ /, '_').downcase
19
- end
20
-
21
- def reset_wizard_session_vars
22
- session[:progression] = nil
23
- init = session[:initial_referer]
24
- session[:initial_referer] = nil
25
- init
26
- end
27
- hide_action :methodize_button_name, :reset_wizard_session_vars
28
-
29
16
  public
30
17
  def wizard_config; self.class.wizard_config; end
31
18
  hide_action :wizard_config
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jeffp-wizardly
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Patmon
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-08-12 00:00:00 -07:00
12
+ date: 2009-08-14 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15