Skip to content

Commit

Permalink
Remove unneeded #conditions_for_page_with_index method
Browse files Browse the repository at this point in the history
  • Loading branch information
lfdebrux committed Feb 24, 2025
1 parent f569097 commit ccbf8fc
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 88 deletions.
2 changes: 1 addition & 1 deletion app/components/page_list_component/view.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
</dd>
</div>

<% conditions_for_page_with_index(page).each do |condition, _route_index| %>
<% page.routing_conditions.each do |condition| %>
<div id="<%= PageListComponent::ErrorSummary::View.error_id(condition.id) %>" class="govuk-summary-list__row app-page-list__row <%= class_names( "govuk-form-group--error": condition.has_routing_errors?) %>">
<dt class="govuk-summary-list__key govuk-summary-list__key app-page-list__key">
<%= t("page_conditions.condition_name", question_number: condition_page_position(condition)) %>
Expand Down
26 changes: 0 additions & 26 deletions app/components/page_list_component/view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,35 +58,9 @@ def condition_page_position(condition)
page_position(check_page)
end

def conditions_for_page_with_index(page)
routing_conditions_with_index.fetch(page.id, [])
end

def routing_conditions_with_index
@routing_conditions_with_index ||= process_routing_conditions
end

def skip_condition_route_page_text(condition)
routing_page = @pages.find { |page| page.id == condition.routing_page_id }
I18n.t("page_conditions.skip_condition_route_page_text", route_page_question_text: routing_page.question_text, route_page_question_number: routing_page.position)
end

# Create hash of page_id => [condition, index]
# where index is the index of the condition in the array of conditions for
# the page referenced by check_page_id
def process_routing_conditions
all_form_conditions = @pages.flat_map(&:routing_conditions).compact_blank

all_form_conditions
.group_by(&:check_page_id)
.values
.flat_map { |conditions|
conditions.map.with_index(1) do |condition, index|
[condition.routing_page_id, [condition, index]] # inclde routing_page_id, so we can group by it
end
}
.group_by(&:first)
.transform_values { |pairs| pairs.map(&:last) } # drop routing_page_id from the value of the hash - it is now the key
end
end
end
61 changes: 0 additions & 61 deletions spec/components/page_list_component/view_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -290,66 +290,5 @@
end
end
end

describe "#conditions_for_page_with_index" do
context "when there are no conditions" do
let(:pages) do
[(build :page, id: 1, position: 1, question_text: "What country do you live in?", routing_conditions:)]
end
let(:routing_conditions) { [] }

it "returns an array of conditions for the page" do
page = pages.first
expect(page_list_component.conditions_for_page_with_index(page)).to eq([])
end
end

context "when there is one page with one condition" do
let(:pages) do
[(build :page, id: 1, position: 1, question_text: "What country do you live in?", routing_conditions:)]
end
let(:routing_conditions) { [build(:condition, id: 1, routing_page_id: 1, check_page_id: 1, answer_value: "Wales", goto_page_id: 3)] }

it "returns an array of conditions for the page" do
page = pages.first
expect(page_list_component.conditions_for_page_with_index(page)).to eq([[routing_conditions.first, 1]])
end
end

context "when there is one page with multiple conditions" do
let(:pages) do
[(build :page, id: 1, position: 1, question_text: "What country do you live in?", routing_conditions:)]
end

let(:routing_conditions) do
[
build(:condition, id: 1, routing_page_id: 1, check_page_id: 1, answer_value: "Wales", goto_page_id: 3),
build(:condition, id: 2, routing_page_id: 2, check_page_id: 1, answer_value: nil, goto_page_id: 4),
]
end

it "returns the correct condition with index" do
page = build(:page, id: 2)
expect(page_list_component.conditions_for_page_with_index(page)).to eq([[routing_conditions.second, 2]])
end
end

context "when there is one page with one condition and a condition for another pages" do
let(:pages) do
[(build :page, id: 1, position: 1, question_text: "What country do you live in?", routing_conditions:)]
end
let(:routing_conditions) do
[
build(:condition, id: 1, routing_page_id: 1, check_page_id: 1, answer_value: "Wales", goto_page_id: 3),
build(:condition, id: 1, routing_page_id: 2, check_page_id: 2, answer_value: "England", goto_page_id: 3),
]
end

it "returns an array of conditions for the page" do
page = pages.first
expect(page_list_component.conditions_for_page_with_index(page)).to eq([[routing_conditions.first, 1]])
end
end
end
end
end

0 comments on commit ccbf8fc

Please sign in to comment.