Merge pull request #8123 from andrej-scinote/aj_SCI_11382

Add delete action to step form response [SCI-11382]
This commit is contained in:
andrej-scinote 2025-01-10 09:51:41 +01:00 committed by GitHub
commit c4b3654544
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 36 additions and 4 deletions

View file

@ -58,6 +58,11 @@ module StepElements
def destroy
ActiveRecord::Base.transaction do
log_step_form_activity(:form_deleted, { form: @form_response.form.id })
if @form_response.destroy
render json: {}, status: :ok
else
render json: { errors: @form_response.errors.full_messages }, status: :unprocessable_entity
end
end
end

View file

@ -83,7 +83,8 @@ export default {
return {
form: this.element.attributes.orderable.form,
formResponse: this.element.attributes.orderable,
formFieldValues: this.element.attributes.orderable.form_field_values
formFieldValues: this.element.attributes.orderable.form_field_values,
deleteUrl: this.element.attributes.orderable.urls.delete_url
};
},
mounted() {
@ -155,12 +156,14 @@ export default {
axios.post(this.formResponse.urls.submit).then((response) => {
const { attributes } = response.data.data;
this.formResponse = attributes.orderable;
this.deleteUrl = attributes.orderable.urls.delete_url;
});
},
resetForm() {
axios.post(this.formResponse.urls.reset).then((response) => {
const { attributes } = response.data.data;
this.formResponse = attributes.orderable;
this.deleteUrl = attributes.orderable.urls.delete_url;
});
}
}

View file

@ -13,7 +13,7 @@ export default {
},
deleteElement() {
$.ajax({
url: this.element.attributes.orderable.urls.delete_url,
url: this.deleteUrl || this.element.attributes.orderable.urls.delete_url,
type: 'DELETE',
success: (result) => {
this.$emit(

View file

@ -17,6 +17,19 @@ class FormResponse < ApplicationRecord
has_many :form_field_values, dependent: :destroy
belongs_to :previous_form_response,
-> { unscope(where: :discarded_at) },
class_name: 'FormResponse',
inverse_of: :next_form_response,
optional: true,
dependent: :destroy
has_one :next_form_response,
class_name: 'FormResponse',
foreign_key: 'previous_form_response_id',
inverse_of: :previous_form_response,
dependent: :destroy
def step
step_orderable_element&.step
end
@ -57,7 +70,7 @@ class FormResponse < ApplicationRecord
ActiveRecord::Base.transaction(requires_new: true) do
new_form_response = dup
new_form_response.update!(status: 'pending', created_by: user)
new_form_response.update!(status: 'pending', created_by: user, previous_form_response: self)
form_field_values.latest.find_each do |form_field_value|
form_field_value.dup.update!(form_response: new_form_response, created_by: user)

View file

@ -42,6 +42,8 @@ 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
accepts_nested_attributes_for :checklists,
reject_if: :all_blank,
@ -204,6 +206,7 @@ class Step < ApplicationRecord
def cascade_before_destroy
assets.each(&:destroy)
tables.each(&:destroy)
form_responses.each(&:destroy)
end
def set_completed_on

View file

@ -39,7 +39,8 @@ class StepFormResponseSerializer < ActiveModel::Serializer
list[:reset] = reset_step_form_response_path(object.step, object) if can_reset_form_response?(user, object)
if can_manage_step?(user, object.step)
list[:move_url] = move_step_form_response_path(object.step, object)
list[:move_targets_url] = move_targets_step_text_path(object.step, object) if can_manage_step?(user, object.step)
list[:move_targets_url] = move_targets_step_text_path(object.step, object)
list[:delete_url] = step_form_response_path(object.step, object)
end
list

View file

@ -0,0 +1,7 @@
# frozen_string_literal: true
class AddPreviousFormResponsesValueToFormResponse < ActiveRecord::Migration[7.0]
def change
add_reference :form_responses, :previous_form_response, foreign_key: { to_table: :form_responses }
end
end