From 4c3e88beee905ee7b59cf97b3201f14a3833f145 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Wed, 15 Apr 2020 00:10:26 +1000 Subject: [PATCH] Added debugging --- config/locales/server.en.yml | 3 +- config/settings.yml | 2 ++ controllers/custom_wizard/steps.rb | 2 +- lib/custom_wizard/actions.rb | 56 ++++++++++++++++++++++++++---- 4 files changed, 54 insertions(+), 9 deletions(-) diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 58332e3c..8932633a 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -30,4 +30,5 @@ en: wizard_redirect_exclude_paths: "Routes excluded from wizard redirects." wizard_recognised_image_upload_formats: "File types which will result in upload displaying an image preview" wizard_step_advanced: "Enable advanced settings for wizard steps (experimental)." - wizard_api_features: "Enable API features (experimental)." \ No newline at end of file + wizard_api_features: "Enable API features (experimental)." + wizard_action_debug: "Log action details for debugging." \ No newline at end of file diff --git a/config/settings.yml b/config/settings.yml index a8764415..6b3458e0 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -20,4 +20,6 @@ plugins: default: false wizard_api_features: client: true + default: false + wizard_action_debug: default: false \ No newline at end of file diff --git a/controllers/custom_wizard/steps.rb b/controllers/custom_wizard/steps.rb index 8707f3ed..e370a1dd 100644 --- a/controllers/custom_wizard/steps.rb +++ b/controllers/custom_wizard/steps.rb @@ -24,7 +24,7 @@ class CustomWizard::StepsController < ::ApplicationController else errors = [] updater.errors.messages.each do |field, msg| - errors << { field: field, description: msg.join } + errors << { field: field, description: msg.join(',') } end render json: { errors: errors }, status: 422 end diff --git a/lib/custom_wizard/actions.rb b/lib/custom_wizard/actions.rb index ff9f35c0..3708b906 100644 --- a/lib/custom_wizard/actions.rb +++ b/lib/custom_wizard/actions.rb @@ -2,7 +2,8 @@ class CustomWizard::Action attr_accessor :data, :action, :user, - :updater + :updater, + :result def initialize(params) @action = params[:action] @@ -12,7 +13,20 @@ class CustomWizard::Action end def perform - ActiveRecord::Base.transaction { self.send(action['type'].to_sym) } + ActiveRecord::Base.transaction do + self.send(action['type'].to_sym) + + if SiteSetting.wizard_action_debug + log = "action: #{action['type']}; " + log << "result: #{@result}" + + updater.errors.messages.each do |field, msg| + log << "error: #{field.to_s}; #{msg.to_s}; " + end + + Rails.logger.warn("Wizard Action: #{log.to_s}") + end + end end def mapper @@ -21,19 +35,26 @@ class CustomWizard::Action def create_topic params = basic_topic_params - + if params[:title].present? && params[:raw].present? params[:category] = action_category params[:tags] = action_tags creator = PostCreator.new(user, params) post = creator.create - + if creator.errors.present? + @result = "failed to create" updater.errors.add(:create_topic, creator.errors.full_messages.join(" ")) elsif action['skip_redirect'].blank? data['redirect_on_complete'] = post.topic.url end + + if creator.errors.blank? + @result = "success (created topic: #{post.topic.id})" + end + else + @result = "invalid params" end end @@ -54,10 +75,17 @@ class CustomWizard::Action post = creator.create if creator.errors.present? + @result = "failed to create" updater.errors.add(:send_message, creator.errors.full_messages.join(" ")) elsif action['skip_redirect'].blank? data['redirect_on_complete'] = post.topic.url end + + if creator.errors.blank? + @result = "success (created pm: #{post.topic.id})" + end + else + @result = "invalid params" end end @@ -76,11 +104,19 @@ class CustomWizard::Action params = add_custom_fields(params) if params.present? - UserUpdater.new(Discourse.system_user, user).update(params) + result = UserUpdater.new(Discourse.system_user, user).update(params) if params[:avatar].present? - update_avatar(params[:avatar]) + result = update_avatar(params[:avatar]) end + + if result + @result = "success (updated fields #{params.keys.map{ |p| p.to_s }.join(',')})" + else + @result = "failed to update" + end + else + @result = "invalid params" end end @@ -160,9 +196,15 @@ class CustomWizard::Action if groups.present? groups.each do |group_id| group = Group.find(group_id) if group_id - group.add(user) if group + result = group.add(user) if group end end + + if result + @result = "success (added to groups: #{groups.map { |g| g.id.to_s }.join(',')})" + else + @result = "failed to add" + end end def route_to