From 0ea0524dcfb39fabf7a413e81390d2d474ac379b Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Mon, 14 Jan 2019 13:53:53 +1100 Subject: [PATCH] add reset flag --- .../wizard/controllers/custom.js.es6 | 3 +++ assets/javascripts/wizard/models/custom.js.es6 | 7 +++++-- assets/javascripts/wizard/routes/custom.js.es6 | 4 +++- controllers/wizard.rb | 5 ++++- lib/builder.rb | 17 +++++++++++++---- lib/wizard.rb | 13 ++++++++++++- 6 files changed, 40 insertions(+), 9 deletions(-) create mode 100644 assets/javascripts/wizard/controllers/custom.js.es6 diff --git a/assets/javascripts/wizard/controllers/custom.js.es6 b/assets/javascripts/wizard/controllers/custom.js.es6 new file mode 100644 index 00000000..9e89a0b0 --- /dev/null +++ b/assets/javascripts/wizard/controllers/custom.js.es6 @@ -0,0 +1,3 @@ +export default Ember.Controller.extend({ + queryParams: ['reset'] +}) diff --git a/assets/javascripts/wizard/models/custom.js.es6 b/assets/javascripts/wizard/models/custom.js.es6 index 61979962..90bd8985 100644 --- a/assets/javascripts/wizard/models/custom.js.es6 +++ b/assets/javascripts/wizard/models/custom.js.es6 @@ -31,8 +31,11 @@ CustomWizard.reopenClass({ } }); -export function findCustomWizard(wizardId) { - return ajax({ url: `/w/${wizardId}`, cache: false, dataType: 'json' }).then(result => { +export function findCustomWizard(wizardId, opts = {}) { + let url = `/w/${wizardId}`; + if (opts.reset) url += '?reset=true'; + + return ajax({ url, cache: false, dataType: 'json' }).then(result => { const wizard = result.wizard; if (!wizard) return null; diff --git a/assets/javascripts/wizard/routes/custom.js.es6 b/assets/javascripts/wizard/routes/custom.js.es6 index 6fe86a21..c8439d7d 100644 --- a/assets/javascripts/wizard/routes/custom.js.es6 +++ b/assets/javascripts/wizard/routes/custom.js.es6 @@ -5,7 +5,9 @@ import { ajax } from 'wizard/lib/ajax'; export default Ember.Route.extend({ model(params) { - return findCustomWizard(params.wizard_id); + let opts = {}; + if (params.reset == 'true') opts['reset'] = true; + return findCustomWizard(params.wizard_id, opts); }, afterModel() { diff --git a/controllers/wizard.rb b/controllers/wizard.rb index f6261f41..3235209c 100644 --- a/controllers/wizard.rb +++ b/controllers/wizard.rb @@ -23,8 +23,11 @@ class CustomWizard::WizardController < ::ApplicationController format.json do builder = CustomWizard::Builder.new(current_user, params[:wizard_id].underscore) + builder_opts = {} + builder_opts[:reset] = params[:reset] if params[:reset] + if builder.wizard.present? - wizard = builder.build + wizard = builder.build(builder_opts) render_serialized(wizard, WizardSerializer) else render json: { error: I18n.t('wizard.none') } diff --git a/lib/builder.rb b/lib/builder.rb index b158f288..ce51edce 100644 --- a/lib/builder.rb +++ b/lib/builder.rb @@ -52,8 +52,11 @@ class CustomWizard::Builder result.gsub!(/w\{(.*?)\}/) { |match| data[$1.to_sym] } end - def build + def build(build_opts = {}) unless (@wizard.completed? && !@wizard.multiple_submissions && !@wizard.user.admin) || !@steps || !@wizard.permitted? + + reset_submissions if build_opts[:reset] + @steps.each do |step_template| @wizard.append_step(step_template['id']) do |step| step.title = step_template['title'] if step_template['title'] @@ -63,7 +66,7 @@ class CustomWizard::Builder if step_template['fields'] && step_template['fields'].length step_template['fields'].each do |field_template| - append_field(step, step_template, field_template) + append_field(step, step_template, field_template, build_opts) end end @@ -126,7 +129,7 @@ class CustomWizard::Builder @wizard end - def append_field(step, step_template, field_template) + def append_field(step, step_template, field_template, build_opts) params = { id: field_template['id'], type: field_template['type'], @@ -139,7 +142,7 @@ class CustomWizard::Builder params[:key] = field_template['key'] if field_template['key'] ## Load previously submitted values - if @submissions.last && !@submissions.last.key?("submitted_at") + if !build_opts[:reset] && @submissions.last && !@submissions.last.key?("submitted_at") submission = @submissions.last params[:value] = submission[field_template['id']] if submission[field_template['id']] end @@ -401,4 +404,10 @@ class CustomWizard::Builder PluginStore.set("#{@wizard.id}_submissions", @wizard.user.id, @submissions) end end + + def reset_submissions + @submissions.pop(1) if @wizard.unfinished? + PluginStore.set("#{@wizard.id}_submissions", @wizard.user.id, @submissions) + @wizard.reset + end end diff --git a/lib/wizard.rb b/lib/wizard.rb index 948d2af2..26b0d45c 100644 --- a/lib/wizard.rb +++ b/lib/wizard.rb @@ -82,7 +82,9 @@ class CustomWizard::Wizard context: @id, ).distinct.order('updated_at DESC').first - if most_recent + if most_recent.subject == "reset" + false + elsif most_recent last_finished_step = most_recent.subject last_step = CustomWizard::Wizard.step_ids(@id).last last_finished_step != last_step @@ -113,6 +115,15 @@ class CustomWizard::Wizard user.staff? || user.trust_level.to_i >= min_trust.to_i end + def reset + ::UserHistory.create( + action: ::UserHistory.actions[:custom_wizard_step], + acting_user_id: @user.id, + context: @id, + subject: "reset" + ) + end + def self.after_signup rows = PluginStoreRow.where(plugin_name: 'custom_wizard') wizards = [*rows].select { |r| r.value['after_signup'] }