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
2 changes: 1 addition & 1 deletion app/controllers/pages/secondary_skip_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,6 @@ def ensure_secondary_skip_blank
end

def secondary_skip_condition
@secondary_skip_condition ||= current_form.pages.flat_map(&:routing_conditions).compact_blank.find { |c| c.secondary_skip? && c.check_page_id == page.id }
@secondary_skip_condition ||= page.secondary_skip_condition
end
end
3 changes: 1 addition & 2 deletions app/controllers/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ def delete
@item_name = page.question_text
@back_url = edit_question_path(current_form.id, page.id)

all_form_conditions = current_form.pages.flat_map(&:routing_conditions).compact_blank
@page_goto_conditions = all_form_conditions.select { |condition| condition.goto_page_id == page.id }
@page_goto_conditions = page.goto_conditions

if page.routing_conditions.any? && page.routing_conditions.first.secondary_skip?
@routing = :start_of_secondary_skip_route
Expand Down
2 changes: 1 addition & 1 deletion app/input_objects/pages/conditions_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def create_exit_page?
end

def secondary_skip?
PageRoutesService.new(form:, pages: form.pages, page:).routes.find(&:secondary_skip?)
page.secondary_skip_condition.present?
end

private
Expand Down
3 changes: 1 addition & 2 deletions app/input_objects/pages/delete_condition_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ def goto_page_question_text

def has_secondary_skip?
check_page = pages.find(proc { raise "Cannot find page with id #{check_page_id}" }) { it.id == check_page_id }
page_conditions_service = PageConditionsService.new(form:, pages:, page: check_page)
page_conditions_service.check_conditions.any? { it != record && it.routing_page_id != it.check_page_id }
check_page.check_conditions.any? { it != record && it.routing_page_id != it.check_page_id }
end

private
Expand Down
4 changes: 1 addition & 3 deletions app/input_objects/pages/routes/delete_confirmation_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ def to_partial_path
private

def delete_routes
pages = form.pages
page_routes = PageRoutesService.new(form:, pages:, page:).routes
page_routes.each(&:destroy_and_update_form!)
page.check_conditions.each(&:destroy_and_update_form!)
end
end
4 changes: 4 additions & 0 deletions app/models/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ def as_form_document_step
}
end

def secondary_skip_condition
check_conditions.where(answer_value: nil).where.not("check_page_id = routing_page_id").first
end

private

def update_form
Expand Down
2 changes: 1 addition & 1 deletion app/presenters/route_summary_card_data_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def summary_card_data
end

def routes
@routes ||= PageRoutesService.new(form:, pages:, page:).routes
@routes ||= page.check_conditions
end

def pages
Expand Down
21 changes: 0 additions & 21 deletions app/services/page_conditions_service.rb

This file was deleted.

23 changes: 0 additions & 23 deletions app/services/page_routes_service.rb

This file was deleted.

20 changes: 15 additions & 5 deletions spec/input_objects/pages/conditions_input_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -267,13 +267,23 @@
let(:page_routes_service) { instance_double(PageRoutesService) }

before do
allow(PageRoutesService).to receive(:new).and_return(page_routes_service)
allow(page_routes_service).to receive(:routes).and_return([instance_double(Api::V1::ConditionResource, secondary_skip?: true)])
page.reload
end

it "calls the PageRoutesService" do
expect(PageRoutesService).to receive(:new).with(form:, pages:, page:)
conditions_input.secondary_skip?
context "when the page has a secondary skip condition" do
let(:condition) { create :condition, routing_page_id: pages.first.id, check_page_id: page.id, goto_page_id: pages.fourth.id }

it "is true" do
expect(conditions_input.secondary_skip?).to be true
end
end

context "when the page does not have a secondary skip condition" do
let(:condition) { create :condition, routing_page_id: page.id, check_page_id: page.id, answer_value: "England", goto_page_id: pages.fourth.id }

it "is false" do
expect(conditions_input.secondary_skip?).to be false
end
end
end
end
8 changes: 3 additions & 5 deletions spec/input_objects/pages/delete_condition_input_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@
subject(:has_secondary_skip?) { delete_condition_input.has_secondary_skip? }

let(:condition) do
condition = build :condition, id: 1, routing_page_id: start_of_branches.id, check_page_id: start_of_branches.id, answer_value: "Wales", goto_page_id: start_of_second_branch.id
start_of_branches.routing_conditions << condition
condition
create :condition, routing_page_id: start_of_branches.id, check_page_id: start_of_branches.id, answer_value: "Wales", goto_page_id: start_of_second_branch.id
end

let(:start_of_branches) { pages.first }
Expand All @@ -97,8 +95,8 @@
let(:end_of_branches) { pages.last }

before do
secondary_skip_condition = build :condition, id: 2, routing_page_id: end_of_first_branch.id, check_page_id: start_of_branches.id, answer_value: nil, goto_page_id: end_of_branches.id
end_of_first_branch.routing_conditions << secondary_skip_condition
create :condition, routing_page_id: end_of_first_branch.id, check_page_id: start_of_branches.id, answer_value: nil, goto_page_id: end_of_branches.id
pages.each(&:reload)
end

it { is_expected.to be true }
Expand Down
25 changes: 25 additions & 0 deletions spec/models/page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -688,4 +688,29 @@
end
end
end

describe "#secondary_skip_condition" do
let(:form) { create :form, :ready_for_routing }
let(:page) { form.pages.second }

before do
# create some conditions that won't be returned
create(:condition, routing_page: page, check_page: page, goto_page: form.pages.fifth, answer_value: "Option 1")
create(:condition, routing_page: page, check_page: page, goto_page: form.pages.fifth, answer_value: nil)
end

context "when there is a secondary skip condition" do
let!(:secondary_skip_condition) { create :condition, routing_page: form.pages.third, check_page: page, goto_page: form.pages.fourth }

it "returns the secondary skip condition" do
expect(page.secondary_skip_condition).to eq secondary_skip_condition
end
end

context "when there are no secondary skip conditions" do
it "returns nil" do
expect(page.secondary_skip_condition).to be_nil
end
end
end
end
15 changes: 10 additions & 5 deletions spec/presenters/route_summary_card_data_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,16 @@
end

describe "#routes" do
it "calls the PageRoutesService" do
double = instance_double(PageRoutesService, routes: [])
allow(PageRoutesService).to receive(:new).with(form: form, pages: pages, page: page).and_return(double)
service.routes
expect(double).to have_received(:routes)
let!(:condition) { create :condition, routing_page: page, check_page: page, goto_page: pages.third, answer_value: "Option 1" }
let!(:secondary_skip_condition) { create :condition, routing_page: pages.second, check_page: page, goto_page: pages.fourth }

before do
pages.each(&:reload)
end

it "returns the conditions that check this page" do
expect(service.routes.count).to eq 2
expect(service.routes).to contain_exactly(condition, secondary_skip_condition)
end
end

Expand Down
18 changes: 0 additions & 18 deletions spec/requests/pages/secondary_skip_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
context "when a condition exists on the page" do
before do
create(:condition, routing_page_id: page.id, check_page_id: page.id, answer_value: "Option 1", goto_page_id: pages[2].id, skip_to_end: false)
page.reload
end

it "returns 200" do
Expand All @@ -51,8 +50,6 @@
context "when a secondary skip condition already exists on the page" do
before do
create(:condition, routing_page_id: pages[1].id, check_page_id: page.id, goto_page_id: pages[4].id)
page.reload
pages[1].reload
end

it "redirects to the show routes page" do
Expand Down Expand Up @@ -84,7 +81,6 @@
context "when a condition exists on the page" do
before do
create(:condition, routing_page_id: page.id, check_page_id: page.id, answer_value: "Option 1", goto_page_id: pages[2].id, skip_to_end: false)
page.reload
end

context "when the submission is successful" do
Expand All @@ -97,8 +93,6 @@
context "when a secondary skip condition already exists on the page" do
before do
create(:condition, routing_page_id: pages[1].id, check_page_id: page.id, goto_page_id: pages[4].id)
page.reload
pages[1].reload
end

it "redirects to the show routes page" do
Expand Down Expand Up @@ -140,7 +134,6 @@
context "when a condition exists on the page" do
before do
create(:condition, routing_page_id: page.id, check_page_id: page.id, answer_value: "Option 1", goto_page_id: pages[2].id, skip_to_end: false)
page.reload
end

context "when no secondary_skip exists on the page" do
Expand All @@ -153,8 +146,6 @@
context "when a secondary_skip exists on the page" do
before do
create(:condition, routing_page_id: pages[1].id, check_page_id: page.id, goto_page_id: pages[4].id)
page.reload
pages[1].reload
end

it "renders the edit template" do
Expand Down Expand Up @@ -187,7 +178,6 @@
context "when a condition exists on the page" do
before do
create(:condition, routing_page_id: page.id, check_page_id: page.id, answer_value: "Option 1", goto_page_id: pages[2].id, skip_to_end: false)
page.reload
end

context "when no secondary_skip exists on the page" do
Expand All @@ -200,8 +190,6 @@
context "when a secondary_skip exists on the page" do
before do
create(:condition, routing_page_id: pages[1].id, check_page_id: page.id, goto_page_id: pages[4].id)
page.reload
pages[1].reload
end

context "when the submission is successful without changing the routing_page_id" do
Expand Down Expand Up @@ -264,7 +252,6 @@
context "when a condition exists on the page" do
before do
create(:condition, routing_page_id: page.id, check_page_id: page.id, answer_value: "Option 1", goto_page_id: pages[2].id, skip_to_end: false)
page.reload
end

context "when no secondary_skip exists on the page" do
Expand All @@ -277,8 +264,6 @@
context "when a secondary_skip exists on the page" do
before do
create(:condition, routing_page_id: pages[1].id, check_page_id: page.id, goto_page_id: pages[4].id)
page.reload
pages[1].reload
end

it "returns 200" do
Expand All @@ -305,7 +290,6 @@
context "when a condition exists on the page" do
before do
create(:condition, routing_page_id: page.id, check_page_id: page.id, answer_value: "Option 1", goto_page_id: pages[2].id, skip_to_end: false)
page.reload
end

context "when no secondary_skip exists on the page" do
Expand All @@ -318,8 +302,6 @@
context "when a secondary_skip exists on the page" do
before do
create(:condition, routing_page_id: pages[1].id, check_page_id: page.id, goto_page_id: pages[4].id)
page.reload
pages[1].reload
end

context "when the submission is successful and deletes the secondary skip condition" do
Expand Down
57 changes: 0 additions & 57 deletions spec/services/page_conditions_service_spec.rb

This file was deleted.

Loading