Skip to content

Commit f21a0ba

Browse files
Refactor Get Involved
This page was created differently to all the other content types. Refactoring so it is the same as everything else. Co-authored-by: Jon Hallam <[email protected]>
1 parent e0c26e4 commit f21a0ba

File tree

7 files changed

+181
-314
lines changed

7 files changed

+181
-314
lines changed

app/controllers/get_involved_controller.rb

-141
This file was deleted.

app/helpers/get_involved_helper.rb

-28
This file was deleted.
+109
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,111 @@
11
class GetInvolvedPresenter < ContentItemPresenter
2+
def open_consultation_count
3+
Services.search_api.search({ filter_content_store_document_type: "open_consultation", count: 0 })["total"]
4+
end
5+
6+
def closed_consultation_count
7+
query = {
8+
filter_content_store_document_type: "closed_consultation",
9+
filter_end_date: "from: #{1.year.ago}",
10+
count: 0,
11+
}
12+
13+
Services.search_api.search(query)["total"]
14+
end
15+
16+
def next_closing_consultation
17+
query = {
18+
filter_content_store_document_type: "open_consultation",
19+
filter_end_date: "from: #{Time.zone.now.to_date}",
20+
fields: "end_date,title,link",
21+
order: "end_date",
22+
count: 1,
23+
}
24+
25+
Services.search_api.search(query)["results"].first
26+
end
27+
28+
def take_part_pages
29+
content_item.dig("links", "take_part_pages")
30+
end
31+
32+
def recently_opened
33+
filtered_links(recently_opened_consultations, I18n.t("get_involved.closes"))
34+
end
35+
36+
def recent_outcomes
37+
filtered_links(recent_consultation_outcomes, I18n.t("get_involved.closed"))
38+
end
39+
40+
def time_until_closure(consultation)
41+
days_left = (consultation["end_date"].to_date - Time.zone.now.to_date).to_i
42+
case days_left
43+
when :negative?.to_proc
44+
I18n.t("get_involved.closed")
45+
when :zero?.to_proc
46+
I18n.t("get_involved.closing_today")
47+
when 1
48+
I18n.t("get_involved.closing_tomorrow")
49+
else
50+
I18n.t("get_involved.days_left", number_of_days: days_left)
51+
end
52+
end
53+
54+
def consultations_link
55+
filters = %w[open_consultations closed_consultations]
56+
"/search/policy-papers-and-consultations?#{filters.to_query('content_store_document_type')}"
57+
end
58+
59+
private
60+
61+
def recently_opened_consultations
62+
query = {
63+
filter_content_store_document_type: "open_consultation",
64+
fields: "end_date,title,link,organisations",
65+
order: "-start_date",
66+
count: 3,
67+
}
68+
69+
Services.search_api.search(query)["results"]
70+
end
71+
72+
def recent_consultation_outcomes
73+
query = {
74+
filter_content_store_document_type: "consultation_outcome",
75+
filter_end_date: "to: #{Time.zone.now.to_date}",
76+
fields: "end_date,title,link,organisations",
77+
order: "-end_date",
78+
count: 3,
79+
}
80+
81+
Services.search_api.search(query)["results"]
82+
end
83+
84+
def filtered_links(array, close_status)
85+
array.map do |item|
86+
{
87+
link: {
88+
text: item["title"],
89+
path: item["link"],
90+
description: "#{close_status} #{item['end_date'].to_date.strftime('%d %B %z')}",
91+
},
92+
metadata: {
93+
public_updated_at: Time.zone.parse(org_time(item)),
94+
document_type: org_acronym(item),
95+
},
96+
}
97+
end
98+
end
99+
100+
def org_time(item)
101+
item["organisations"].map { |org|
102+
org["public_timestamp"]
103+
}.join(", ")
104+
end
105+
106+
def org_acronym(item)
107+
item["organisations"].map { |org|
108+
org["acronym"]
109+
}.join(", ")
110+
end
2111
end

app/views/content_items/get_involved.html.erb

+12-24
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,3 @@
1-
<% page_title %>
2-
<% page_class "govuk-main-wrapper" %>
3-
<%= render "govuk_publishing_components/components/breadcrumbs", {
4-
collapse_on_mobile: true,
5-
breadcrumbs: [
6-
{
7-
title: "Home",
8-
url: "/"
9-
}
10-
]
11-
} %>
12-
131
<div class="govuk-grid-row">
142
<div class="govuk-grid-column-two-thirds">
153
<%= render "govuk_publishing_components/components/title", {
@@ -51,32 +39,32 @@
5139
<div class="govuk-grid-row">
5240
<div class="govuk-grid-column-one-third">
5341
<%= render "govuk_publishing_components/components/big_number", {
54-
number: @open_consultation_count,
42+
number: @content_item.open_consultation_count,
5543
label: t('get_involved.open_consultations'),
5644
href: "/search/policy-papers-and-consultations?content_store_document_type=open_consultations"
5745
} %>
5846
</div>
5947
<div class="govuk-grid-column-two-thirds">
6048
<%= render "govuk_publishing_components/components/big_number", {
61-
number: @closed_consultation_count,
49+
number: @content_item.closed_consultation_count,
6250
label: t('get_involved.closed_consultations'),
6351
href: "/search/policy-papers-and-consultations?content_store_document_type=closed_consultations"
6452
} %>
6553
</div>
6654
</div>
6755

68-
<% if @next_closing_consultation %>
56+
<% if @content_item.next_closing_consultation %>
6957
<%# Attention to closing consultation %>
7058
<%= render "govuk_publishing_components/components/inset_text", {
7159
} do %>
7260
<%= render "govuk_publishing_components/components/heading", {
73-
text: time_until_closure(@next_closing_consultation),
61+
text: @content_item.time_until_closure(@content_item.next_closing_consultation),
7462
heading_level: 3
7563
} %>
7664
<p class="govuk-body">
77-
<%= @next_closing_consultation['title'] %>
65+
<%= @content_item.next_closing_consultation['title'] %>
7866
</p>
79-
<%= link_to t('get_involved.read_respond'), @next_closing_consultation['link'], class: "govuk-link" %>
67+
<%= link_to t('get_involved.read_respond'), @content_item.next_closing_consultation['link'], class: "govuk-link" %>
8068
<% end %>
8169
<% end %>
8270
</div>
@@ -93,9 +81,9 @@
9381
margin_bottom: 4
9482
} %>
9583
<%= render "govuk_publishing_components/components/document_list", {
96-
items: @recently_opened
84+
items: @content_item.recently_opened
9785
} %>
98-
<%= link_to(t('get_involved.search_all'), get_consultations_link, class: "govuk-link" ) %>
86+
<%= link_to(t('get_involved.search_all'), @content_item.consultations_link, class: "govuk-link" ) %>
9987
</div>
10088
</div>
10189

@@ -111,10 +99,10 @@
11199
} %>
112100

113101
<%= render "govuk_publishing_components/components/document_list", {
114-
items: @recent_outcomes
102+
items: @content_item.recent_outcomes
115103
} %>
116104

117-
<%= link_to(t('get_involved.search_all'), get_consultations_link, class: "govuk-link" ) %>
105+
<%= link_to(t('get_involved.search_all'), @content_item.consultations_link, class: "govuk-link" ) %>
118106
</div>
119107
</div>
120108

@@ -177,7 +165,7 @@
177165
id: "take-part"
178166
} %>
179167
<div role="list">
180-
<% @take_part_pages.each_with_index do |take_part_page, index| %>
168+
<% @content_item.take_part_pages.each_with_index do |take_part_page, index| %>
181169
<% if index % 3 == 0 && index != 0 %> </div> <% end %>
182170
<% if index % 3 == 0 %>
183171
<div class="govuk-grid-row">
@@ -193,7 +181,7 @@
193181
image_loading: "lazy",
194182
} %>
195183
</div>
196-
<% if index == @take_part_pages.size-1 %>
184+
<% if index == @content_item.take_part_pages.size-1 %>
197185
</div>
198186
<% end %>
199187
<% end %>

config/routes.rb

-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
get "/government/uploads/*path" => "asset_manager_redirect#show", format: false
1818

19-
get "/government/get-involved" => "get_involved#show"
20-
2119
get "/service-manual/search",
2220
to: redirect { |_, request|
2321
query = request.query_parameters.merge(filter_manual: "/service-manual").to_query

0 commit comments

Comments
 (0)