0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-25 18:50:27 +01:00
Dieser Commit ist enthalten in:
Angus McLeod 2019-12-09 12:43:30 +11:00
Ursprung a96a2b965c
Commit be83350b72
4 geänderte Dateien mit 81 neuen und 51 gelöschten Zeilen

Datei anzeigen

@ -8,6 +8,7 @@ en:
custom_title: "Wizard" custom_title: "Wizard"
field: field:
too_short: "%{label} must be at least %{min} characters" too_short: "%{label} must be at least %{min} characters"
required: "%{label} is required."
none: "We couldn't find a wizard at that address." none: "We couldn't find a wizard at that address."
no_skip: "Wizard can't be skipped" no_skip: "Wizard can't be skipped"
export: export:

Datei anzeigen

@ -154,7 +154,7 @@ class CustomWizard::Builder
next if updater.errors.any? next if updater.errors.any?
data = updater.fields.to_h data = updater.fields
## if the wizard has data from the previous steps make that accessible to the actions. ## if the wizard has data from the previous steps make that accessible to the actions.
if @submissions && @submissions.last && !@submissions.last.key?("submitted_at") if @submissions && @submissions.last && !@submissions.last.key?("submitted_at")
@ -324,13 +324,17 @@ class CustomWizard::Builder
def validate_field(field, updater, step_template) def validate_field(field, updater, step_template)
value = updater.fields[field['id']] value = updater.fields[field['id']]
min_length = false min_length = false
label = field['label'] || I18n.t("#{field['key']}.label")
if field['required'] && !value
updater.errors.add(field['id'].to_s, I18n.t('wizard.field.required', label: label))
end
if is_text_type(field) if is_text_type(field)
min_length = field['min_length'] min_length = field['min_length']
end end
if min_length && value.is_a?(String) && value.strip.length < min_length.to_i if min_length && value.is_a?(String) && value.strip.length < min_length.to_i
label = field['label'] || I18n.t("#{field['key']}.label")
updater.errors.add(field['id'].to_s, I18n.t('wizard.field.too_short', label: label, min: min_length.to_i)) updater.errors.add(field['id'].to_s, I18n.t('wizard.field.too_short', label: label, min: min_length.to_i))
end end

Datei anzeigen

@ -8,7 +8,7 @@ class CustomWizard::StepUpdater
@wizard = wizard @wizard = wizard
@step = step @step = step
@refresh_required = false @refresh_required = false
@fields = fields @fields = fields.to_h.with_indifferent_access
@result = {} @result = {}
end end

Datei anzeigen

@ -12,16 +12,40 @@ describe CustomWizard::Builder do
).read) ).read)
end end
let(:permitted_params) {[{"key":"param_key","value":"submission_param_key"}]}
let(:required_data) {[{"key":"nickname","connector":"equals","value":"name"}]}
let(:required_data_message) {"Nickname is required to match your name"}
let(:checkbox_field) {{"id":"checkbox","type":"checkbox","label":"Checkbox"}}
let(:composer_field) {{"id": "composer","label":"Composer","type":"composer"}}
let(:dropdown_categories_field) {{"id": "dropdown_categories","type": "dropdown","label": "Dropdown Categories","choices_type": "preset","choices_preset": "categories"}}
let(:tag_field) {{"id": "tag","type": "tag","label": "Tag","limit": "2"}}
let(:category_field) {{"id": "category","type": "category","limit": "1","label": "Category"}}
let(:image_field) {{"id": "image","type": "image","label": "Image"}}
let(:text_field) {{"id": "text","type": "text","min_length": "12","label": "Text"}}
let(:textarea_field) {{"id": "textarea","type": "textarea","min_length": "40","label": "Textarea"}}
let(:text_only_field) {{"id": "text_only","type": "text-only","label": "Text only"}}
let(:upload_field) {{"id": "upload","type": "upload","file_types": ".jpg,.png,.pdf","label": "Upload"}}
let(:user_selector_field) {{"id": "user_selector","type": "user-selector","label": "User selector"}}
let(:dropdown_groups_field) {{"id": "dropdown_groups","type": "dropdown","choices_type": "preset","choices_preset": "groups","label": "Dropdown Groups"}}
let(:dropdown_tags_field) {{"id": "dropdown_tags","type": "dropdown","choices_type": "preset","choices_preset": "tags","label": "Dropdown Tags"}}
let(:dropdown_custom_field) {{"id": "dropdown_custom","type": "dropdown","choices_type": "custom","choices": [{"key": "option_1","value": "Option 1"},{"key": "option_2","value": "Option 2"}]}}
let(:dropdown_translation_field) {{"id": "dropdown_translation","type": "dropdown","choices_type": "translation","choices_key": "key1.key2"}}
let(:dropdown_categories_filtered_field) {{"id": "dropdown_categories_filtered_field","type": "dropdown","choices_type": "preset","choices_preset": "categories","choices_filters": [{"key": "slug","value": "staff"}]}}
def build_wizard(t = template, u = user, build_opts = {}, params = {}) def build_wizard(t = template, u = user, build_opts = {}, params = {})
CustomWizard::Wizard.add_wizard(t) CustomWizard::Wizard.add_wizard(t)
CustomWizard::Builder.new(u, 'welcome').build(build_opts, params) CustomWizard::Builder.new(u, 'welcome').build(build_opts, params)
end end
def add_submission_data def add_submission_data(data = {})
PluginStore.set("welcome_submissions", user.id, PluginStore.set("welcome_submissions", user.id, {
name: 'Angus', name: 'Angus',
website: 'https://thepavilion.io' website: 'https://thepavilion.io'
) }.merge(data))
end
def get_submission_data
PluginStore.get("welcome_submissions", user.id)
end end
it "returns no steps when disabled" do it "returns no steps when disabled" do
@ -84,76 +108,77 @@ describe CustomWizard::Builder do
end end
it 'saves permitted params' do it 'saves permitted params' do
template['steps'][0]['permitted_params'] = [ template['steps'][0]['permitted_params'] = permitted_params
{
"key": "param_key",
"value": "submission_param_key"
}
]
wizard = build_wizard(template, user, {}, param_key: 'param_value') wizard = build_wizard(template, user, {}, param_key: 'param_value')
submissions = PluginStore.get("welcome_submissions", user.id) submissions = get_submission_data
expect(submissions.first['submission_param_key']).to eq('param_value') expect(submissions.first['submission_param_key']).to eq('param_value')
end end
it 'ensures required data is present' do it 'is not permitted if required data is not present' do
template['steps'][0]['required_data'] = required_data
end expect(build_wizard(template, user).steps[0].permitted).to eq(false)
end
context 'building fields' do
it 'returns field data correctly' do
end end
it 'returns checkbox fields correctly' do it "is not permitted if required data is not present" do
template['steps'][0]['required_data'] = required_data
add_submission_data(nickname: "John")
expect(build_wizard(template, user).steps[0].permitted).to eq(false)
end end
it 'returns upload fields correctly' do it 'it shows required data message if required data has message' do
template['steps'][0]['required_data'] = required_data
template['steps'][0]['required_data_message'] = required_data_message
add_submission_data(nickname: "John")
wizard = build_wizard(template, user)
expect(wizard.steps[0].permitted).to eq(false)
expect(wizard.steps[0].permitted_message).to eq(required_data_message)
end end
it 'returns category fields correctly' do it 'is permitted if required data is present' do
template['steps'][0]['required_data'] = required_data
PluginStore.set('welcome_submissions', user.id, nickname: "Angus", name: "Angus")
expect(build_wizard(template, user).steps[0].permitted).to eq(true)
end end
it 'returns tag fields correctly' do it 'returns field metadata' do
expect(build_wizard(template, user).steps[0].fields[0].label).to eq("<p>Name</p>")
expect(build_wizard(template, user).steps[0].fields[0].type).to eq("text")
end end
it 'returns custom dropdown fields correctly' do it 'returns fields' do
template['steps'][0]['fields'][1] = checkbox_field
end expect(build_wizard(template, user).steps[0].fields.length).to eq(2)
it 'returns translated dropdown fields correctly' do
end
it 'returns preset dropdown fields correctly' do
end
it 'applies preset dropdown filters correctly' do
end
it 'prefils profile data correctly' do
end end
end end
context 'on update' do context 'on update' do
context 'validation' do context 'validation' do
it 'applies min length correctly' do it 'applies min length' do
template['steps'][0]['fields'][0]['min_length'] = 10
wizard = build_wizard(template, user)
updater = wizard.create_updater(template['steps'][0]['id'], name: 'short')
updater.update
expect(updater.errors.messages[:name].first).to eq(
I18n.t('wizard.field.too_short', label: 'Name', min: 10)
)
end end
it 'standardises boolean entries' do it 'standardises boolean entries' do
template['steps'][0]['fields'][0] = checkbox_field
wizard = build_wizard(template, user)
updater = wizard.create_updater(template['steps'][0]['id'], checkbox: 'false')
updater.update
submissions = get_submission_data
expect(submissions.first['checkbox']).to eq(false)
end end
it 'requires required fields' do it 'requires required fields' do
## this may require additional work? wizard = build_wizard
updater = wizard.create_updater(template['steps'][0]['id'], other_field: 'other')
updater.update
expect(updater.errors.messages[:name].first).to eq(
I18n.t('wizard.field.required', label: 'Name')
)
end end
context 'submisisons' do context 'submisisons' do