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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
# File 'lib/generators/doopgovuk/templates/app/controllers/demo_controller.rb', line 12
def setup_doop
@doop_controller = Doop::DoopController.new self do |doop|
load_yaml do
data = params["doop_data"]
if data != nil
if Rails.env.development? || Rails.env.test? || params.include?("harness")
next data
else
next ActiveSupport::MessageEncryptor.new(Rails.application.secrets.secret_key_base).decrypt_and_verify data if !Rails.env.development?
end
end
<<-EOS
page: {
before_you_begin: {
_page: "before_you_begin",
_nav_name: "Before you begin",
},
preamble: {
_page: "preamble",
_nav_name: "Preamble",
income_more_than_50000: {
_question: "Does you or your partner have an individual income of more than £50,000 a year ?"
},
do_you_still_want_to_apply: {
_question: "Do you still want to apply for child benefit?"
}
},
about_you: {
_page: "about_you",
_nav_name: "About You",
your_name: {
_question: "What is your name?",
_answer: {}
},
known_by_other_name: {
_question: "Have you ever been known by another surname ?"
},
previous_name: {
_question: "What name were you previously known by ?"
},
dob: {
_question: "Your date of birth"
},
your_address: {
_question: "Your address",
_answer: {}
},
lived_at_address_for_more_than_12_months: {
_question: "Have you lived at this address for more than 12 months ?"
},
last_address: {
_question: "What was your last address ?",
_answer: {}
},
your_phone_numbers: {
_question: "What numbers can we contact you on ?",
_answer: {}
},
have_nino: {
_question: "Do you have a national insurance number ?"
},
nino: {
_question: "What is your national insurance number ?"
}
},
children: {
_page: "children",
_nav_name: "Children",
how_many_birth_certs: {
_question: "How many birth certificates are you sending us?"
},
#{child_yaml}
},
declaration: {
_page: "declaration",
_nav_name: "Declaration",
}
}
EOS
end
def child_yaml
<<-EOS
child__1: {
name: {
_question: "Child's name",
_answer: {}
},
gender: {
_question: "Is this child male or female ?"
},
dob: {
_question: "Child's date of birth"
},
own_child: {
_question: "Is this child your own child ?"
}
}
EOS
end
save_yaml do |yaml|
if Rails.env.development? || Rails.env.test?
request["doop_data"] = yaml
else
crypt = ActiveSupport::MessageEncryptor.new(Rails.application.secrets.secret_key_base)
data = crypt.encrypt_and_sign(yaml)
request["doop_data"] = data
end
end
on_answer "/page/preamble/income_more_than_50000" do |question,path, params, answer|
answer_with( question, { "_summary" => answer } )
enable( "/page/preamble/do_you_still_want_to_apply", answer == 'Yes' )
end
on_answer "/page/preamble/do_you_still_want_to_apply" do |question,path, params, answer|
answer_with( question, { "_summary" => "Yes" } )
end
on_answer "/page/about_you/your_name" do |question,path, params, answer|
res = validate( answer, ["title", "firstname", "surname"] )
next res if !res.empty?
name = "#{answer['title']} #{answer['firstname']} #{answer['middlenames']} #{answer['surname']}".squish
formatted_name = name.split( " ").map{ |n| n.capitalize }.join( " ")
answer_with( question, { "_summary" => formatted_name } )
end
on_answer "/page/about_you/known_by_other_name" do |question,path, params, answer|
answer_with( question, { "_summary" => answer } )
enable( "/page/about_you/previous_name", answer == 'Yes' )
end
on_answer "/page/about_you/previous_name" do |question,path, params, answer|
res = validate( answer )
next res if !res.empty?
answer_with( question, { "_summary" => answer } )
end
on_answer "/page/about_you/dob" do |question,path, params, answer|
d = format_date answer
next { :answer_error => "Date of birth must be formated as dd/mm/yyyy" } if d.nil?
answer_with( question, { "_summary" => d } )
end
on_answer "/page/about_you/your_address" do |question,path, params, answer|
res = validate( answer, ["address1", "address2", "address3", "postcode"] )
next res if !res.empty?
a = "#{answer['address1']}, #{answer['postcode']}"
answer_with( question, { "_summary" => a } )
end
on_answer "/page/about_you/lived_at_address_for_more_than_12_months" do |question, path, params, answer|
answer_with( question, { "_summary" => answer } )
enable( "/page/about_you/last_address", answer == 'No' )
end
on_answer "/page/about_you/last_address" do |question,path, params, answer|
res = validate( answer, ["address1", "address2", "address3", "postcode"] )
next res if !res.empty?
a = "#{answer['address1']}, #{answer['postcode']}"
answer_with( question, { "_summary" => a } )
end
on_answer "/page/about_you/your_phone_numbers" do |question,path, params, answer|
res = validate( answer, ["daytime", "evening"] )
next res if !res.empty?
answer_with( question, { "_summary" => "Provided" } )
end
on_answer "/page/about_you/have_nino" do |question, path, params, answer|
answer_with( question, { "_summary" => answer } )
enable( "/page/about_you/nino", answer == 'Yes' )
end
on_answer "/page/about_you/nino" do |question, path, params, answer|
res = validate( answer )
next res if !res.empty?
answer_with( question, { "_summary" => answer } )
end
on_answer "/page/children/how_many_birth_certs" do |question,path, params, answer|
res = validate( answer )
next res if !res.empty?
next { :answer_error => "Must be a number" } if !is_number answer
answer_with( question, { "_summary" => answer } )
end
on_answer "/page/children/child__(\\d+)/name" do |question,path, params, answer|
res = validate( answer, ["firstname", "surname"] )
next res if !res.empty?
name = "#{answer['firstname']} #{answer['middlenames']} #{answer['surname']}".squish
formatted_name = name.split( " ").map{ |n| n.capitalize }.join( " ")
answer_with( question, { "_summary" => formatted_name } )
end
on_answer "/page/children/child__(\\d+)/gender" do |question,path, params, answer|
answer_with( question, { "_summary" => answer } )
end
on_answer "/page/children/child__(\\d+)/dob" do |question,path, params, answer|
d = format_date answer
next { :answer_error => "Date of birth must be formated as dd/mm/yyyy" } if d.nil?
answer_with( question, { "_summary" => d } )
end
on_answer "/page/children/child__(\\d+)/own_child" do |question,path, params, answer|
answer_with( question, { "_summary" => answer } )
end
on_answer "/page/children/child__(\\d+)" do |question,path, params, answer|
if params.include?("remove_child")
remove path
renumber "/page/children"
next
end
name = doop["#{path}/name/_answer"]
answer_with( question, { "_summary" => doop["#{path}/name/_summary"] } )
end
on_answer "/page/children" do |question, path, params, answer|
if params.include?("add_child")
add( "/page/children/child__99", YAML.load( child_yaml )["child__1"] )
renumber( "/page/children" )
next
end
answer_with( question, { "_summary" => "Provided" } )
end
on_answer "/page/declaration" do |question, path, params, answer |
{ :redirect => "https://github.com/coder36/doop" }
end
end
end
|