diff --git a/app/controllers/step_elements/form_responses_controller.rb b/app/controllers/step_elements/form_responses_controller.rb index 1721e4ca4..0eb332dc9 100644 --- a/app/controllers/step_elements/form_responses_controller.rb +++ b/app/controllers/step_elements/form_responses_controller.rb @@ -12,7 +12,7 @@ module StepElements render_403 and return unless can_create_protocol_form_responses?(@step.protocol) ActiveRecord::Base.transaction do - @form_response = FormResponse.create!(form: @form, created_by: current_user) + @form_response = FormResponse.create!(form: @form, created_by: current_user, parent: @step) create_in_step!(@step, @form_response) log_step_form_activity(:form_added, { form: @form.id }) end @@ -43,6 +43,7 @@ module StepElements ActiveRecord::Base.transaction do @form_response.step_orderable_element.update!(step: target, position: target.step_orderable_elements.size) + @form_response.update!(parent: target) @step.normalize_elements_position log_step_form_activity(:form_moved, diff --git a/app/models/form_field_value.rb b/app/models/form_field_value.rb index 2d3faef34..91bed582f 100644 --- a/app/models/form_field_value.rb +++ b/app/models/form_field_value.rb @@ -23,4 +23,8 @@ class FormFieldValue < ApplicationRecord def value_in_range? true end + + def name + form_field&.name + end end diff --git a/app/models/form_response.rb b/app/models/form_response.rb index 65418f2c6..fd65fbc8c 100644 --- a/app/models/form_response.rb +++ b/app/models/form_response.rb @@ -10,6 +10,7 @@ class FormResponse < ApplicationRecord belongs_to :form belongs_to :created_by, class_name: 'User' belongs_to :submitted_by, class_name: 'User', optional: true + belongs_to :parent, polymorphic: true, inverse_of: :form_responses has_one :step_orderable_element, as: :orderable, dependent: :destroy @@ -34,10 +35,6 @@ class FormResponse < ApplicationRecord step_orderable_element&.step end - def parent - step_orderable_element&.step - end - def create_value!(created_by, form_field, value, not_applicable: false) ActiveRecord::Base.transaction(requires_new: true) do current_form_field_value = form_field_values.where(latest: true).last @@ -102,7 +99,8 @@ class FormResponse < ApplicationRecord form_id: form_id, discarded_at: nil, submitted_by: nil, - created_by_id: user.id + created_by_id: user.id, + parent: parent ) parent.step_orderable_elements.create!( diff --git a/app/models/step.rb b/app/models/step.rb index d7320407c..0cc7c64f8 100644 --- a/app/models/step.rb +++ b/app/models/step.rb @@ -42,8 +42,7 @@ class Step < ApplicationRecord has_many :step_tables, inverse_of: :step, dependent: :destroy has_many :tables, through: :step_tables, dependent: :destroy has_many :report_elements, inverse_of: :step, dependent: :destroy - has_many :form_responses, through: :step_orderable_elements, - source: :orderable, source_type: 'FormResponse', dependent: :destroy + has_many :form_responses, as: :parent, inverse_of: :parent, dependent: :destroy accepts_nested_attributes_for :checklists, reject_if: :all_blank, diff --git a/db/migrate/20250325124848_add_parent_to_form_responses.rb b/db/migrate/20250325124848_add_parent_to_form_responses.rb new file mode 100644 index 000000000..18bdc6dfd --- /dev/null +++ b/db/migrate/20250325124848_add_parent_to_form_responses.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +class AddParentToFormResponses < ActiveRecord::Migration[7.0] + def up + add_reference :form_responses, :parent, polymorphic: true + + FormResponse.find_each do |form_response| + form_response.update(parent_type: 'Step', parent_id: form_response.step.id) + end + end + + def down + remove_reference :form_responses, :parent, polymorphic: true + end +end diff --git a/db/schema.rb b/db/schema.rb index aa0346b89..3c6536756 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: 2025_02_05_100223) do +ActiveRecord::Schema[7.0].define(version: 2025_03_25_124848) do # These are extensions that must be enabled in order to support this database enable_extension "btree_gist" enable_extension "pg_trgm" @@ -242,6 +242,8 @@ ActiveRecord::Schema[7.0].define(version: 2025_02_05_100223) do t.text "selection", array: true t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.boolean "flag" + t.jsonb "data" t.index ["created_by_id"], name: "index_form_field_values_on_created_by_id" t.index ["form_field_id"], name: "index_form_field_values_on_form_field_id" t.index ["form_response_id"], name: "index_form_field_values_on_form_response_id" @@ -277,8 +279,11 @@ ActiveRecord::Schema[7.0].define(version: 2025_02_05_100223) do t.datetime "created_at", null: false t.datetime "updated_at", null: false t.bigint "previous_form_response_id" + t.string "parent_type" + t.bigint "parent_id" t.index ["created_by_id"], name: "index_form_responses_on_created_by_id" t.index ["form_id"], name: "index_form_responses_on_form_id" + t.index ["parent_type", "parent_id"], name: "index_form_responses_on_parent" t.index ["previous_form_response_id"], name: "index_form_responses_on_previous_form_response_id" t.index ["submitted_by_id"], name: "index_form_responses_on_submitted_by_id" end @@ -923,6 +928,7 @@ ActiveRecord::Schema[7.0].define(version: 2025_02_05_100223) do t.string "external_id" t.integer "parent_connections_count" t.integer "child_connections_count" + t.bigint "my_module_id" t.index "(('IT'::text || id)) gin_trgm_ops", name: "index_repository_rows_on_repository_row_code", using: :gin t.index "((id)::text) gin_trgm_ops", name: "index_repository_rows_on_id_text", using: :gin t.index "date_trunc('minute'::text, archived_on)", name: "index_repository_rows_on_archived_on_as_date_time_minutes" @@ -930,6 +936,7 @@ ActiveRecord::Schema[7.0].define(version: 2025_02_05_100223) do t.index "trim_html_tags((name)::text) gin_trgm_ops", name: "index_repository_rows_on_name", using: :gin t.index ["archived"], name: "index_repository_rows_on_archived" t.index ["archived_by_id"], name: "index_repository_rows_on_archived_by_id" + t.index ["my_module_id"], name: "index_repository_rows_on_my_module_id" t.index ["repository_id", "external_id"], name: "unique_index_repository_rows_on_external_id", unique: true t.index ["repository_id"], name: "index_repository_rows_on_repository_id" t.index ["restored_by_id"], name: "index_repository_rows_on_restored_by_id" @@ -1606,6 +1613,7 @@ ActiveRecord::Schema[7.0].define(version: 2025_02_05_100223) do add_foreign_key "repository_row_connections", "repository_rows", column: "parent_id" add_foreign_key "repository_row_connections", "users", column: "created_by_id" add_foreign_key "repository_row_connections", "users", column: "last_modified_by_id" + add_foreign_key "repository_rows", "my_modules" add_foreign_key "repository_rows", "users", column: "archived_by_id" add_foreign_key "repository_rows", "users", column: "created_by_id" add_foreign_key "repository_rows", "users", column: "last_modified_by_id"