From 0b9abf84ccf951ef9bf3e0443f9f6fd0dbf27c9f Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Wed, 11 Sep 2019 19:53:51 +1000 Subject: [PATCH 1/9] Add support for date variable in field interpolation --- lib/builder.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/builder.rb b/lib/builder.rb index 018fd7ff..f6d426b4 100644 --- a/lib/builder.rb +++ b/lib/builder.rb @@ -53,7 +53,21 @@ class CustomWizard::Builder result end - result.gsub(/w\{(.*?)\}/) { |match| recurse(data, [*$1.split('.')]) } + result = result.gsub(/w\{(.*?)\}/) { |match| recurse(data, [*$1.split('.')]) } + + result.gsub(/v\{(.*?)\}/) do |match| + attrs = $1.split(':') + key = attrs.first + format = attrs.length > 1 ? attrs.last : nil + v = nil + + if key == 'time' + time_format = format.present? ? format : "%B %-d, %Y" + v = Time.now.strftime(time_format) + end + + v + end end def self.recurse(data, keys) From 26ce6a313b6b34ca41e1a24e7134bc6ba6d68fb6 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Wed, 11 Sep 2019 21:30:59 +1000 Subject: [PATCH 2/9] boolean cast bugfix --- lib/builder.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/builder.rb b/lib/builder.rb index f6d426b4..b0f46947 100644 --- a/lib/builder.rb +++ b/lib/builder.rb @@ -341,7 +341,7 @@ class CustomWizard::Builder end def standardise_boolean(value) - !!HasCustomFields::Helpers::CUSTOM_FIELD_TRUE.include?(value) + ActiveRecord::Type::Boolean.new.cast(value) end def create_topic(user, action, data) From 9a3506a9d69d60ec6828b8d319777a7ed81f1b3d Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Tue, 24 Sep 2019 15:52:09 +0800 Subject: [PATCH 3/9] FIX: required data check --- lib/builder.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/builder.rb b/lib/builder.rb index b0f46947..b6778fe6 100644 --- a/lib/builder.rb +++ b/lib/builder.rb @@ -104,7 +104,7 @@ class CustomWizard::Builder end if required_data = step_template['required_data'] - if !@submissions.last && required_data.length + if !@submissions.last && required_data.present? step.permitted = false next end From e328331249fa6dc592a7667130777cf1ea24135a Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Wed, 2 Oct 2019 14:24:02 +1000 Subject: [PATCH 4/9] FIX: action category selection --- lib/builder.rb | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/lib/builder.rb b/lib/builder.rb index b6778fe6..8cb86ba7 100644 --- a/lib/builder.rb +++ b/lib/builder.rb @@ -364,22 +364,7 @@ class CustomWizard::Builder skip_validations: true } - if action['custom_category_enabled'] - if action['custom_category_wizard_field'] - category_id = data[action['category_id']] - elsif action['custom_category_user_field_key'] - if action['custom_category_user_field_key'].include?('custom_fields') - field = action['custom_category_user_field_key'].split('.').last - category_id = user.custom_fields[field] - else - category_id = user.send(action['custom_category_user_field_key']) - end - end - else - category_id = action['category_id'] - end - - params[:category] = category_id + params[:category] = action_category_id(action, data) topic_custom_fields = {} @@ -544,8 +529,8 @@ class CustomWizard::Builder url += "&body=#{post}" - if action['category_id'] - if category = Category.find(action['category_id']) + if category_id = action_category_id(action, data) + if category = Category.find(category_id) url += "&category=#{category.full_slug('/')}" end end @@ -591,4 +576,21 @@ class CustomWizard::Builder PluginStore.set("#{@wizard.id}_submissions", @wizard.user.id, @submissions) @wizard.reset end + + def action_category_id(action, data) + if action['custom_category_enabled'] + if action['custom_category_wizard_field'] + category_id = data[action['category_id']] + elsif action['custom_category_user_field_key'] + if action['custom_category_user_field_key'].include?('custom_fields') + field = action['custom_category_user_field_key'].split('.').last + user.custom_fields[field] + else + user.send(action['custom_category_user_field_key']) + end + end + else + action['category_id'] + end + end end From 82d98892a35b018854d0dadb51e57e9b44de0571 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Thu, 3 Oct 2019 12:17:33 +1000 Subject: [PATCH 5/9] FIX: allow action tags and topic field tag mapping to work together --- lib/builder.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/builder.rb b/lib/builder.rb index 8cb86ba7..1d3a80b2 100644 --- a/lib/builder.rb +++ b/lib/builder.rb @@ -366,6 +366,9 @@ class CustomWizard::Builder params[:category] = action_category_id(action, data) + tags = action['tags'] || [] + params[:tags] = tags + topic_custom_fields = {} if action['add_fields'] @@ -389,7 +392,7 @@ class CustomWizard::Builder end end else - value = [*value] if key === 'tags' + value = [*value] + tags if key === 'tags' params[key.to_sym] = value end end From 954dea0af97a5a3c973ebeb888eae00060777984 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Thu, 3 Oct 2019 15:17:21 +1000 Subject: [PATCH 6/9] zeitwork compatibility --- controllers/steps.rb | 2 +- jobs/clear_after_time_wizard.rb | 2 +- jobs/refresh_api_access_token.rb | 2 +- jobs/set_after_time_wizard.rb | 2 +- lib/wizard.rb | 5 ----- lib/wizard_edits.rb | 4 ---- plugin.rb | 3 --- serializers/api/api_serializer.rb | 2 +- serializers/api/authorization_serializer.rb | 2 +- serializers/api/basic_api_serializer.rb | 2 +- serializers/api/basic_endpoint_serializer.rb | 2 +- serializers/api/endpoint_serializer.rb | 2 +- serializers/api/log_serializer.rb | 2 +- 13 files changed, 10 insertions(+), 22 deletions(-) diff --git a/controllers/steps.rb b/controllers/steps.rb index c66c7bfe..a627b960 100644 --- a/controllers/steps.rb +++ b/controllers/steps.rb @@ -1,4 +1,4 @@ -class CustomWizard::StepsController < ApplicationController +class CustomWizard::StepsController < ::ApplicationController before_action :ensure_logged_in def update diff --git a/jobs/clear_after_time_wizard.rb b/jobs/clear_after_time_wizard.rb index 4b53e5b9..ffaa6fa9 100644 --- a/jobs/clear_after_time_wizard.rb +++ b/jobs/clear_after_time_wizard.rb @@ -1,5 +1,5 @@ module Jobs - class ClearAfterTimeWizard < Jobs::Base + class ClearAfterTimeWizard < ::Jobs::Base sidekiq_options queue: 'critical' def execute(args) diff --git a/jobs/refresh_api_access_token.rb b/jobs/refresh_api_access_token.rb index 54f6da55..114c39d1 100644 --- a/jobs/refresh_api_access_token.rb +++ b/jobs/refresh_api_access_token.rb @@ -1,5 +1,5 @@ module Jobs - class RefreshApiAccessToken < Jobs::Base + class RefreshApiAccessToken < ::Jobs::Base def execute(args) CustomWizard::Api::Authorization.get_token(args[:name], refresh: true) end diff --git a/jobs/set_after_time_wizard.rb b/jobs/set_after_time_wizard.rb index e3ec5ee3..76d408d4 100644 --- a/jobs/set_after_time_wizard.rb +++ b/jobs/set_after_time_wizard.rb @@ -1,5 +1,5 @@ module Jobs - class SetAfterTimeWizard < Jobs::Base + class SetAfterTimeWizard < ::Jobs::Base def execute(args) if CustomWizard::Wizard.find(args[:wizard_id]) user_ids = [] diff --git a/lib/wizard.rb b/lib/wizard.rb index b47d68f1..879d6f2b 100644 --- a/lib/wizard.rb +++ b/lib/wizard.rb @@ -1,8 +1,3 @@ -require_dependency 'wizard/step' -require_dependency 'wizard/field' -require_dependency 'wizard/step_updater' -require_dependency 'wizard/builder' - class CustomWizard::Wizard attr_reader :steps, :user diff --git a/lib/wizard_edits.rb b/lib/wizard_edits.rb index 11aa7ed8..e1ff2dc4 100644 --- a/lib/wizard_edits.rb +++ b/lib/wizard_edits.rb @@ -1,7 +1,3 @@ -require_dependency 'wizard' -require_dependency 'wizard/field' -require_dependency 'wizard/step' - ::Wizard.class_eval do def self.user_requires_completion?(user) wizard_result = self.new(user).requires_completion? diff --git a/plugin.rb b/plugin.rb index 957682ef..222aac34 100644 --- a/plugin.rb +++ b/plugin.rb @@ -36,7 +36,6 @@ end after_initialize do UserHistory.actions[:custom_wizard_step] = 1000 - require_dependency 'application_controller' module ::CustomWizard class Engine < ::Rails::Engine engine_name 'custom_wizard' @@ -52,7 +51,6 @@ after_initialize do put ':wizard_id/steps/:step_id' => 'steps#update' end - require_dependency 'admin_constraint' Discourse::Application.routes.append do mount ::CustomWizard::Engine, at: 'w' post 'wizard/authorization/callback' => "custom_wizard/authorization#callback" @@ -139,7 +137,6 @@ after_initialize do end end - require_dependency 'invites_controller' class ::InvitesController prepend InvitesControllerCustomWizard end diff --git a/serializers/api/api_serializer.rb b/serializers/api/api_serializer.rb index 9d7fba70..1fa601a3 100644 --- a/serializers/api/api_serializer.rb +++ b/serializers/api/api_serializer.rb @@ -1,4 +1,4 @@ -class CustomWizard::ApiSerializer < ApplicationSerializer +class CustomWizard::ApiSerializer < ::ApplicationSerializer attributes :name, :title, :authorization, diff --git a/serializers/api/authorization_serializer.rb b/serializers/api/authorization_serializer.rb index 2ca347b5..fe6e7444 100644 --- a/serializers/api/authorization_serializer.rb +++ b/serializers/api/authorization_serializer.rb @@ -1,4 +1,4 @@ -class CustomWizard::Api::AuthorizationSerializer < ApplicationSerializer +class CustomWizard::Api::AuthorizationSerializer < ::ApplicationSerializer attributes :auth_type, :auth_url, :token_url, diff --git a/serializers/api/basic_api_serializer.rb b/serializers/api/basic_api_serializer.rb index d0214d65..5b037650 100644 --- a/serializers/api/basic_api_serializer.rb +++ b/serializers/api/basic_api_serializer.rb @@ -1,4 +1,4 @@ -class CustomWizard::BasicApiSerializer < ApplicationSerializer +class CustomWizard::BasicApiSerializer < ::ApplicationSerializer attributes :name, :title, :endpoints diff --git a/serializers/api/basic_endpoint_serializer.rb b/serializers/api/basic_endpoint_serializer.rb index e2cc2262..63758c48 100644 --- a/serializers/api/basic_endpoint_serializer.rb +++ b/serializers/api/basic_endpoint_serializer.rb @@ -1,4 +1,4 @@ -class CustomWizard::Api::BasicEndpointSerializer < ApplicationSerializer +class CustomWizard::Api::BasicEndpointSerializer < ::ApplicationSerializer attributes :id, :name end diff --git a/serializers/api/endpoint_serializer.rb b/serializers/api/endpoint_serializer.rb index 31d0f975..0430bea3 100644 --- a/serializers/api/endpoint_serializer.rb +++ b/serializers/api/endpoint_serializer.rb @@ -1,4 +1,4 @@ -class CustomWizard::Api::EndpointSerializer < ApplicationSerializer +class CustomWizard::Api::EndpointSerializer < ::ApplicationSerializer attributes :id, :name, :method, diff --git a/serializers/api/log_serializer.rb b/serializers/api/log_serializer.rb index 13ee1555..819185b5 100644 --- a/serializers/api/log_serializer.rb +++ b/serializers/api/log_serializer.rb @@ -1,4 +1,4 @@ -class CustomWizard::Api::LogSerializer < ApplicationSerializer +class CustomWizard::Api::LogSerializer < ::ApplicationSerializer attributes :log_id, :time, :status, From 514a03c3af31ea81e29592b5562933197d983261 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Thu, 3 Oct 2019 16:54:15 +1000 Subject: [PATCH 7/9] add require_dependency back for backwards compatibility --- lib/wizard.rb | 5 +++++ lib/wizard_edits.rb | 4 ++++ plugin.rb | 7 +++++-- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/wizard.rb b/lib/wizard.rb index 879d6f2b..b47d68f1 100644 --- a/lib/wizard.rb +++ b/lib/wizard.rb @@ -1,3 +1,8 @@ +require_dependency 'wizard/step' +require_dependency 'wizard/field' +require_dependency 'wizard/step_updater' +require_dependency 'wizard/builder' + class CustomWizard::Wizard attr_reader :steps, :user diff --git a/lib/wizard_edits.rb b/lib/wizard_edits.rb index e1ff2dc4..11aa7ed8 100644 --- a/lib/wizard_edits.rb +++ b/lib/wizard_edits.rb @@ -1,3 +1,7 @@ +require_dependency 'wizard' +require_dependency 'wizard/field' +require_dependency 'wizard/step' + ::Wizard.class_eval do def self.user_requires_completion?(user) wizard_result = self.new(user).requires_completion? diff --git a/plugin.rb b/plugin.rb index 222aac34..0e182b20 100644 --- a/plugin.rb +++ b/plugin.rb @@ -51,6 +51,7 @@ after_initialize do put ':wizard_id/steps/:step_id' => 'steps#update' end + require_dependency 'admin_constraint' Discourse::Application.routes.append do mount ::CustomWizard::Engine, at: 'w' post 'wizard/authorization/callback' => "custom_wizard/authorization#callback" @@ -136,11 +137,13 @@ after_initialize do @user = user end end - + + require_dependency 'invites_controller' class ::InvitesController prepend InvitesControllerCustomWizard end - + + require_dependency 'application_controller' class ::ApplicationController before_action :redirect_to_wizard_if_required, if: :current_user From 82cb2668bc154b98f972133267299681e7ac5f6e Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Tue, 8 Oct 2019 16:46:13 +1100 Subject: [PATCH 8/9] add guest login --- assets/javascripts/wizard-custom-guest.js | 3 +++ controllers/wizard.rb | 6 ++++++ views/layouts/wizard.html.erb | 8 ++++++-- 3 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 assets/javascripts/wizard-custom-guest.js diff --git a/assets/javascripts/wizard-custom-guest.js b/assets/javascripts/wizard-custom-guest.js new file mode 100644 index 00000000..38a526d5 --- /dev/null +++ b/assets/javascripts/wizard-custom-guest.js @@ -0,0 +1,3 @@ +(function() { + window.location.href = "/login"; +})(); diff --git a/controllers/wizard.rb b/controllers/wizard.rb index e2d5656f..2d384530 100644 --- a/controllers/wizard.rb +++ b/controllers/wizard.rb @@ -4,6 +4,12 @@ class CustomWizard::WizardController < ::ApplicationController helper_method :wizard_page_title helper_method :theme_ids + + before_action :handle_login_redirect, unless: :current_user + + def handle_login_redirect + cookies[:destination_url] = "/w/#{params[:wizard_id]}" + end def wizard CustomWizard::Template.new(PluginStore.get('custom_wizard', params[:wizard_id].underscore)) diff --git a/views/layouts/wizard.html.erb b/views/layouts/wizard.html.erb index 251d13ed..db15672b 100644 --- a/views/layouts/wizard.html.erb +++ b/views/layouts/wizard.html.erb @@ -40,8 +40,12 @@ <%= raw theme_lookup("body_tag") %> <%- end %> - <%= preload_script 'wizard-custom-start' %> - <%= preload_script 'wizard-raw-templates' %> + <%- if current_user %> + <%= preload_script 'wizard-custom-start' %> + <%= preload_script 'wizard-raw-templates' %> + <%- else %> + <%= preload_script 'wizard-custom-guest' %> + <%- end %>