diff --git a/lib/custom_wizard/builder.rb b/lib/custom_wizard/builder.rb index abdc4cd1..a9157c2b 100644 --- a/lib/custom_wizard/builder.rb +++ b/lib/custom_wizard/builder.rb @@ -98,9 +98,10 @@ class CustomWizard::Builder if permitted_params = step_template['permitted_params'] permitted_data = {} - permitted_params.each do |param| - key = param['key'].to_sym - permitted_data[key] = params[key] if params[key] + permitted_params.each do |p| + params_key = p['key'].to_sym + submission_key = p['value'].to_sym + permitted_data[submission_key] = params[params_key] if params[params_key] end if permitted_data.present? @@ -215,7 +216,7 @@ class CustomWizard::Builder submission = @submissions.last params[:value] = submission[field_template['id']] if submission[field_template['id']] end - + ## If a field updates a profile field, load the current value if step_template['actions'] && step_template['actions'].any? profile_actions = step_template['actions'].select { |a| a['type'] === 'update_profile' } @@ -223,7 +224,7 @@ class CustomWizard::Builder if profile_actions.any? profile_actions.each do |action| if update = action['profile_updates'].select { |u| u['key'] === field_template['id'] }.first - params[:value] = prefill_profile_field(update) + params[:value] = prefill_profile_field(update) || params[:value] end end end diff --git a/lib/custom_wizard/wizard.rb b/lib/custom_wizard/wizard.rb index b8d0f7af..26f1c79a 100644 --- a/lib/custom_wizard/wizard.rb +++ b/lib/custom_wizard/wizard.rb @@ -192,8 +192,8 @@ class CustomWizard::Wizard end end - def self.add_wizard(json) - wizard = ::JSON.parse(json) + def self.add_wizard(obj) + wizard = obj.is_a?(String) ? ::JSON.parse(json) : obj PluginStore.set('custom_wizard', wizard["id"], wizard) end diff --git a/serializers/site_serializer.rb b/serializers/site_serializer.rb index 21b996f6..faff37c9 100644 --- a/serializers/site_serializer.rb +++ b/serializers/site_serializer.rb @@ -1,6 +1,11 @@ ## TODO limit this to the first admin module SiteSerializerCWX - attributes :complete_custom_wizard + extend ActiveSupport::Concern + + included do + attributes :complete_custom_wizard + end + def include_wizard_required? scope.is_admin? && Wizard.new(scope.user).requires_completion? diff --git a/serializers/wizard_field_serializer.rb b/serializers/wizard_field_serializer.rb index 1749fb89..c1ef779f 100644 --- a/serializers/wizard_field_serializer.rb +++ b/serializers/wizard_field_serializer.rb @@ -1,5 +1,9 @@ module CustomWizardWizardFieldSerializerExtension - attributes :dropdown_none, :image, :file_types, :limit, :property + extend ActiveSupport::Concern + + included do + attributes :dropdown_none, :image, :file_types, :limit, :property + end def label return object.label if object.label.present? diff --git a/serializers/wizard_serializer.rb b/serializers/wizard_serializer.rb index 18659417..9cd97f26 100644 --- a/serializers/wizard_serializer.rb +++ b/serializers/wizard_serializer.rb @@ -1,14 +1,18 @@ module CustomWizardWizardSerializerExtension - attributes :id, - :name, - :background, - :completed, - :required, - :min_trust, - :permitted, - :user, - :categories, - :uncategorized_category_id + extend ActiveSupport::Concern + + included do + attributes :id, + :name, + :background, + :completed, + :required, + :min_trust, + :permitted, + :user, + :categories, + :uncategorized_category_id + end def id object.id diff --git a/serializers/wizard_step_serializer.rb b/serializers/wizard_step_serializer.rb index 0ee28202..e3774069 100644 --- a/serializers/wizard_step_serializer.rb +++ b/serializers/wizard_step_serializer.rb @@ -1,5 +1,9 @@ module CustomWizardWizardStepSerializerExtension - attributes :permitted, :permitted_message + extend ActiveSupport::Concern + + included do + attributes :permitted, :permitted_message + end def title return PrettyText.cook(object.title) if object.title diff --git a/spec/components/custom_wizard/builder_spec.rb b/spec/components/custom_wizard/builder_spec.rb index 5d0a8c73..b5be4ca4 100644 --- a/spec/components/custom_wizard/builder_spec.rb +++ b/spec/components/custom_wizard/builder_spec.rb @@ -3,18 +3,30 @@ require 'rails_helper' describe CustomWizard::Builder do - fab!(:user) { Fabricate(:user) } + fab!(:user) { Fabricate(:user) } + fab!(:trusted_user) { Fabricate(:user, trust_level: 3)} - before do - @template = File.open( + let!(:template) do + JSON.parse(File.open( "#{Rails.root}/plugins/discourse-custom-wizard/spec/fixtures/wizard.json" - ).read + ).read) + end + + def build_wizard(t = template, u = user, build_opts = {}, params = {}) + CustomWizard::Wizard.add_wizard(t) + CustomWizard::Builder.new(u, 'welcome').build(build_opts, params) + end + + def add_submission_data + PluginStore.set("welcome_submissions", user.id, + name: 'Angus', + website: 'https://thepavilion.io' + ) end it "returns no steps when disabled" do - CustomWizard::Wizard.add_wizard(@template) SiteSetting.custom_wizard_enabled = false - wizard = CustomWizard::Builder.new(user, 'welcome').build + wizard = build_wizard expect(wizard.steps.length).to eq(0) expect(wizard.name).to eq('Welcome') end @@ -24,38 +36,63 @@ describe CustomWizard::Builder do SiteSetting.custom_wizard_enabled = true end - it "returns steps when enabled" do - CustomWizard::Wizard.add_wizard(@template) - wizard = CustomWizard::Builder.new(user, 'welcome').build - expect(wizard.steps.length).to eq(2) + it "returns steps" do + expect(build_wizard.steps.length).to eq(2) end - it 'returns no steps if the multiple submissions are disabled and user has completed' do - @template['multiple_submissions'] = false - CustomWizard::Wizard.add_wizard(@template) - wizard = CustomWizard::Builder.new(user, 'welcome').build - expect(wizard.steps.length).to eq(0) + it 'returns no steps if the multiple submissions are disabled and user has completed it' do + history_params = { + action: UserHistory.actions[:custom_wizard_step], + acting_user_id: user.id, + context: template['id'] + } + UserHistory.create!(history_params.merge(subject: template['steps'][0]['id'])) + UserHistory.create!(history_params.merge(subject: template['steps'][1]['id'])) + + template["multiple_submissions"] = false + expect(build_wizard(template).steps.length).to eq(0) end - it 'returns nothing if the user is not permitted to see it' do - + it 'returns no steps if has min trust and user does not meet it' do + template["min_trust"] = 3 + expect(build_wizard(template).steps.length).to eq(0) end - it 'returns a wizard with prefilled data if user has partially completed' do + it 'returns steps if it has min trust and user meets it' do + template["min_trust"] = 3 + expect(build_wizard(template, trusted_user).steps.length).to eq(2) + end + it 'returns a wizard with prefilled data if user has partially completed it' do + add_submission_data + wizard = build_wizard + expect(wizard.steps[0].fields.first.value).to eq('Angus') + expect(wizard.steps[1].fields.first.value).to eq('https://thepavilion.io') end it 'returns a wizard with no prefilled data if options include reset' do - + add_submission_data + wizard = build_wizard(template, user, reset: true) + expect(wizard.steps[0].fields.first.value).to eq(nil) + expect(wizard.steps[1].fields.first.value).to eq(nil) end context 'building steps' do - it 'returns step data correctly' do - + it 'returns step metadata' do + expect(build_wizard.steps[0].title).to eq('Welcome to Pavilion') + expect(build_wizard.steps[1].title).to eq('Tell us about you') end it 'saves permitted params' do - + template['steps'][0]['permitted_params'] = [ + { + "key": "param_key", + "value": "submission_param_key" + } + ] + wizard = build_wizard(template, user, {}, param_key: 'param_value') + submissions = PluginStore.get("welcome_submissions", user.id) + expect(submissions.first['submission_param_key']).to eq('param_value') end it 'ensures required data is present' do diff --git a/spec/fixtures/wizard.json b/spec/fixtures/wizard.json index e384427b..e657cb3c 100644 --- a/spec/fixtures/wizard.json +++ b/spec/fixtures/wizard.json @@ -8,24 +8,27 @@ "theme_id": 4, "steps": [ { - "id": "step_1", + "id": "welcome", "title": "Welcome to Pavilion", "raw_description": "Hey there, thanks for signing up.\n\nWe're Pavilion, an international freelancer cooperative that specialises in online communities.\n\nThis site is our own community, where we work with our clients, users of our open source work and our broader community.\n\n", - "description": "

Hey there, thanks for signing up.

\n

We’re Pavilion, an international freelancer cooperative that specialises in online communities.

\n

This site is our own community, where we work with our clients, users of our open source work and our broader community.

" - }, - { - "id": "step_2", - "title": "Tell us about you", - "raw_description": "We'd like to know a little more about you. Add a your name and your website below. This will update your user profile.", + "description": "

Hey there, thanks for signing up.

\n

We’re Pavilion, an international freelancer cooperative that specialises in online communities.

\n

This site is our own community, where we work with our clients, users of our open source work and our broader community.

", "fields": [ { "id": "name", "type": "text", "label": "Name", "required": true - }, + } + ] + }, + { + "id": "about_you", + "title": "Tell us about you", + "raw_description": "We'd like to know a little more about you. Add a your name and your website below. This will update your user profile.", + "description": "

We’d like to know a little more about you. Add a your name and your website below. This will update your user profile.

", + "fields": [ { - "id": "field_3", + "id": "website", "label": "Website", "type": "text" } @@ -40,13 +43,12 @@ "value": "name" }, { - "key": "field_3", + "key": "website", "value": "website" } ] } - ], - "description": "

We’d like to know a little more about you. Add a your name and your website below. This will update your user profile.

" + ] } ] } \ No newline at end of file