Spiegel von
https://github.com/paviliondev/discourse-custom-wizard.git
synchronisiert 2024-11-25 18:50:27 +01:00
Properly pass and permit submission data
Dieser Commit ist enthalten in:
Ursprung
bd88f6ace6
Commit
48ed74c4ce
3 geänderte Dateien mit 32 neuen und 15 gelöschten Zeilen
|
@ -2,8 +2,11 @@ class CustomWizard::StepsController < ApplicationController
|
||||||
before_action :ensure_logged_in
|
before_action :ensure_logged_in
|
||||||
|
|
||||||
def update
|
def update
|
||||||
wizard = CustomWizard::Builder.new(current_user, params[:wizard_id].underscore).build
|
field_ids = CustomWizard::Wizard.field_ids(params[:wizard_id], params[:step_id])
|
||||||
updater = wizard.create_updater(params[:step_id], params[:fields])
|
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
|
updater.update
|
||||||
|
|
||||||
if updater.success?
|
if updater.success?
|
||||||
|
|
|
@ -100,14 +100,11 @@ class CustomWizard::Builder
|
||||||
|
|
||||||
step.on_update do |updater|
|
step.on_update do |updater|
|
||||||
@updater = updater
|
@updater = updater
|
||||||
submission = @submissions.last || {}
|
|
||||||
step_input = updater.fields || {}
|
|
||||||
user = @wizard.user
|
user = @wizard.user
|
||||||
final_step = updater.step.next.nil?
|
|
||||||
|
|
||||||
if s['fields'] && s['fields'].length
|
if s['fields'] && s['fields'].length
|
||||||
s['fields'].each do |f|
|
s['fields'].each do |f|
|
||||||
value = step_input[f['id']]
|
value = updater.fields[f['id']]
|
||||||
min_length = f['min_length']
|
min_length = f['min_length']
|
||||||
if min_length && value.is_a?(String) && value.length < min_length.to_i
|
if min_length && value.is_a?(String) && value.length < min_length.to_i
|
||||||
label = f['label'] || I18n.t("#{f['key']}.label")
|
label = f['label'] || I18n.t("#{f['key']}.label")
|
||||||
|
@ -126,13 +123,14 @@ class CustomWizard::Builder
|
||||||
|
|
||||||
next if updater.errors.any?
|
next if updater.errors.any?
|
||||||
|
|
||||||
if @wizard.save_submissions
|
step_input = updater.fields.to_h
|
||||||
data = submission
|
data = step_input
|
||||||
else
|
final_step = updater.step.next.nil?
|
||||||
data = step_input
|
|
||||||
|
|
||||||
# Allow redirect to be passed to wizard that doesn't save submissions.
|
## if the wizard has data from the previous steps make that accessible to the actions.
|
||||||
data['redirect_to'] = submission['redirect_to'] if submission['redirect_to']
|
if @submissions && @submissions.last && !@submissions.last.key?("submitted_at")
|
||||||
|
submission = @submissions.last
|
||||||
|
data = submission.merge(data)
|
||||||
end
|
end
|
||||||
|
|
||||||
if s['actions'] && s['actions'].length
|
if s['actions'] && s['actions'].length
|
||||||
|
@ -221,6 +219,7 @@ class CustomWizard::Builder
|
||||||
a['profile_updates'].each do |pu|
|
a['profile_updates'].each do |pu|
|
||||||
attributes[pu['value'].to_sym] = data[pu['key']]
|
attributes[pu['value'].to_sym] = data[pu['key']]
|
||||||
end
|
end
|
||||||
|
puts "UPDATING WITH: #{attributes}"
|
||||||
user_updater.update(attributes) if attributes.present?
|
user_updater.update(attributes) if attributes.present?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -115,9 +115,24 @@ class CustomWizard::Wizard
|
||||||
end
|
end
|
||||||
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)
|
def self.step_ids(wizard_id)
|
||||||
data = PluginStore.get('custom_wizard', wizard_id)
|
steps = self.steps(wizard_id)
|
||||||
steps = data['steps'] || []
|
steps.map { |s| s['id'] }.flatten.uniq if steps
|
||||||
steps.map { |s| s['id'] }.flatten.uniq
|
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
|
||||||
end
|
end
|
||||||
|
|
Laden …
In neuem Issue referenzieren