-
Notifications
You must be signed in to change notification settings - Fork 17
/
get_involved_controller.rb
141 lines (118 loc) · 3.84 KB
/
get_involved_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
class GetInvolvedController < ContentItemsController
attr_accessor :content_item
def show
load_content_item
load_get_involved_data
@do_not_show_breadcrumbs = true
@recently_opened = filtered_links(@recently_opened_consultations, t("get_involved.closes"))
@recent_outcomes = filtered_links(@recent_consultation_outcomes, t("get_involved.closed"))
render template: "content_items/get_involved"
end
def load_content_item
content_item = get_involved_item
@content_item = PresenterBuilder.new(
content_item,
content_item_path,
view_context,
).presenter
end
def load_get_involved_data
@open_consultation_count = retrieve_open_consultation_count
@closed_consultation_count = retrieve_date_filtered_closed_consultations(12)
@next_closing_consultation = retrieve_next_closing
@recently_opened_consultations = retrieve_new_consultations
@recent_consultation_outcomes = retrieve_consultation_outcomes
@take_part_pages = get_involved_item["links"]["take_part_pages"]
end
def get_involved_item
@get_involved_item ||= Services.content_store.content_item("/government/get-involved")
end
def retrieve_open_consultation_count
Services.search_api.search({ filter_content_store_document_type: "open_consultation", count: 0 })["total"]
end
# Aims to find a count of consultations closed in the last n months.
def retrieve_date_filtered_closed_consultations(months)
cutoff_date = Time.zone.now.prev_month(months)
query = {
filter_content_store_document_type: "closed_consultation",
filter_end_date: "from: #{cutoff_date}",
count: 0,
}
Services.search_api.search(query)["total"]
end
def retrieve_next_closing
# Ensure that on query we're not looking in the past
cutoff_date = Time.zone.now.to_date
query = {
filter_content_store_document_type: "open_consultation",
filter_end_date: "from: #{cutoff_date}",
fields: "end_date,title,link",
order: "end_date",
count: 1,
}
Services.search_api.search(query)["results"].first
end
def retrieve_new_consultations
query = {
filter_content_store_document_type: "open_consultation",
fields: "end_date,title,link,organisations",
order: "-start_date",
count: 3,
}
Services.search_api.search(query)["results"]
end
def retrieve_consultation_outcomes
# Ensure that on query we're not looking into the future
cutoff_date = Time.zone.now.to_date
query = {
filter_content_store_document_type: "consultation_outcome",
filter_end_date: "to: #{cutoff_date}",
fields: "end_date,title,link,organisations",
order: "-end_date",
count: 3,
}
Services.search_api.search(query)["results"]
end
def time_until_closure(consultation)
days_left = (consultation["end_date"].to_date - Time.zone.now.to_date).to_i
case days_left
when :negative?.to_proc
t("get_involved.closed")
when :zero?.to_proc
t("get_involved.closing_today")
when 1
t("get_involved.closing_tomorrow")
else
t("get_involved.days_left", number_of_days: days_left)
end
end
def date_microformat(attribute_name)
attribute_name.to_date.strftime("%d %B %Y")
end
def filtered_links(array, close_status)
array.map do |item|
{
link: {
text: item["title"],
path: item["link"],
description: "#{close_status} #{date_microformat(item['end_date'])}",
},
metadata: {
public_updated_at: Time.zone.parse(org_time(item)),
document_type: org_acronym(item),
},
}
end
end
private
def org_time(item)
item["organisations"].map { |org|
org["public_timestamp"]
}.join(", ")
end
def org_acronym(item)
item["organisations"].map { |org|
org["acronym"]
}.join(", ")
end
end