mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-09-29 08:24:40 +08:00
Merge pull request #8123 from andrej-scinote/aj_SCI_11382
Add delete action to step form response [SCI-11382]
This commit is contained in:
commit
c4b3654544
7 changed files with 36 additions and 4 deletions
|
@ -58,6 +58,11 @@ module StepElements
|
||||||
def destroy
|
def destroy
|
||||||
ActiveRecord::Base.transaction do
|
ActiveRecord::Base.transaction do
|
||||||
log_step_form_activity(:form_deleted, { form: @form_response.form.id })
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -83,7 +83,8 @@ export default {
|
||||||
return {
|
return {
|
||||||
form: this.element.attributes.orderable.form,
|
form: this.element.attributes.orderable.form,
|
||||||
formResponse: this.element.attributes.orderable,
|
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() {
|
mounted() {
|
||||||
|
@ -155,12 +156,14 @@ export default {
|
||||||
axios.post(this.formResponse.urls.submit).then((response) => {
|
axios.post(this.formResponse.urls.submit).then((response) => {
|
||||||
const { attributes } = response.data.data;
|
const { attributes } = response.data.data;
|
||||||
this.formResponse = attributes.orderable;
|
this.formResponse = attributes.orderable;
|
||||||
|
this.deleteUrl = attributes.orderable.urls.delete_url;
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
resetForm() {
|
resetForm() {
|
||||||
axios.post(this.formResponse.urls.reset).then((response) => {
|
axios.post(this.formResponse.urls.reset).then((response) => {
|
||||||
const { attributes } = response.data.data;
|
const { attributes } = response.data.data;
|
||||||
this.formResponse = attributes.orderable;
|
this.formResponse = attributes.orderable;
|
||||||
|
this.deleteUrl = attributes.orderable.urls.delete_url;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,7 @@ export default {
|
||||||
},
|
},
|
||||||
deleteElement() {
|
deleteElement() {
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: this.element.attributes.orderable.urls.delete_url,
|
url: this.deleteUrl || this.element.attributes.orderable.urls.delete_url,
|
||||||
type: 'DELETE',
|
type: 'DELETE',
|
||||||
success: (result) => {
|
success: (result) => {
|
||||||
this.$emit(
|
this.$emit(
|
||||||
|
|
|
@ -17,6 +17,19 @@ class FormResponse < ApplicationRecord
|
||||||
|
|
||||||
has_many :form_field_values, dependent: :destroy
|
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
|
def step
|
||||||
step_orderable_element&.step
|
step_orderable_element&.step
|
||||||
end
|
end
|
||||||
|
@ -57,7 +70,7 @@ class FormResponse < ApplicationRecord
|
||||||
|
|
||||||
ActiveRecord::Base.transaction(requires_new: true) do
|
ActiveRecord::Base.transaction(requires_new: true) do
|
||||||
new_form_response = dup
|
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_values.latest.find_each do |form_field_value|
|
||||||
form_field_value.dup.update!(form_response: new_form_response, created_by: user)
|
form_field_value.dup.update!(form_response: new_form_response, created_by: user)
|
||||||
|
|
|
@ -42,6 +42,8 @@ class Step < ApplicationRecord
|
||||||
has_many :step_tables, inverse_of: :step, dependent: :destroy
|
has_many :step_tables, inverse_of: :step, dependent: :destroy
|
||||||
has_many :tables, through: :step_tables, dependent: :destroy
|
has_many :tables, through: :step_tables, dependent: :destroy
|
||||||
has_many :report_elements, inverse_of: :step, 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,
|
accepts_nested_attributes_for :checklists,
|
||||||
reject_if: :all_blank,
|
reject_if: :all_blank,
|
||||||
|
@ -204,6 +206,7 @@ class Step < ApplicationRecord
|
||||||
def cascade_before_destroy
|
def cascade_before_destroy
|
||||||
assets.each(&:destroy)
|
assets.each(&:destroy)
|
||||||
tables.each(&:destroy)
|
tables.each(&:destroy)
|
||||||
|
form_responses.each(&:destroy)
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_completed_on
|
def set_completed_on
|
||||||
|
|
|
@ -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)
|
list[:reset] = reset_step_form_response_path(object.step, object) if can_reset_form_response?(user, object)
|
||||||
if can_manage_step?(user, object.step)
|
if can_manage_step?(user, object.step)
|
||||||
list[:move_url] = move_step_form_response_path(object.step, object)
|
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
|
end
|
||||||
|
|
||||||
list
|
list
|
||||||
|
|
|
@ -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
|
Loading…
Add table
Reference in a new issue