From 4f7d18d193cca9a8d29f4663ca59e7199d1b52cc Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Tue, 8 Dec 2020 12:44:37 +0530 Subject: [PATCH 1/7] FEATURE: added max length setting for text type fields --- .../components/wizard-custom-field.js.es6 | 2 +- .../discourse/models/custom-wizard.js.es6 | 2 +- .../components/wizard-custom-field.hbs | 16 ++++++++++- config/locales/client.en.yml | 2 ++ config/locales/server.en.yml | 1 + controllers/custom_wizard/admin/wizard.rb | 1 + extensions/wizard_field.rb | 2 ++ lib/custom_wizard/builder.rb | 1 + lib/custom_wizard/field.rb | 5 +++- lib/custom_wizard/validators/update.rb | 5 ++++ .../custom_wizard/update_validator_spec.rb | 27 ++++++++++++++++++- 11 files changed, 59 insertions(+), 5 deletions(-) diff --git a/assets/javascripts/discourse/components/wizard-custom-field.js.es6 b/assets/javascripts/discourse/components/wizard-custom-field.js.es6 index 7615fe07..b17464d4 100644 --- a/assets/javascripts/discourse/components/wizard-custom-field.js.es6 +++ b/assets/javascripts/discourse/components/wizard-custom-field.js.es6 @@ -21,7 +21,7 @@ export default Component.extend(UndoChanges, { showPrefill: or('isText', 'isCategory', 'isTag', 'isGroup', 'isDropdown'), showContent: or('isCategory', 'isTag', 'isGroup', 'isDropdown'), showLimit: or('isCategory', 'isTag'), - showMinLength: or('isText', 'isTextarea', 'isComposer'), + showLengthControls: or('isText', 'isTextarea', 'isComposer'), categoryPropertyTypes: selectKitContent(['id', 'slug']), showAdvanced: alias('field.type'), messageUrl: 'https://thepavilion.io/t/2809', diff --git a/assets/javascripts/discourse/models/custom-wizard.js.es6 b/assets/javascripts/discourse/models/custom-wizard.js.es6 index ea555f41..4a17c04d 100644 --- a/assets/javascripts/discourse/models/custom-wizard.js.es6 +++ b/assets/javascripts/discourse/models/custom-wizard.js.es6 @@ -8,7 +8,7 @@ import { popupAjaxError } from 'discourse/lib/ajax-error'; const CustomWizard = EmberObject.extend({ save(opts) { - return new Promise((resolve, reject) => { + return new Promise((resolve, reject) => { let wizard = this.buildJson(this, 'wizard'); if (wizard.error) { diff --git a/assets/javascripts/discourse/templates/components/wizard-custom-field.hbs b/assets/javascripts/discourse/templates/components/wizard-custom-field.hbs index 8e673247..d55a1a8b 100644 --- a/assets/javascripts/discourse/templates/components/wizard-custom-field.hbs +++ b/assets/javascripts/discourse/templates/components/wizard-custom-field.hbs @@ -70,7 +70,7 @@ url=messageUrl component='field'}} -{{#if showMinLength}} +{{#if showLengthControls}}
@@ -84,6 +84,20 @@ class="small"}}
+ +
+
+ +
+ +
+ {{input + type="number" + name="max_length" + value=field.max_length + class="small"}} +
+
{{/if}} {{#if isUpload}} diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 5bd88f24..be138d48 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -164,6 +164,8 @@ en: required_label: "Field is Required" min_length: "Min Length" min_length_placeholder: "Minimum length in characters" + max_length: "Max Length" + max_length_placeholder: "Maximum length in characters" file_types: "File Types" limit: "Limit" property: "Property" diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index d6c136ff..4bda825a 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -20,6 +20,7 @@ en: field: too_short: "%{label} must be at least %{min} characters" + too_long: "%{label} must not be more than %{max} characters" required: "%{label} is required." not_url: "%{label} must be a valid url" invalid_file: "%{label} must be a %{types}" diff --git a/controllers/custom_wizard/admin/wizard.rb b/controllers/custom_wizard/admin/wizard.rb index 9a0dc4cf..a9a01035 100644 --- a/controllers/custom_wizard/admin/wizard.rb +++ b/controllers/custom_wizard/admin/wizard.rb @@ -97,6 +97,7 @@ class CustomWizard::AdminWizardController < CustomWizard::AdminController :key, :type, :min_length, + :max_length, :file_types, :format, :limit, diff --git a/extensions/wizard_field.rb b/extensions/wizard_field.rb index 2301c6d3..9fd8d6d1 100644 --- a/extensions/wizard_field.rb +++ b/extensions/wizard_field.rb @@ -5,6 +5,7 @@ module CustomWizardFieldExtension :image, :key, :min_length, + :max_length, :file_types, :format, :limit, @@ -18,6 +19,7 @@ module CustomWizardFieldExtension @image = attrs[:image] @key = attrs[:key] @min_length = attrs[:min_length] + @max_length = attrs[:max_length] @file_types = attrs[:file_types] @format = attrs[:format] @limit = attrs[:limit] diff --git a/lib/custom_wizard/builder.rb b/lib/custom_wizard/builder.rb index e530e5e9..d7d5a517 100644 --- a/lib/custom_wizard/builder.rb +++ b/lib/custom_wizard/builder.rb @@ -158,6 +158,7 @@ class CustomWizard::Builder params[:image] = field_template['image'] if field_template['image'] params[:key] = field_template['key'] if field_template['key'] params[:min_length] = field_template['min_length'] if field_template['min_length'] + params[:max_length] = field_template['max_length'] if field_template['max_length'] params[:value] = prefill_field(field_template, step_template) if !build_opts[:reset] && (submission = @wizard.current_submission) diff --git a/lib/custom_wizard/field.rb b/lib/custom_wizard/field.rb index cbb08e9c..6e051285 100644 --- a/lib/custom_wizard/field.rb +++ b/lib/custom_wizard/field.rb @@ -3,14 +3,17 @@ class CustomWizard::Field @types ||= { text: { min_length: nil, + max_length:nil, prefill: nil }, textarea: { min_length: nil, + max_length:nil, prefill: nil }, composer: { - min_length: nil + min_length: nil, + max_length:nil, }, text_only: {}, date: { diff --git a/lib/custom_wizard/validators/update.rb b/lib/custom_wizard/validators/update.rb index 0229f676..0db83c01 100644 --- a/lib/custom_wizard/validators/update.rb +++ b/lib/custom_wizard/validators/update.rb @@ -21,6 +21,7 @@ class ::CustomWizard::UpdateValidator type = field.type required = field.required min_length = field.min_length if is_text_type(field) + max_length = field.max_length if is_text_type(field) file_types = field.file_types format = field.format @@ -32,6 +33,10 @@ class ::CustomWizard::UpdateValidator @updater.errors.add(field_id, I18n.t('wizard.field.too_short', label: label, min: min_length.to_i)) end + if max_length && value.is_a?(String) && value.strip.length > max_length.to_i + @updater.errors.add(field_id, I18n.t('wizard.field.too_long', label: label, max: max_length.to_i)) + end + if is_url_type(field) && !check_if_url(value) @updater.errors.add(field_id, I18n.t('wizard.field.not_url', label: label)) end diff --git a/spec/components/custom_wizard/update_validator_spec.rb b/spec/components/custom_wizard/update_validator_spec.rb index 3bceb939..8f89b623 100644 --- a/spec/components/custom_wizard/update_validator_spec.rb +++ b/spec/components/custom_wizard/update_validator_spec.rb @@ -49,7 +49,32 @@ describe CustomWizard::UpdateValidator do updater.errors.messages[:step_1_field_3].first ).to eq(I18n.t('wizard.field.too_short', label: 'Composer', min: min_length)) end - + + it 'applies max length to text type fields' do + max_length = 100 + + @template[:steps][0][:fields][0][:max_length] = max_length + @template[:steps][0][:fields][1][:max_length] = max_length + @template[:steps][0][:fields][2][:max_length] = max_length + + CustomWizard::Template.save(@template) + long_string = "Our Competitive Capability solution offers platforms a suite of wholesale offerings. In the future, will you be able to effectively revolutionize synergies in your business? In the emerging market space, industry is ethically investing its mission critical executive searches. Key players will take ownership of their capabilities by iteratively right-sizing world-class visibilities. " + updater = perform_validation('step_1', step_1_field_1: long_string) + expect( + updater.errors.messages[:step_1_field_1].first + ).to eq(I18n.t('wizard.field.too_long', label: 'Text', max: max_length)) + + updater = perform_validation('step_1', step_1_field_2: long_string) + expect( + updater.errors.messages[:step_1_field_2].first + ).to eq(I18n.t('wizard.field.too_long', label: 'Textarea', max: max_length)) + + updater = perform_validation('step_1', step_1_field_3: long_string) + expect( + updater.errors.messages[:step_1_field_3].first + ).to eq(I18n.t('wizard.field.too_long', label: 'Composer', max: max_length)) + end + it 'standardises boolean entries' do updater = perform_validation('step_2', step_2_field_5: 'false') expect(updater.submission['step_2_field_5']).to eq(false) From 3c3e67684f30fb768ccd280ec956c54ac94c1359 Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Tue, 8 Dec 2020 12:49:08 +0530 Subject: [PATCH 2/7] fixed html code formatting --- .../discourse/templates/components/wizard-custom-field.hbs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assets/javascripts/discourse/templates/components/wizard-custom-field.hbs b/assets/javascripts/discourse/templates/components/wizard-custom-field.hbs index d55a1a8b..2792fdc5 100644 --- a/assets/javascripts/discourse/templates/components/wizard-custom-field.hbs +++ b/assets/javascripts/discourse/templates/components/wizard-custom-field.hbs @@ -85,7 +85,7 @@ -
+
From 49f82b4014ba914385ab150c3c16da66c168e858 Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Tue, 8 Dec 2020 13:19:31 +0530 Subject: [PATCH 3/7] fixed formatting --- lib/custom_wizard/field.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/custom_wizard/field.rb b/lib/custom_wizard/field.rb index 6e051285..53a8b5da 100644 --- a/lib/custom_wizard/field.rb +++ b/lib/custom_wizard/field.rb @@ -3,17 +3,17 @@ class CustomWizard::Field @types ||= { text: { min_length: nil, - max_length:nil, + max_length: nil, prefill: nil }, textarea: { min_length: nil, - max_length:nil, + max_length: nil, prefill: nil }, composer: { min_length: nil, - max_length:nil, + max_length: nil, }, text_only: {}, date: { From e12faad1f5dfa69b3e8067609523e7da7deffe9a Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Tue, 8 Dec 2020 13:29:36 +0530 Subject: [PATCH 4/7] added positive test for max length check --- .../custom_wizard/update_validator_spec.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/spec/components/custom_wizard/update_validator_spec.rb b/spec/components/custom_wizard/update_validator_spec.rb index 8f89b623..c0e55db3 100644 --- a/spec/components/custom_wizard/update_validator_spec.rb +++ b/spec/components/custom_wizard/update_validator_spec.rb @@ -73,6 +73,23 @@ describe CustomWizard::UpdateValidator do expect( updater.errors.messages[:step_1_field_3].first ).to eq(I18n.t('wizard.field.too_long', label: 'Composer', max: max_length)) + + hundred_chars_string = "This is a line, exactly hundred characters long and not more even a single character more than that." + updater = perform_validation('step_1', step_1_field_1: hundred_chars_string) + expect( + updater.errors.messages[:step_1_field_1].first + ).to eq(nil) + + updater = perform_validation('step_1', step_1_field_2: hundred_chars_string) + expect( + updater.errors.messages[:step_1_field_2].first + ).to eq(nil) + + updater = perform_validation('step_1', step_1_field_3: hundred_chars_string) + expect( + updater.errors.messages[:step_1_field_3].first + ).to eq(nil) + end it 'standardises boolean entries' do From 9c6f6d0db25b85757f0f306528f60789ac04d44b Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Tue, 8 Dec 2020 13:41:45 +0530 Subject: [PATCH 5/7] Reverted a formatting change --- assets/javascripts/discourse/models/custom-wizard.js.es6 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assets/javascripts/discourse/models/custom-wizard.js.es6 b/assets/javascripts/discourse/models/custom-wizard.js.es6 index 4a17c04d..ea555f41 100644 --- a/assets/javascripts/discourse/models/custom-wizard.js.es6 +++ b/assets/javascripts/discourse/models/custom-wizard.js.es6 @@ -8,7 +8,7 @@ import { popupAjaxError } from 'discourse/lib/ajax-error'; const CustomWizard = EmberObject.extend({ save(opts) { - return new Promise((resolve, reject) => { + return new Promise((resolve, reject) => { let wizard = this.buildJson(this, 'wizard'); if (wizard.error) { From efaa0c5915e88553a19b302b46c44f82f92102ab Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Mon, 14 Dec 2020 12:16:32 +0530 Subject: [PATCH 6/7] FIX: broke down the max length test into different cases --- .../custom_wizard/update_validator_spec.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/spec/components/custom_wizard/update_validator_spec.rb b/spec/components/custom_wizard/update_validator_spec.rb index c0e55db3..996c1548 100644 --- a/spec/components/custom_wizard/update_validator_spec.rb +++ b/spec/components/custom_wizard/update_validator_spec.rb @@ -50,7 +50,7 @@ describe CustomWizard::UpdateValidator do ).to eq(I18n.t('wizard.field.too_short', label: 'Composer', min: min_length)) end - it 'applies max length to text type fields' do + it 'prevents submission if the length is over the max length' do max_length = 100 @template[:steps][0][:fields][0][:max_length] = max_length @@ -73,7 +73,16 @@ describe CustomWizard::UpdateValidator do expect( updater.errors.messages[:step_1_field_3].first ).to eq(I18n.t('wizard.field.too_long', label: 'Composer', max: max_length)) + end + it "allows submission if the length is under or equal to the max length" do + max_length = 100 + + @template[:steps][0][:fields][0][:max_length] = max_length + @template[:steps][0][:fields][1][:max_length] = max_length + @template[:steps][0][:fields][2][:max_length] = max_length + + CustomWizard::Template.save(@template) hundred_chars_string = "This is a line, exactly hundred characters long and not more even a single character more than that." updater = perform_validation('step_1', step_1_field_1: hundred_chars_string) expect( @@ -89,7 +98,6 @@ describe CustomWizard::UpdateValidator do expect( updater.errors.messages[:step_1_field_3].first ).to eq(nil) - end it 'standardises boolean entries' do From 4aa040474f3530ad7aca71a731187b9ec1d001b2 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Mon, 14 Dec 2020 21:40:14 +1100 Subject: [PATCH 7/7] Update coverage --- coverage/.last_run.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/coverage/.last_run.json b/coverage/.last_run.json index 375f0421..6e843e2c 100644 --- a/coverage/.last_run.json +++ b/coverage/.last_run.json @@ -1,5 +1,5 @@ { "result": { - "covered_percent": 89.41 + "covered_percent": 89.45 } }