Skip to content

Commit bb58045

Browse files
authored
Merge pull request #1892 from alphagov/add-user-research-contact-preference
Add user research contact preference
2 parents 94ee8e4 + 6be8a49 commit bb58045

22 files changed

Lines changed: 384 additions & 4 deletions
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
module Account
2+
class ContactForResearchController < WebController
3+
include AfterSignInPathHelper
4+
5+
skip_before_action :redirect_if_account_not_completed
6+
7+
def edit
8+
@contact_for_research_input = ContactForResearchInput.new(research_contact_status:, user: current_user)
9+
end
10+
11+
def update
12+
@contact_for_research_input = ContactForResearchInput.new(account_contact_for_research_params(current_user))
13+
14+
if @contact_for_research_input.submit
15+
redirect_to next_path
16+
else
17+
render :edit, status: :unprocessable_entity
18+
end
19+
end
20+
21+
def account_contact_for_research_params(user)
22+
params.require(:account_contact_for_research_input).permit(:research_contact_status).merge(user:)
23+
end
24+
25+
private
26+
27+
def redirect_contact_for_research_set
28+
redirect_to root_path unless current_user.research_contact_to_be_asked?
29+
end
30+
31+
def research_contact_status
32+
current_user.research_contact_status.to_sym if current_user.research_contact_status.in?(ContactForResearchInput::RADIO_OPTIONS)
33+
end
34+
35+
def next_path
36+
after_sign_in_next_path
37+
end
38+
end
39+
end

app/controllers/reports_controller.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,12 @@ def live_questions_csv
132132
disposition: "attachment; filename=#{csv_filename('live_questions_report')}"
133133
end
134134

135+
def contact_for_research
136+
data = Reports::ContactForResearchService.new.contact_for_research_data
137+
138+
render locals: { data: }
139+
end
140+
135141
private
136142

137143
def questions_feature_report(tag, report, questions)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Account::ContactForResearchInput < BaseInput
2+
attr_accessor :user, :research_contact_status
3+
4+
RADIO_OPTIONS = %w[consented declined].freeze
5+
6+
validates :research_contact_status, presence: true, inclusion: { in: RADIO_OPTIONS }
7+
8+
def submit
9+
return false if invalid?
10+
11+
user.research_contact_status = research_contact_status
12+
user.user_research_opted_in_at = Time.zone.now
13+
user.save!
14+
end
15+
16+
def values
17+
RADIO_OPTIONS
18+
end
19+
end

app/lib/after_sign_in_path_helper.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ def next_account_path
88

99
return edit_account_name_path if current_user.name.blank?
1010

11-
edit_account_terms_of_use_path if current_user.terms_agreed_at.blank?
11+
return edit_account_terms_of_use_path if current_user.terms_agreed_at.blank?
12+
13+
edit_account_contact_for_research_path if current_user.research_contact_to_be_asked?
1214
end
1315

1416
def store_location(path)

app/models/user.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
class User < ApplicationRecord
22
include GDS::SSO::User
3-
has_paper_trail only: %i[role organisation_id has_access]
43

54
class UserAuthenticationException < StandardError; end
65

@@ -21,6 +20,14 @@ class UserAuthenticationException < StandardError; end
2120

2221
serialize :permissions, coder: YAML, type: Array
2322

23+
enum :research_contact_status, {
24+
consented: "consented",
25+
declined: "declined",
26+
to_be_asked: "to_be_asked",
27+
}, prefix: "research_contact"
28+
29+
has_paper_trail only: %i[role organisation_id has_access]
30+
2431
enum :role, {
2532
super_admin: "super_admin",
2633
organisation_admin: "organisation_admin",
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Reports::ContactForResearchService
2+
def contact_for_research_data
3+
{
4+
caption: I18n.t("reports.contact_for_research.heading"),
5+
head: [
6+
{ text: I18n.t("reports.contact_for_research.table_headings.name") },
7+
{ text: I18n.t("reports.contact_for_research.table_headings.email") },
8+
{ text: I18n.t("reports.contact_for_research.table_headings.date_added") },
9+
],
10+
rows:,
11+
}
12+
end
13+
14+
private
15+
16+
def rows
17+
as_data_rows(user_contact_for_research)
18+
end
19+
20+
def as_data_rows(raw_data)
21+
raw_data.map do |name, email, user_research_opted_in_at|
22+
[{ text: name }, { text: email }, { text: user_research_opted_in_at.to_formatted_s(:long) }]
23+
end
24+
end
25+
26+
def user_contact_for_research
27+
User.research_contact_consented.order(user_research_opted_in_at: :desc).pluck(:name, :email, :user_research_opted_in_at)
28+
end
29+
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<% set_page_title(title_with_error_prefix(t("page_titles.account_contact_for_research"), @contact_for_research_input.errors.any?)) %>
2+
3+
<div class="govuk-grid-row">
4+
<div class="govuk-grid-column-two-thirds">
5+
<%= form_with(model: @contact_for_research_input, url: account_contact_for_research_path, method: :patch) do |f| %>
6+
<%= f.govuk_error_summary %>
7+
8+
<h1 class="govuk-heading-l">
9+
<%=t("page_titles.account_contact_for_research")%>
10+
</h1>
11+
12+
<%= f.govuk_collection_radio_buttons :research_contact_status, @contact_for_research_input.values, :itself, legend: nil, hint:{ text: @hint } %>
13+
14+
<%= f.govuk_submit t('continue') %>
15+
<% end %>
16+
</div>
17+
</div>
18+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<% set_page_title(t(".title")) %>
2+
<% content_for :back_link, govuk_back_link_to(reports_path, t("reports.back_link")) %>
3+
<div class="govuk-grid-row">
4+
<div class="govuk-grid-column-two-thirds">
5+
<h1 class="govuk-heading-l"><%= t(".title") %></h1>
6+
7+
<%= govuk_table(**data) %>
8+
9+
</div>
10+
</div>
11+

app/views/reports/index.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<li><%= govuk_link_to t("reports.users.title"), report_users_path %></li>
1111
<li><%= govuk_link_to t("reports.last_signed_in_at.title"), report_last_signed_in_at_path %></li>
1212
<li><%= govuk_link_to t("reports.csv_downloads.title"), report_csv_downloads_path %></li>
13+
<li><%= govuk_link_to t("reports.contact_for_research.title"), report_contact_for_research_path %></li>
1314
</ul>
1415
</div>
1516
</div>

config/initializers/warden/strategies/user_research.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ def prep_user(auth_hash)
1717
provider: Settings.auth_provider,
1818
uid: auth_hash[:uid],
1919
terms_agreed_at: Time.zone.now,
20+
research_contact_status: "declined",
21+
user_research_opted_in_at: Time.zone.now,
2022
)
2123
end
2224

0 commit comments

Comments
 (0)