From 1066131143982f88a2aa746b630030e26cdc245f Mon Sep 17 00:00:00 2001 From: Pete Matsyburka Date: Mon, 13 Nov 2023 01:46:28 +0200 Subject: [PATCH] use hash for field options --- app/controllers/api/templates_controller.rb | 2 +- app/javascript/submission_form/form.vue | 22 ++++---- .../submission_form/multi_select_step.vue | 14 ++--- app/javascript/template_builder/area.vue | 3 +- app/javascript/template_builder/builder.vue | 4 +- app/javascript/template_builder/field.vue | 16 ++++-- app/javascript/template_builder/fields.vue | 2 +- .../20231112224432_update_field_options.rb | 51 +++++++++++++++++++ db/schema.rb | 2 +- 9 files changed, 88 insertions(+), 28 deletions(-) create mode 100644 db/migrate/20231112224432_update_field_options.rb diff --git a/app/controllers/api/templates_controller.rb b/app/controllers/api/templates_controller.rb index 8bf8a1a22..43d5f99c4 100644 --- a/app/controllers/api/templates_controller.rb +++ b/app/controllers/api/templates_controller.rb @@ -57,7 +57,7 @@ def template_params schema: [%i[attachment_uuid name]], submitters: [%i[name uuid]], fields: [[:uuid, :submitter_uuid, :name, :type, :required, :readonly, :default_value, - { options: [], areas: [%i[x y w h cell_w attachment_uuid page]] }]] + { options: [%i[value uuid]], areas: [%i[x y w h cell_w attachment_uuid option_uuid page]] }]] ) end end diff --git a/app/javascript/submission_form/form.vue b/app/javascript/submission_form/form.vue index 86eb7f097..e1321b82a 100644 --- a/app/javascript/submission_form/form.vue +++ b/app/javascript/submission_form/form.vue @@ -99,12 +99,12 @@ {{ t('select_your_option') }} @@ -119,24 +119,24 @@
diff --git a/app/javascript/submission_form/multi_select_step.vue b/app/javascript/submission_form/multi_select_step.vue index ad52f4c37..d1db49f79 100644 --- a/app/javascript/submission_form/multi_select_step.vue +++ b/app/javascript/submission_form/multi_select_step.vue @@ -7,25 +7,25 @@
diff --git a/app/javascript/template_builder/area.vue b/app/javascript/template_builder/area.vue index d323d6645..c5a1cfc45 100644 --- a/app/javascript/template_builder/area.vue +++ b/app/javascript/template_builder/area.vue @@ -140,6 +140,7 @@ import FieldSubmitter from './field_submitter' import FieldType from './field_type' import Field from './field' import { IconX } from '@tabler/icons-vue' +import { v4 } from 'uuid' export default { name: 'FieldArea', @@ -303,7 +304,7 @@ export default { } if (['select', 'multiple', 'radio'].includes(this.field.type)) { - this.field.options ||= [''] + this.field.options ||= [{ value: '', uuid: v4() }] } (this.field.areas || []).forEach((area) => { diff --git a/app/javascript/template_builder/builder.vue b/app/javascript/template_builder/builder.vue index 809f6fe1c..248e3122a 100644 --- a/app/javascript/template_builder/builder.vue +++ b/app/javascript/template_builder/builder.vue @@ -429,7 +429,7 @@ export default { } if (['select', 'multiple', 'radio'].includes(type)) { - field.options = [''] + field.options = [{ value: '', uuid: v4() }] } this.drawField = field @@ -592,7 +592,7 @@ export default { } if (['select', 'multiple', 'radio'].includes(field.type)) { - field.options = [''] + field.options = [{ value: '', uuid: v4() }] } const fieldArea = { diff --git a/app/javascript/template_builder/field.vue b/app/javascript/template_builder/field.vue index eb99836a9..3fa0abf7a 100644 --- a/app/javascript/template_builder/field.vue +++ b/app/javascript/template_builder/field.vue @@ -194,17 +194,19 @@
{{ index + 1 }}. + Add option @@ -233,6 +235,7 @@ import Contenteditable from './contenteditable' import FieldType from './field_type' import { IconShape, IconNewSection, IconTrashX, IconCopy, IconSettings } from '@tabler/icons-vue' +import { v4 } from 'uuid' export default { name: 'TemplateField', @@ -309,6 +312,11 @@ export default { closeDropdown () { document.activeElement.blur() }, + addOption () { + this.field.options.push({ value: '', uuid: v4() }) + + this.save() + }, maybeUpdateOptions () { delete this.field.default_value @@ -317,7 +325,7 @@ export default { } if (['radio', 'multiple', 'select'].includes(this.field.type)) { - this.field.options ||= [''] + this.field.options ||= [{ value: '', uuid: v4() }] } (this.field.areas || []).forEach((area) => { diff --git a/app/javascript/template_builder/fields.vue b/app/javascript/template_builder/fields.vue index 81ede9c41..793120d7e 100644 --- a/app/javascript/template_builder/fields.vue +++ b/app/javascript/template_builder/fields.vue @@ -252,7 +252,7 @@ export default { } if (['select', 'multiple', 'radio'].includes(type)) { - field.options = [''] + field.options = [{ value: '', uuid: v4() }] } this.fields.push(field) diff --git a/db/migrate/20231112224432_update_field_options.rb b/db/migrate/20231112224432_update_field_options.rb new file mode 100644 index 000000000..b72db381e --- /dev/null +++ b/db/migrate/20231112224432_update_field_options.rb @@ -0,0 +1,51 @@ +# frozen_string_literal: true + +class UpdateFieldOptions < ActiveRecord::Migration[7.0] + class MigrationTemplate < ApplicationRecord + self.table_name = 'templates' + end + + class MigrationSubmission < ApplicationRecord + self.table_name = 'submissions' + end + + # rubocop:disable Metrics + def up + MigrationTemplate.find_each do |template| + next if template.fields.blank? + + template_fields = JSON.parse(template.fields) + + new_fields = template_fields.deep_dup + + new_fields.each do |field| + if field['options'].present? && !field['options'].first.is_a?(Hash) + field['options'] = field['options'].map { |o| { value: o || '', uuid: SecureRandom.uuid } } + end + end + + template.update_columns(fields: new_fields.to_json) if template_fields != new_fields + end + + MigrationSubmission.find_each do |submission| + next if submission.template_fields.blank? + + template_fields = JSON.parse(submission.template_fields) + + new_fields = template_fields.deep_dup + + new_fields.each do |field| + if field['options'].present? && !field['options'].first.is_a?(Hash) + field['options'] = field['options'].map { |o| { value: o || '', uuid: SecureRandom.uuid } } + end + end + + submission.update_columns(template_fields: new_fields.to_json) if template_fields != new_fields + end + end + # rubocop:enable Metrics + + def down + nil + end +end diff --git a/db/schema.rb b/db/schema.rb index 512178c1f..ce043569b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2023_11_02_171817) do +ActiveRecord::Schema[7.0].define(version: 2023_11_12_224432) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql"