Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions app/models/condition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ def self.create_and_update_form!(...)

def save_and_update_form
save!
form.question_section_completed = false
form.save_draft!
form.save_question_changes!
end

def destroy_and_update_form!
destroy! && form.update!(question_section_completed: false)
destroy! && form.save_question_changes!
end

def validation_errors
Expand Down
2 changes: 2 additions & 0 deletions app/models/form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class Form < ApplicationRecord
def save_question_changes!
self.question_section_completed = false
save_draft!
# Make sure the updated_at is updated as we use this to determine if the form has changed in forms-runner.
touch unless changed?
end

def save_draft!
Expand Down
2 changes: 1 addition & 1 deletion app/models/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def self.create_and_update_form!(...)

def destroy_and_update_form!
form = self.form
destroy! && form.update!(question_section_completed: false)
destroy! && form.save_question_changes!
end

def save_and_update_form
Expand Down
27 changes: 27 additions & 0 deletions spec/models/condition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,33 @@
end
end

describe "#destroy_and_update_form!" do
subject(:condition) { create :condition, :with_exit_page, routing_page: page, check_page: page }

let(:page) { create :page, :with_selection_settings, form: }
let(:form) { create :form, question_section_completed: true }

before do
condition.destroy_and_update_form!
end

it "destroys the condition" do
expect(condition).to be_destroyed
end

it "sets form.question_section_completed to false" do
expect(form.reload.question_section_completed).to be false
end

context "when the form is live" do
let(:form) { create :form, :live }

it "updates the form state to live_with_draft" do
expect(form.reload.state).to eq("live_with_draft")
end
end
end

describe "#validation_errors" do
let(:form) { create :form }
let(:routing_page) { create :page, form: }
Expand Down
8 changes: 8 additions & 0 deletions spec/models/form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,14 @@
}.to change { form.reload.state }.to("archived_with_draft")
end
end

context "when no attributes on the form will be changed" do
it "still changes the form's updated_at" do
expect {
form.save_question_changes!
}.to(change { form.reload.updated_at })
end
end
end

describe "#save_draft!" do
Expand Down
23 changes: 17 additions & 6 deletions spec/models/page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -418,13 +418,19 @@
end

context "when the form question section is complete" do
let(:form) { create(:form, question_section_completed: true) }
let(:form) { create(:form, :live, question_section_completed: true) }

it "updates the form to mark the question section as incomplete" do
expect {
described_class.create_and_update_form!(**page_params)
}.to change { form.reload.question_section_completed }.to(false)
end

it "changes the form to live_with_draft" do
expect {
described_class.create_and_update_form!(**page_params)
}.to change { form.reload.state }.to("live_with_draft")
end
end

context "when the page has answer settings" do
Expand All @@ -444,14 +450,19 @@
end

describe "#destroy_and_update_form!" do
let(:page) { create :page }
let(:form) { page.form }
let(:form) { create :form, :live, question_section_completed: true }
let(:page) { create :page, form: }

it "sets form.question_section_completed to false" do
form.update!(question_section_completed: true)
expect {
page.destroy_and_update_form!
}.to change { form.reload.question_section_completed }.to(false)
end

page.destroy_and_update_form!
expect(form.question_section_completed).to be false
it "changes the form to live_with_draft" do
expect {
page.destroy_and_update_form!
}.to change { form.reload.state }.to("live_with_draft")
end
end

Expand Down
Loading