Add parent relationship for FormResponse [SCI-11707]

This commit is contained in:
Andrej 2025-03-27 11:49:29 +01:00
parent 78de60e38e
commit 1c8edec966
6 changed files with 34 additions and 9 deletions

View file

@ -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,

View file

@ -23,4 +23,8 @@ class FormFieldValue < ApplicationRecord
def value_in_range?
true
end
def name
form_field&.name
end
end

View file

@ -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!(

View file

@ -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,

View file

@ -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

View file

@ -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"