diff --git a/.github/workflows/plugin-tests.yml b/.github/workflows/plugin-tests.yml index ef042fbc..782ebc4f 100644 --- a/.github/workflows/plugin-tests.yml +++ b/.github/workflows/plugin-tests.yml @@ -49,10 +49,17 @@ jobs: --health-retries 5 steps: + - uses: haya14busa/action-cond@v1 + id: discourse_branch + with: + cond: ${{ github.base_ref == 'stable' }} + if_true: "stable" + if_false: "tests-passed" + - uses: actions/checkout@v2 with: repository: discourse/discourse - ref: "${{ (github.base_ref || github.ref) }}" + ref: ${{ steps.discourse_branch.outputs.value }} fetch-depth: 1 - name: Fetch Repo Name @@ -63,6 +70,7 @@ jobs: uses: actions/checkout@v2 with: path: plugins/${{ steps.repo-name.outputs.value }} + ref: "${{ github.base_ref }}" fetch-depth: 1 - name: Check spec existence @@ -133,7 +141,13 @@ jobs: - name: Plugin RSpec with Coverage if: matrix.build_type == 'backend' && steps.check_spec.outputs.files_exists == 'true' - run: SIMPLECOV=1 bin/rake plugin:spec[${{ steps.repo-name.outputs.value }}] + run: | + if [ -e plugins/${{ steps.repo-name.outputs.value }}/.simplecov ] + then + cp plugins/${{ steps.repo-name.outputs.value }}/.simplecov .simplecov + export COVERAGE=1 + fi + bin/rake plugin:spec[${{ steps.repo-name.outputs.value }}] - name: Plugin QUnit if: matrix.build_type == 'frontend' && steps.check_qunit.outputs.files_exists == 'true' diff --git a/.simplecov b/.simplecov new file mode 100644 index 00000000..c7b6143b --- /dev/null +++ b/.simplecov @@ -0,0 +1,7 @@ +# frozen_string_literal: true +plugin = "discourse-custom-wizard" + +SimpleCov.configure do + track_files "plugins/#{plugin}/**/*.rb" + add_filter { |src| !(src.filename =~ /(\/#{plugin}\/app\/|\/#{plugin}\/lib\/)/) } +end diff --git a/controllers/custom_wizard/admin/admin.rb b/app/controllers/custom_wizard/admin/admin.rb similarity index 100% rename from controllers/custom_wizard/admin/admin.rb rename to app/controllers/custom_wizard/admin/admin.rb diff --git a/controllers/custom_wizard/admin/api.rb b/app/controllers/custom_wizard/admin/api.rb similarity index 100% rename from controllers/custom_wizard/admin/api.rb rename to app/controllers/custom_wizard/admin/api.rb diff --git a/controllers/custom_wizard/admin/custom_fields.rb b/app/controllers/custom_wizard/admin/custom_fields.rb similarity index 100% rename from controllers/custom_wizard/admin/custom_fields.rb rename to app/controllers/custom_wizard/admin/custom_fields.rb diff --git a/controllers/custom_wizard/admin/logs.rb b/app/controllers/custom_wizard/admin/logs.rb similarity index 100% rename from controllers/custom_wizard/admin/logs.rb rename to app/controllers/custom_wizard/admin/logs.rb diff --git a/controllers/custom_wizard/admin/manager.rb b/app/controllers/custom_wizard/admin/manager.rb similarity index 100% rename from controllers/custom_wizard/admin/manager.rb rename to app/controllers/custom_wizard/admin/manager.rb diff --git a/controllers/custom_wizard/admin/notice.rb b/app/controllers/custom_wizard/admin/notice.rb similarity index 100% rename from controllers/custom_wizard/admin/notice.rb rename to app/controllers/custom_wizard/admin/notice.rb diff --git a/controllers/custom_wizard/admin/submissions.rb b/app/controllers/custom_wizard/admin/submissions.rb similarity index 100% rename from controllers/custom_wizard/admin/submissions.rb rename to app/controllers/custom_wizard/admin/submissions.rb diff --git a/controllers/custom_wizard/admin/subscription.rb b/app/controllers/custom_wizard/admin/subscription.rb similarity index 100% rename from controllers/custom_wizard/admin/subscription.rb rename to app/controllers/custom_wizard/admin/subscription.rb diff --git a/controllers/custom_wizard/admin/wizard.rb b/app/controllers/custom_wizard/admin/wizard.rb similarity index 96% rename from controllers/custom_wizard/admin/wizard.rb rename to app/controllers/custom_wizard/admin/wizard.rb index fe0b8b02..ad63209a 100644 --- a/controllers/custom_wizard/admin/wizard.rb +++ b/app/controllers/custom_wizard/admin/wizard.rb @@ -39,7 +39,7 @@ class CustomWizard::AdminWizardController < CustomWizard::AdminController wizard_id = template.save(create: params[:create]) if template.errors.any? - render json: failed_json.merge(errors: template.errors.full_messages) + render json: failed_json.merge(backend_validation_error: template.errors.full_messages.join("\n\n")) else render json: success_json.merge(wizard_id: wizard_id) end @@ -119,6 +119,7 @@ class CustomWizard::AdminWizardController < CustomWizard::AdminController condition: mapped_params, index: mapped_params, validations: {}, + tag_groups: [], ] ], actions: [ diff --git a/controllers/custom_wizard/realtime_validations.rb b/app/controllers/custom_wizard/realtime_validations.rb similarity index 100% rename from controllers/custom_wizard/realtime_validations.rb rename to app/controllers/custom_wizard/realtime_validations.rb diff --git a/controllers/custom_wizard/steps.rb b/app/controllers/custom_wizard/steps.rb similarity index 98% rename from controllers/custom_wizard/steps.rb rename to app/controllers/custom_wizard/steps.rb index 66ec2da9..df3c2cb3 100644 --- a/controllers/custom_wizard/steps.rb +++ b/app/controllers/custom_wizard/steps.rb @@ -54,7 +54,7 @@ class CustomWizard::StepsController < ::ApplicationController updater.result[:redirect_on_complete] = redirect end - @wizard.final_cleanup! + @wizard.cleanup_on_complete! result[:final] = true else diff --git a/controllers/custom_wizard/wizard.rb b/app/controllers/custom_wizard/wizard.rb similarity index 92% rename from controllers/custom_wizard/wizard.rb rename to app/controllers/custom_wizard/wizard.rb index d877f035..a7988b0d 100644 --- a/controllers/custom_wizard/wizard.rb +++ b/app/controllers/custom_wizard/wizard.rb @@ -6,7 +6,7 @@ class CustomWizard::WizardController < ::ActionController::Base include CanonicalURL::ControllerExtensions include GlobalPath - prepend_view_path(Rails.root.join('plugins', 'discourse-custom-wizard', 'views')) + prepend_view_path(Rails.root.join('plugins', 'discourse-custom-wizard', 'app', 'views')) layout :set_wizard_layout before_action :preload_wizard_json @@ -48,14 +48,11 @@ class CustomWizard::WizardController < ::ActionController::Base result = success_json if current_user && wizard.can_access? - submission = wizard.current_submission - - if submission.present? && submission.redirect_to - result.merge!(redirect_to: submission.redirect_to) + if redirect_to = wizard.current_submission&.redirect_to + result.merge!(redirect_to: redirect_to) end - submission.remove if submission.present? - wizard.reset + wizard.cleanup_on_skip! end render json: result diff --git a/jobs/regular/refresh_api_access_token.rb b/app/jobs/regular/refresh_api_access_token.rb similarity index 100% rename from jobs/regular/refresh_api_access_token.rb rename to app/jobs/regular/refresh_api_access_token.rb diff --git a/jobs/regular/set_after_time_wizard.rb b/app/jobs/regular/set_after_time_wizard.rb similarity index 91% rename from jobs/regular/set_after_time_wizard.rb rename to app/jobs/regular/set_after_time_wizard.rb index 7a5b86c6..a8935c8a 100644 --- a/jobs/regular/set_after_time_wizard.rb +++ b/app/jobs/regular/set_after_time_wizard.rb @@ -14,6 +14,8 @@ module Jobs end end + CustomWizard::Template.clear_cache_keys + MessageBus.publish "/redirect_to_wizard", wizard.id, user_ids: user_ids end end diff --git a/jobs/scheduled/custom_wizard/update_notices.rb b/app/jobs/scheduled/custom_wizard/update_notices.rb similarity index 100% rename from jobs/scheduled/custom_wizard/update_notices.rb rename to app/jobs/scheduled/custom_wizard/update_notices.rb diff --git a/jobs/scheduled/custom_wizard/update_subscription.rb b/app/jobs/scheduled/custom_wizard/update_subscription.rb similarity index 100% rename from jobs/scheduled/custom_wizard/update_subscription.rb rename to app/jobs/scheduled/custom_wizard/update_subscription.rb diff --git a/serializers/custom_wizard/api/authorization_serializer.rb b/app/serializers/custom_wizard/api/authorization_serializer.rb similarity index 100% rename from serializers/custom_wizard/api/authorization_serializer.rb rename to app/serializers/custom_wizard/api/authorization_serializer.rb diff --git a/serializers/custom_wizard/api/basic_endpoint_serializer.rb b/app/serializers/custom_wizard/api/basic_endpoint_serializer.rb similarity index 100% rename from serializers/custom_wizard/api/basic_endpoint_serializer.rb rename to app/serializers/custom_wizard/api/basic_endpoint_serializer.rb diff --git a/serializers/custom_wizard/api/endpoint_serializer.rb b/app/serializers/custom_wizard/api/endpoint_serializer.rb similarity index 100% rename from serializers/custom_wizard/api/endpoint_serializer.rb rename to app/serializers/custom_wizard/api/endpoint_serializer.rb diff --git a/serializers/custom_wizard/api/log_serializer.rb b/app/serializers/custom_wizard/api/log_serializer.rb similarity index 100% rename from serializers/custom_wizard/api/log_serializer.rb rename to app/serializers/custom_wizard/api/log_serializer.rb diff --git a/serializers/custom_wizard/api_serializer.rb b/app/serializers/custom_wizard/api_serializer.rb similarity index 100% rename from serializers/custom_wizard/api_serializer.rb rename to app/serializers/custom_wizard/api_serializer.rb diff --git a/serializers/custom_wizard/basic_api_serializer.rb b/app/serializers/custom_wizard/basic_api_serializer.rb similarity index 100% rename from serializers/custom_wizard/basic_api_serializer.rb rename to app/serializers/custom_wizard/basic_api_serializer.rb diff --git a/serializers/custom_wizard/basic_wizard_serializer.rb b/app/serializers/custom_wizard/basic_wizard_serializer.rb similarity index 100% rename from serializers/custom_wizard/basic_wizard_serializer.rb rename to app/serializers/custom_wizard/basic_wizard_serializer.rb diff --git a/serializers/custom_wizard/custom_field_serializer.rb b/app/serializers/custom_wizard/custom_field_serializer.rb similarity index 100% rename from serializers/custom_wizard/custom_field_serializer.rb rename to app/serializers/custom_wizard/custom_field_serializer.rb diff --git a/serializers/custom_wizard/log_serializer.rb b/app/serializers/custom_wizard/log_serializer.rb similarity index 100% rename from serializers/custom_wizard/log_serializer.rb rename to app/serializers/custom_wizard/log_serializer.rb diff --git a/serializers/custom_wizard/notice_serializer.rb b/app/serializers/custom_wizard/notice_serializer.rb similarity index 100% rename from serializers/custom_wizard/notice_serializer.rb rename to app/serializers/custom_wizard/notice_serializer.rb diff --git a/serializers/custom_wizard/realtime_validation/similar_topics_serializer.rb b/app/serializers/custom_wizard/realtime_validation/similar_topics_serializer.rb similarity index 100% rename from serializers/custom_wizard/realtime_validation/similar_topics_serializer.rb rename to app/serializers/custom_wizard/realtime_validation/similar_topics_serializer.rb diff --git a/app/serializers/custom_wizard/submission_serializer.rb b/app/serializers/custom_wizard/submission_serializer.rb new file mode 100644 index 00000000..9c2f8133 --- /dev/null +++ b/app/serializers/custom_wizard/submission_serializer.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true +class CustomWizard::SubmissionSerializer < ApplicationSerializer + attributes :id, + :fields, + :submitted_at + + has_one :user, serializer: ::BasicUserSerializer, embed: :objects + + def include_user? + object.user.present? + end + + def fields + @fields ||= begin + result = {} + + object.wizard.template['steps'].each do |step| + step['fields'].each do |field| + if value = object.fields[field['id']] + result[field['id']] = { + value: value, + type: field['type'], + label: field['label'] + } + end + end + end + + result + end + end +end diff --git a/serializers/custom_wizard/subscription/authentication_serializer.rb b/app/serializers/custom_wizard/subscription/authentication_serializer.rb similarity index 100% rename from serializers/custom_wizard/subscription/authentication_serializer.rb rename to app/serializers/custom_wizard/subscription/authentication_serializer.rb diff --git a/serializers/custom_wizard/subscription/subscription_serializer.rb b/app/serializers/custom_wizard/subscription/subscription_serializer.rb similarity index 100% rename from serializers/custom_wizard/subscription/subscription_serializer.rb rename to app/serializers/custom_wizard/subscription/subscription_serializer.rb diff --git a/serializers/custom_wizard/subscription_serializer.rb b/app/serializers/custom_wizard/subscription_serializer.rb similarity index 100% rename from serializers/custom_wizard/subscription_serializer.rb rename to app/serializers/custom_wizard/subscription_serializer.rb diff --git a/serializers/custom_wizard/wizard_field_serializer.rb b/app/serializers/custom_wizard/wizard_field_serializer.rb similarity index 96% rename from serializers/custom_wizard/wizard_field_serializer.rb rename to app/serializers/custom_wizard/wizard_field_serializer.rb index bba2b41a..d5f57060 100644 --- a/serializers/custom_wizard/wizard_field_serializer.rb +++ b/app/serializers/custom_wizard/wizard_field_serializer.rb @@ -16,6 +16,7 @@ class CustomWizard::FieldSerializer < ::ApplicationSerializer :limit, :property, :content, + :tag_groups, :validations, :max_length, :char_counter, @@ -93,6 +94,10 @@ class CustomWizard::FieldSerializer < ::ApplicationSerializer object.content end + def tag_groups + object.tag_groups + end + def validations validations = {} object.validations&.each do |type, props| diff --git a/serializers/custom_wizard/wizard_serializer.rb b/app/serializers/custom_wizard/wizard_serializer.rb similarity index 100% rename from serializers/custom_wizard/wizard_serializer.rb rename to app/serializers/custom_wizard/wizard_serializer.rb diff --git a/serializers/custom_wizard/wizard_step_serializer.rb b/app/serializers/custom_wizard/wizard_step_serializer.rb similarity index 100% rename from serializers/custom_wizard/wizard_step_serializer.rb rename to app/serializers/custom_wizard/wizard_step_serializer.rb diff --git a/views/layouts/qunit.html.erb b/app/views/layouts/qunit.html.erb similarity index 100% rename from views/layouts/qunit.html.erb rename to app/views/layouts/qunit.html.erb diff --git a/views/layouts/wizard.html.erb b/app/views/layouts/wizard.html.erb similarity index 100% rename from views/layouts/wizard.html.erb rename to app/views/layouts/wizard.html.erb diff --git a/assets/javascripts/discourse/components/wizard-custom-field.js.es6 b/assets/javascripts/discourse/components/wizard-custom-field.js.es6 index 37266a6b..edde41bf 100644 --- a/assets/javascripts/discourse/components/wizard-custom-field.js.es6 +++ b/assets/javascripts/discourse/components/wizard-custom-field.js.es6 @@ -85,8 +85,8 @@ export default Component.extend(UndoChanges, { if (this.isDropdown) { options.wizardFieldSelection = "key,value"; options.userFieldOptionsSelection = "output"; - options.textSelection = "key,value,output"; - options.inputTypes = "conditional,association,assignment"; + options.textSelection = "key,value"; + options.inputTypes = "association,conditional,assignment"; options.pairConnector = "association"; options.keyPlaceholder = "admin.wizard.key"; options.valuePlaceholder = "admin.wizard.value"; diff --git a/assets/javascripts/discourse/components/wizard-links.js.es6 b/assets/javascripts/discourse/components/wizard-links.js.es6 index 50dadfe6..62c45911 100644 --- a/assets/javascripts/discourse/components/wizard-links.js.es6 +++ b/assets/javascripts/discourse/components/wizard-links.js.es6 @@ -81,7 +81,8 @@ export default Component.extend({ let index = 0; if (items.length) { - index = items.length; + let last_item = items[items.length - 1]; + index = Number(last_item.id.split("_").pop()); } params.index = index; diff --git a/assets/javascripts/discourse/controllers/admin-wizards-wizard-show.js.es6 b/assets/javascripts/discourse/controllers/admin-wizards-wizard-show.js.es6 index 6aa5dea4..e6b0ad04 100644 --- a/assets/javascripts/discourse/controllers/admin-wizards-wizard-show.js.es6 +++ b/assets/javascripts/discourse/controllers/admin-wizards-wizard-show.js.es6 @@ -58,6 +58,21 @@ export default Controller.extend({ } return wizardFieldList(steps); }, + getErrorMessage(result) { + if (result.backend_validation_error) { + return result.backend_validation_error; + } + + let errorType = "failed"; + let errorParams = {}; + + if (result.error) { + errorType = result.error.type; + errorParams = result.error.params; + } + + return I18n.t(`admin.wizard.error.${errorType}`, errorParams); + }, actions: { save() { @@ -84,18 +99,7 @@ export default Controller.extend({ } }) .catch((result) => { - let errorType = "failed"; - let errorParams = {}; - - if (result.error) { - errorType = result.error.type; - errorParams = result.error.params; - } - - this.set( - "error", - I18n.t(`admin.wizard.error.${errorType}`, errorParams) - ); + this.set("error", this.getErrorMessage(result)); later(() => this.set("error", null), 10000); }) diff --git a/assets/javascripts/discourse/models/custom-wizard.js.es6 b/assets/javascripts/discourse/models/custom-wizard.js.es6 index d0b49079..0e3f6f8d 100644 --- a/assets/javascripts/discourse/models/custom-wizard.js.es6 +++ b/assets/javascripts/discourse/models/custom-wizard.js.es6 @@ -28,7 +28,7 @@ const CustomWizard = EmberObject.extend({ contentType: "application/json", data: JSON.stringify(data), }).then((result) => { - if (result.error) { + if (result.backend_validation_error) { reject(result); } else { resolve(result); diff --git a/assets/javascripts/discourse/templates/components/wizard-custom-field.hbs b/assets/javascripts/discourse/templates/components/wizard-custom-field.hbs index 0802d772..b4f1ac3a 100644 --- a/assets/javascripts/discourse/templates/components/wizard-custom-field.hbs +++ b/assets/javascripts/discourse/templates/components/wizard-custom-field.hbs @@ -19,7 +19,7 @@
Text
") expect(json_array[0][:description]).to eq("Text field description.") expect(json_array[3][:index]).to eq(3) diff --git a/spec/serializers/custom_wizard/wizard_serializer_spec.rb b/spec/serializers/custom_wizard/wizard_serializer_spec.rb index fe36d5a2..030f109e 100644 --- a/spec/serializers/custom_wizard/wizard_serializer_spec.rb +++ b/spec/serializers/custom_wizard/wizard_serializer_spec.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require_relative '../../plugin_helper' - describe CustomWizard::WizardSerializer do fab!(:user) { Fabricate(:user) } fab!(:category) { Fabricate(:category) } diff --git a/spec/serializers/custom_wizard/wizard_step_serializer_spec.rb b/spec/serializers/custom_wizard/wizard_step_serializer_spec.rb index f0128765..2c28479c 100644 --- a/spec/serializers/custom_wizard/wizard_step_serializer_spec.rb +++ b/spec/serializers/custom_wizard/wizard_step_serializer_spec.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require_relative '../../plugin_helper' - describe CustomWizard::StepSerializer do fab!(:user) { Fabricate(:user) } let(:wizard_template) { get_wizard_fixture("wizard") } @@ -29,7 +27,8 @@ describe CustomWizard::StepSerializer do each_serializer: described_class, scope: Guardian.new(user) ).as_json - expect(json_array[0][:fields].length).to eq(4) + + expect(json_array[0][:fields].length).to eq(@wizard.steps[0].fields.length) end context 'with required data' do