Skip to content

Commit

Permalink
Update state machine to update FormDocument
Browse files Browse the repository at this point in the history
The V1 API needs to keep FormDocuments up to date.

This commit updates FormDocuments when forms are made live and archived.

Controller tests are added for the FormController but most of the work
happens within the state machine.

Calls to the ModelSync class are made on transistions.
  • Loading branch information
thomasiles committed Sep 27, 2024
1 parent 96ad358 commit 802b093
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
11 changes: 11 additions & 0 deletions app/state_machines/form_state_machine.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
module FormStateMachine
extend ActiveSupport::Concern

def form_sync
Api::V2::ModelSync.new
end

included do
include AASM

Expand Down Expand Up @@ -32,6 +36,8 @@ module FormStateMachine

form_blob = snapshot(live_at:)
made_live_forms.create!(json_form_blob: form_blob.to_json, created_at: live_at)

form_sync.make_live(id, form_blob, aasm.from_state, external_id)
end

transitions from: %i[draft live_with_draft archived archived_with_draft], to: :live, guard: proc { ready_for_live }
Expand All @@ -46,6 +52,11 @@ module FormStateMachine
end

event :archive_live_form do
after do
form_blob = JSON.parse(archived_live_version)
form_sync.archive_live_form(id, form_blob, external_id)
end

transitions from: :live, to: :archived
transitions from: :live_with_draft, to: :archived_with_draft
end
Expand Down
20 changes: 20 additions & 0 deletions spec/requests/api/v1/forms_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,13 @@
expect(response.headers["Content-Type"]).to eq("application/json")
expect(json_body).to include(live_at: Time.zone.now)
end

it "creates a form document with the tag :live" do
form_to_be_made_live = create(:form, :ready_for_live)
post make_live_form_path(form_to_be_made_live), as: :json
expect(response).to have_http_status(:ok)
expect(Api::V2::FormDocument.find_by(form_id: form_to_be_made_live.id, tag: :live)).to be_present
end
end

describe "#show_live" do
Expand Down Expand Up @@ -377,6 +384,19 @@
expect(response.headers["Content-Type"]).to eq("application/json")
expect(json_body).to include(state: "archived")
end

it "creates a FormDocument with the tag :archived" do
form = create(:made_live_form).form
post archive_form_path(form), as: :json

expect(response).to have_http_status(:ok)
expect(Api::V2::FormDocument.find_by(form_id: form.id, tag: :archived)).to be_present
end

it "removes the FormDocument with the tag :live" do
form = create(:made_live_form).form
expect { post archive_form_path(form), as: :json }.to change { Api::V2::FormDocument.find_by(form_id: form.id, tag: :live) }.to(nil)
end
end

context "when the form is live with draft" do
Expand Down
15 changes: 15 additions & 0 deletions spec/state_machines/form_state_machine_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ class FakeForm < Form
end

describe ".archive_live_form" do
let(:form_sync) { instance_double(Api::V2::ModelSync) }

before do
allow(form_sync).to receive(:make_live)
allow(form_sync).to receive(:archive_live_form)
end

context "when the form is draft" do
let(:form) { FakeForm.new(state: :draft) }

Expand All @@ -120,6 +127,10 @@ class FakeForm < Form
context "when the form is live" do
let(:form) { FakeForm.new(state: :live) }

before do
allow(form).to receive_messages(form_sync:, archived_live_version: "{}")
end

it "transitions to archived if form is live" do
expect(form).to transition_from(:live).to(:archived).on_event(:archive_live_form)
end
Expand All @@ -128,6 +139,10 @@ class FakeForm < Form
context "when form is live_with_draft" do
let(:form) { FakeForm.new(state: :live_with_draft) }

before do
allow(form).to receive_messages(form_sync:, archived_live_version: "{}")
end

it "transitions to archived_with_draft" do
expect(form).to transition_from(:live_with_draft).to(:archived_with_draft).on_event(:archive_live_form)
end
Expand Down
5 changes: 4 additions & 1 deletion spec/support/shared_examples/state_machine.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
RSpec.shared_examples "transition to live state" do |form_object, form_state|
let(:form) { form_object.new(state: form_state) }
let(:form_sync) { instance_double(Api::V2::ModelSync) }

before do
allow(form).to receive(:ready_for_live).and_return(true)
allow(form_sync).to receive(:make_live)
allow(form_sync).to receive(:archive_live_form)
allow(form).to receive_messages(ready_for_live: true, form_sync:)
end

it "transitions to live state" do
Expand Down

0 comments on commit 802b093

Please sign in to comment.