/
model_patches.rb
158 lines (135 loc) · 6.44 KB
/
model_patches.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# -*- coding: utf-8 -*-
# Add a callback - to be executed before each request in development,
# and at startup in production - to patch existing app classes.
# Doing so in init/environment.rb wouldn't work in development, since
# classes are reloaded, but initialization is not run each time.
# See http://stackoverflow.com/questions/7072758/plugin-not-reloading-in-development-mode
#
Rails.configuration.to_prepare do
User.class_eval do
# Return this user’s survey
def survey
return @survey if @survey
@survey = MySociety::Survey.new(AlaveteliConfiguration::site_name, self.email)
end
end
# Now patch the validator for UserInfoRequestSentAlert.alert_type
# to permit 'survey_1' as a new alert type.
UserInfoRequestSentAlert._validate_callbacks[0].options[:in] << 'survey_1'
InfoRequest.class_eval do
def email_subject_request(opts = {})
html = opts.fetch(:html, true)
subject_title = html ? self.title : self.title.html_safe
if (!is_batch_request_template?) && (public_body.url_name == 'general_register_office')
# without GQ in the subject, you just get an auto response
_('{{law_used_full}} request GQ - {{title}}', :law_used_full => law_used_human(:full),
:title => subject_title)
else
_('{{law_used_full}} request - {{title}}', :law_used_full => law_used_human(:full),
:title => subject_title)
end
end
alias_method :orig_late_calculator, :late_calculator
def late_calculator
@late_calculator ||=
if public_body.has_tag?('school')
SchoolLateCalculator.new
else
orig_late_calculator
end
end
end
PublicBody.class_eval do
# Return the domain part of an email address, canonicalised and with common
# extra UK Government server name parts removed.
#
# TODO: Extract to library class
def self.extract_domain_from_email(email)
email =~ /@(.*)/
if $1.nil?
return nil
end
# take lower case
ret = $1.downcase
# remove special email domains for UK Government addresses
%w(gsi x pnn).each do |subdomain|
if ret =~ /.*\.*#{ subdomain }\.*.*\.gov\.uk$/
ret.sub!(".#{ subdomain }.", '.')
end
end
return ret
end
def is_school?
has_tag?('school')
end
end
# Add survey methods to RequestMailer
RequestMailer.class_eval do
def survey_alert(info_request)
user = info_request.user
post_redirect = PostRedirect.new(
:uri => survey_url,
:user_id => user.id)
post_redirect.save!
@url = confirm_url(:email_token => post_redirect.email_token)
headers('Return-Path' => blackhole_email, 'Reply-To' => contact_from_name_and_email, # not much we can do if the user's email is broken
'Auto-Submitted' => 'auto-generated', # http://tools.ietf.org/html/rfc3834
'X-Auto-Response-Suppress' => 'OOF')
@info_request = info_request
mail(:to => user.name_and_email,
:from => contact_from_name_and_email,
:subject => "Can you help us improve WhatDoTheyKnow?")
end
class << self
# Send an email with a link to the survey two weeks after a request was made,
# if the user has not already completed the survey.
def alert_survey
# Exclude requests made by users who have already been alerted about the survey
info_requests = InfoRequest.where(
" created_at between now() - '2 weeks + 1 day'::interval and now() - '2 weeks'::interval" +
" and user_id is not null" +
" and not exists (" +
" select *" +
" from user_info_request_sent_alerts" +
" where user_id = info_requests.user_id" +
" and alert_type = 'survey_1'" +
" )"
).includes(:user)
# TODO: change the initial query to iterate over users rather
# than info_requests rather than using an array to check whether
# we're about to send multiple emails to the same user_id
sent_to = []
for info_request in info_requests
# Exclude users who have already completed the survey or
# have already been sent a survey email in this run
logger.debug "[alert_survey] Considering #{info_request.user.url_name}"
next if info_request.user.survey.already_done? || sent_to.include?(info_request.user_id)
store_sent = UserInfoRequestSentAlert.new
store_sent.info_request = info_request
store_sent.user = info_request.user
store_sent.alert_type = 'survey_1'
store_sent.info_request_event_id = info_request.info_request_events[0].id
sent_to << info_request.user_id
RequestMailer.survey_alert(info_request).deliver
store_sent.save!
end
end
def alert_new_response_reminders_with_alert_survey
alert_new_response_reminders_without_alert_survey
alert_survey if AlaveteliConfiguration::send_survey_mails
end
alias_method_chain :alert_new_response_reminders, :alert_survey
end
end
ContactValidator.class_eval do
attr_accessor :understand
validates_acceptance_of :understand,
:message => N_("Please confirm that you " \
"understand that WhatDoTheyKnow " \
"is not run by the government, " \
"and the WhatDoTheyKnow " \
"volunteers cannot help you " \
"with personal matters relating " \
"to government services.")
end
end