Browse Source

Properly pass and permit submission data

Angus McLeod 7 years ago
parent
commit
48ed74c4ce
3 changed files with 32 additions and 15 deletions
  1. 5 2
      controllers/steps.rb
  2. 9 10
      lib/builder.rb
  3. 18 3
      lib/wizard.rb

+ 5 - 2
controllers/steps.rb

@@ -2,8 +2,11 @@ class CustomWizard::StepsController < ApplicationController
   before_action :ensure_logged_in
 
   def update
-    wizard = CustomWizard::Builder.new(current_user, params[:wizard_id].underscore).build
-    updater = wizard.create_updater(params[:step_id], params[:fields])
+    field_ids = CustomWizard::Wizard.field_ids(params[:wizard_id], params[:step_id])
+    permitted = params.permit(:step_id, :wizard_id, fields: field_ids.map(&:to_sym)) if field_ids.present?
+
+    wizard = CustomWizard::Builder.new(current_user, permitted[:wizard_id].underscore).build
+    updater = wizard.create_updater(permitted[:step_id], permitted[:fields])
     updater.update
 
     if updater.success?

+ 9 - 10
lib/builder.rb

@@ -100,14 +100,11 @@ class CustomWizard::Builder
 
           step.on_update do |updater|
             @updater = updater
-            submission = @submissions.last || {}
-            step_input = updater.fields || {}
             user = @wizard.user
-            final_step = updater.step.next.nil?
 
             if s['fields'] && s['fields'].length
               s['fields'].each do |f|
-                value = step_input[f['id']]
+                value = updater.fields[f['id']]
                 min_length = f['min_length']
                 if min_length && value.is_a?(String) && value.length < min_length.to_i
                   label = f['label'] || I18n.t("#{f['key']}.label")
@@ -126,13 +123,14 @@ class CustomWizard::Builder
 
             next if updater.errors.any?
 
-            if @wizard.save_submissions
-              data = submission
-            else
-              data = step_input
+            step_input = updater.fields.to_h
+            data = step_input
+            final_step = updater.step.next.nil?
 
-              # Allow redirect to be passed to wizard that doesn't save submissions.
-              data['redirect_to'] = submission['redirect_to'] if submission['redirect_to']
+            ## if the wizard has data from the previous steps make that accessible to the actions.
+            if @submissions && @submissions.last && !@submissions.last.key?("submitted_at")
+              submission = @submissions.last
+              data = submission.merge(data)
             end
 
             if s['actions'] && s['actions'].length
@@ -221,6 +219,7 @@ class CustomWizard::Builder
                   a['profile_updates'].each do |pu|
                     attributes[pu['value'].to_sym] = data[pu['key']]
                   end
+                  puts "UPDATING WITH: #{attributes}"
                   user_updater.update(attributes) if attributes.present?
                 end
               end

+ 18 - 3
lib/wizard.rb

@@ -115,9 +115,24 @@ class CustomWizard::Wizard
     end
   end
 
+  def self.steps(wizard_id)
+    wizard = PluginStore.get('custom_wizard', wizard_id)
+    wizard ? wizard['steps'] : nil
+  end
+
   def self.step_ids(wizard_id)
-    data = PluginStore.get('custom_wizard', wizard_id)
-    steps = data['steps'] || []
-    steps.map { |s| s['id'] }.flatten.uniq
+    steps = self.steps(wizard_id)
+    steps.map { |s| s['id'] }.flatten.uniq if steps
+  end
+
+  def self.field_ids(wizard_id, step_id)
+    steps = self.steps(wizard_id)
+    return nil if !steps
+    step = steps.select { |s| s['id'] === step_id }.first
+    if step && fields = step['fields']
+      fields.map { |f| f['id'] }
+    else
+      nil
+    end
   end
 end