Skip to content

Commit f222951

Browse files
authored
Merge pull request #1744 from alphagov/ldeb-add-page-conditions-service
Get list of page routes in routes controller show action
2 parents b179e7e + 43f325b commit f222951

File tree

11 files changed

+285
-65
lines changed

11 files changed

+285
-65
lines changed

app/controllers/pages/routes_controller.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
class Pages::RoutesController < PagesController
22
def show
33
back_link_url = form_pages_path(current_form.id)
4-
render locals: { current_form:, page:, pages: FormRepository.pages(current_form), back_link_url: }
4+
pages = FormRepository.pages(current_form)
5+
routes = PageRoutesService.new(form: current_form, pages:, page:).routes
6+
render locals: { current_form:, page:, pages:, routes:, back_link_url: }
57
end
68

79
def delete

app/input_objects/pages/routes/delete_confirmation_input.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ def to_partial_path
1818
private
1919

2020
def delete_routes
21-
all_form_routing_conditions = FormRepository.pages(form).flat_map(&:routing_conditions).compact_blank
22-
page_routes = all_form_routing_conditions.select { |rc| rc.check_page_id == page.id }
21+
pages = FormRepository.pages(form)
22+
page_routes = PageRoutesService.new(form:, pages:, page:).routes
2323
page_routes.each do |rc|
2424
rc.prefix_options[:form_id] = form.id
2525
rc.prefix_options[:page_id] = rc.routing_page_id

app/presenters/route_summary_card_data_presenter.rb

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,30 @@ class RouteSummaryCardDataPresenter
33
include ActionView::Helpers::UrlHelper
44
include GovukRailsCompatibleLinkHelper
55

6-
attr_reader :form, :page, :pages
6+
attr_reader :form, :pages, :page, :routes
77

8-
class << self
9-
def call(**args)
10-
new(**args)
11-
end
12-
end
13-
14-
def initialize(form:, page:, pages:)
15-
@page = page
16-
@pages = pages
8+
def initialize(form:, pages:, page:, routes:)
179
@form = form
10+
@pages = pages
11+
@page = page
12+
@routes = routes
1813
end
1914

2015
def summary_card_data
2116
conditional_cards = conditional_route_cards
2217
conditional_cards + [default_route_card(conditional_cards.length + 1)]
2318
end
2419

25-
def all_routes
26-
all_form_routing_conditions = pages.flat_map(&:routing_conditions).compact_blank
27-
all_form_routing_conditions.select { |rc| rc.check_page_id == page.id }
20+
private
21+
22+
def secondary_skip
23+
@secondary_skip ||= routes.find(&:secondary_skip?)
2824
end
2925

3026
def conditional_routes
31-
all_routes.select { |rc| rc.answer_value.present? }
27+
routes.select { |rc| rc.answer_value.present? }
3228
end
3329

34-
private
35-
3630
def conditional_route_cards
3731
conditional_routes.map.with_index(1) { |routing_condition, index| conditional_route_card(routing_condition, index) }
3832
end
@@ -64,7 +58,7 @@ def conditional_route_card(routing_condition, index)
6458
def default_route_card(index)
6559
continue_to_name = page.has_next_page? ? page_name(page.next_page) : end_page_name
6660

67-
actions = if FeatureService.new(group: form.group).enabled?(:branch_routing) && all_routes.find(&:secondary_skip?).present?
61+
actions = if FeatureService.new(group: form.group).enabled?(:branch_routing) && secondary_skip
6862
[
6963
edit_secondary_skip_link,
7064
delete_secondary_skip_link,
@@ -98,9 +92,7 @@ def delete_secondary_skip_link
9892
end
9993

10094
def secondary_skip_rows
101-
secondary_skip = all_routes.find(&:secondary_skip?)
102-
103-
if secondary_skip.blank?
95+
unless secondary_skip
10496
if FeatureService.new(group: form.group).enabled?(:branch_routing)
10597
return [
10698
{
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class PageConditionsService
2+
attr_reader :page
3+
4+
def initialize(form:, pages:, page:)
5+
@form = form
6+
@pages = pages
7+
@page = page
8+
end
9+
10+
def check_conditions
11+
form_conditions.select { |condition| condition.check_page_id == page.id }
12+
end
13+
14+
delegate :routing_conditions, to: :page
15+
16+
private
17+
18+
def form_conditions
19+
@form_conditions ||= @pages.flat_map(&:routing_conditions).compact_blank
20+
end
21+
end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class PageRoutesService
2+
attr_reader :page
3+
4+
def initialize(form:, pages:, page:)
5+
@form = form
6+
@pages = pages
7+
@page = page
8+
end
9+
10+
def routes
11+
check_conditions
12+
end
13+
14+
private
15+
16+
def check_conditions
17+
page_conditions_service.check_conditions
18+
end
19+
20+
def page_conditions_service
21+
@page_conditions_service ||= PageConditionsService.new(form: @form, pages: @pages, page: @page)
22+
end
23+
end

app/views/pages/routes/show.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
end;
1717
end %>
1818

19-
<% RouteSummaryCardDataPresenter.call(form: current_form, page:, pages:).summary_card_data.each do |card| %>
19+
<% RouteSummaryCardDataPresenter.new(form: current_form, page:, pages:, routes:).summary_card_data.each do |card| %>
2020
<%= govuk_summary_list(**card) %>
2121
<% end %>
2222

spec/presenters/route_summary_card_data_presenter_spec.rb

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33
describe RouteSummaryCardDataPresenter do
44
include Capybara::RSpecMatchers
55

6-
subject(:service) { described_class.new(form:, page: current_page, pages:) }
6+
subject(:service) { described_class.new(form:, pages:, page: current_page, routes:) }
77

88
let(:form) { build :form, id: 99, pages: }
99

1010
let(:current_page) do
1111
build(:page, id: 1, position: 1, question_text: "Current Question", next_page: next_page.id, routing_conditions:)
1212
end
1313

14+
let(:routes) do
15+
PageRoutesService.new(form:, pages:, page: current_page).routes
16+
end
17+
1418
let(:next_page) do
1519
build(:page, id: 2, position: 2, question_text: "Next Question", routing_conditions: next_page_routing_conditions)
1620
end
@@ -29,13 +33,6 @@
2933
allow(form).to receive(:group).and_return(build(:group))
3034
end
3135

32-
describe ".call" do
33-
it "instantiates and returns a new instance" do
34-
service = described_class.call(form:, page: current_page, pages:)
35-
expect(service).to be_an_instance_of(described_class)
36-
end
37-
end
38-
3936
describe "#summary_card_data" do
4037
context "with conditional routes" do
4138
it "returns an array of route cards including conditional and default routes" do
@@ -142,36 +139,5 @@
142139
expect(result[0][:rows][0][:value][:text]).to eq("Check your answers before submitting")
143140
end
144141
end
145-
146-
describe "#all_routes" do
147-
context "when no pages have conditions" do
148-
let(:routing_conditions) { [] }
149-
150-
it "is an empty array when there are no matching conditions" do
151-
expect(service.all_routes).to be_empty
152-
end
153-
end
154-
155-
context "when pages have conditions with matching check_page_ids" do
156-
let(:pages) do
157-
[
158-
build(:page, id: 1, position: 1, question_text: "Current Question", next_page: next_page.id, routing_conditions: [
159-
build(:condition, id: 1, routing_page_id: 1, check_page_id: 1, answer_value: "Yes", goto_page_id: 2, skip_to_end: false),
160-
]),
161-
build(:page, id: 2, position: 2, question_text: "Next Question", routing_conditions: [
162-
build(:condition, id: 1, routing_page_id: 2, check_page_id: 1, answer_value: nil, goto_page_id: nil, skip_to_end: true),
163-
]),
164-
build(:page, id: 3, position: 3, question_text: "unrelated question", routing_conditions: [
165-
build(:condition, id: 1, routing_page_id: 3, check_page_id: 3, answer_value: "Unrelated", goto_page_id: 5, skip_to_end: false),
166-
]),
167-
]
168-
end
169-
170-
it "returns all condtions which match the check_page_id" do
171-
expected_condtions = pages.first.routing_conditions + pages.second.routing_conditions
172-
expect(service.all_routes).to match_array(expected_condtions)
173-
end
174-
end
175-
end
176142
end
177143
end
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
require "rails_helper"
2+
3+
RSpec.describe PageConditionsService do
4+
subject(:page_conditions_service) do
5+
described_class.new(form:, pages:, page:)
6+
end
7+
8+
let(:form) do
9+
build(:form)
10+
end
11+
12+
include_context "with pages with routing"
13+
14+
describe "#check_conditions" do
15+
subject(:check_conditions) { page_conditions_service.check_conditions }
16+
17+
context "when page has no routes" do
18+
let(:page) { page_with_no_routes }
19+
20+
it { is_expected.to eq [] }
21+
end
22+
23+
context "when page has no check conditions" do
24+
let(:page) { end_of_a_secondary_skip }
25+
26+
it { is_expected.to eq [] }
27+
end
28+
29+
context "when page has check conditions" do
30+
let(:page) { page_with_skip_and_secondary_skip }
31+
32+
it { is_expected.to eq(page_with_skip_and_secondary_skip.routing_conditions + start_of_a_secondary_skip.routing_conditions) }
33+
end
34+
end
35+
36+
describe "#routing_conditions" do
37+
subject(:routing_conditions) { page_conditions_service.routing_conditions }
38+
39+
context "when page has no routes" do
40+
let(:page) { page_with_no_routes }
41+
42+
it { is_expected.to eq [] }
43+
end
44+
45+
context "when page has no routing conditions" do
46+
let(:page) { end_of_a_secondary_skip }
47+
48+
it { is_expected.to eq [] }
49+
end
50+
51+
context "when page has routing conditions" do
52+
let(:page) { page_with_skip_and_secondary_skip }
53+
54+
it { is_expected.to eq page.routing_conditions }
55+
end
56+
end
57+
end
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
require "rails_helper"
2+
3+
RSpec.describe PageRoutesService do
4+
subject(:page_routes_service) do
5+
described_class.new(form:, pages:, page:)
6+
end
7+
8+
let(:form) do
9+
build(:form)
10+
end
11+
12+
include_context "with pages with routing"
13+
14+
describe "#routes" do
15+
subject(:routes) { page_routes_service.routes }
16+
17+
context "when page has no routes" do
18+
let(:page) { page_with_no_routes }
19+
20+
it { is_expected.to eq [] }
21+
end
22+
23+
context "when page is at the end of a route" do
24+
let(:page) { end_of_a_secondary_skip }
25+
26+
it { is_expected.to eq [] }
27+
end
28+
29+
context "when page has a skip route" do
30+
let(:page) { page_with_skip_route }
31+
32+
it { is_expected.to eq page.routing_conditions }
33+
end
34+
35+
context "when page has a skip and secondary skip" do
36+
let(:page) { page_with_skip_and_secondary_skip }
37+
38+
it { is_expected.to eq(page.routing_conditions + start_of_a_secondary_skip.routing_conditions) }
39+
end
40+
end
41+
end

0 commit comments

Comments
 (0)