Skip to content

Commit ac4c59a

Browse files
authoredOct 30, 2020
Merge pull request #4653 from alphagov/refactor-arrested-abroad
Move arrested abroad flow logic to calculator class
2 parents d3dc2d9 + ac4cc60 commit ac4c59a

File tree

6 files changed

+181
-166
lines changed

6 files changed

+181
-166
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,101 @@
11
module SmartAnswer::Calculators
22
class ArrestedAbroad
33
# created for the help-if-you-are-arrested-abroad calculator
4-
attr_reader :data
5-
6-
def initialize
7-
@data = self.class.prisoner_packs
8-
end
9-
10-
def generate_url_for_download(country, field, text)
11-
country_data = @data.find { |c| c["slug"] == country }
12-
return "" unless country_data
13-
14-
url = country_data[field]
15-
output = []
16-
if url
17-
urls = url.split(" ")
18-
urls.each do |u|
19-
new_link = "- [#{text}](#{u})"
20-
new_link += '{:rel="external"}' if u.include? "http"
21-
output.push(new_link)
22-
end
23-
output.join("\n")
24-
else
25-
""
4+
attr_reader :country
5+
6+
PRISONER_PACKS = YAML.load_file(Rails.root.join("config/smart_answers/prisoner_packs.yml")).freeze
7+
COUNTRIES_WITH_REGIONS = %w[cyprus].freeze
8+
COUNTRIES_WITHOUT_TRANFSERS_BACK = %w[austria belgium croatia denmark finland hungary italy latvia luxembourg malta netherlands slovakia].freeze
9+
10+
def initialize(country)
11+
@country = country
12+
end
13+
14+
def country_data
15+
@country_data ||= PRISONER_PACKS.find { |c| c["slug"] == country }
16+
end
17+
18+
def generate_url_for_download(field, text)
19+
return "" unless country_data && country_data[field]
20+
21+
urls = country_data[field].split(" ")
22+
output = urls.map do |url|
23+
new_link = "- [#{text}](#{url})"
24+
new_link += '{:rel="external"}' if url.include? "http"
25+
new_link
2626
end
27+
output.join("\n")
28+
end
29+
30+
def get_country_regions
31+
country_data["regions"]
32+
end
33+
34+
def location
35+
@location ||= WorldLocation.find(country)
36+
raise InvalidResponse unless @location
37+
38+
@location
39+
end
40+
41+
def organisation
42+
location.fco_organisation
43+
end
44+
45+
def country_name
46+
location.name
47+
end
48+
49+
def pdf
50+
generate_url_for_download("pdf", "Prisoner pack for #{country_name}")
2751
end
2852

29-
def self.prisoner_packs
30-
@prisoner_packs ||= YAML.load_file(Rails.root.join("config/smart_answers/prisoner_packs.yml"))
53+
def doc
54+
generate_url_for_download("doc", "Prisoner pack for #{country_name}")
3155
end
3256

33-
def countries_with_regions
34-
%w[cyprus]
57+
def benefits
58+
generate_url_for_download("benefits", "Benefits or legal aid in #{country_name}")
59+
end
60+
61+
def prison
62+
generate_url_for_download("prison", "Information on prisons and prison procedures in #{country_name}")
63+
end
64+
65+
def judicial
66+
generate_url_for_download("judicial", "Information on the judicial system and procedures in #{country_name}")
67+
end
68+
69+
def police
70+
generate_url_for_download("police", "Information on the police and police procedures in #{country_name}")
71+
end
72+
73+
def consul
74+
generate_url_for_download("consul", "Consul help available in #{country_name}")
75+
end
76+
77+
def lawyer
78+
generate_url_for_download("lawyer", "English speaking lawyers and translators/interpreters in #{country_name}")
79+
end
80+
81+
def has_extra_downloads
82+
extra_downloads = [police, judicial, consul, prison, lawyer, benefits, doc, pdf]
83+
84+
extra_downloads.any?(&:present?) || COUNTRIES_WITH_REGIONS.include?(country)
85+
end
86+
87+
def region_downloads
88+
return "" unless COUNTRIES_WITH_REGIONS.include?(country)
89+
90+
links = get_country_regions.values.map do |region|
91+
"- [#{region['url_text']}](#{region['link']})"
92+
end
93+
94+
links.join("\n")
3595
end
3696

37-
def get_country_regions(slug)
38-
@data.find { |c| c["slug"] == slug }["regions"]
97+
def transfer_back
98+
COUNTRIES_WITHOUT_TRANFSERS_BACK.exclude?(country)
3999
end
40100
end
41101
end

‎lib/smart_answer_flows/help-if-you-are-arrested-abroad.rb

+3-78
Original file line numberDiff line numberDiff line change
@@ -7,76 +7,13 @@ def define
77
status :published
88
satisfies_need "de7bf117-408b-4b26-8e08-7aa51415b4e2"
99

10-
arrested_calc = Calculators::ArrestedAbroad.new
1110
exclude_countries = %w[holy-see british-antarctic-territory]
1211
british_overseas_territories = %w[anguilla bermuda british-indian-ocean-territory british-virgin-islands cayman-islands falkland-islands gibraltar montserrat pitcairn-island st-helena-ascension-and-tristan-da-cunha south-georgia-and-the-south-sandwich-islands turks-and-caicos-islands]
1312

1413
# Q1
1514
country_select :which_country?, exclude_countries: exclude_countries do
16-
save_input_as :country
17-
18-
calculate :location do
19-
loc = WorldLocation.find(country)
20-
raise InvalidResponse unless loc
21-
22-
loc
23-
end
24-
25-
calculate :organisation do
26-
location.fco_organisation
27-
end
28-
29-
calculate :country_name do
30-
location.name
31-
end
32-
33-
calculate :pdf do
34-
arrested_calc.generate_url_for_download(country, "pdf", "Prisoner pack for #{country_name}")
35-
end
36-
37-
calculate :doc do
38-
arrested_calc.generate_url_for_download(country, "doc", "Prisoner pack for #{country_name}")
39-
end
40-
41-
calculate :benefits do
42-
arrested_calc.generate_url_for_download(country, "benefits", "Benefits or legal aid in #{country_name}")
43-
end
44-
45-
calculate :prison do
46-
arrested_calc.generate_url_for_download(country, "prison", "Information on prisons and prison procedures in #{country_name}")
47-
end
48-
49-
calculate :judicial do
50-
arrested_calc.generate_url_for_download(country, "judicial", "Information on the judicial system and procedures in #{country_name}")
51-
end
52-
53-
calculate :police do
54-
arrested_calc.generate_url_for_download(country, "police", "Information on the police and police procedures in #{country_name}")
55-
end
56-
57-
calculate :consul do
58-
arrested_calc.generate_url_for_download(country, "consul", "Consul help available in #{country_name}")
59-
end
60-
61-
calculate :lawyer do
62-
arrested_calc.generate_url_for_download(country, "lawyer", "English speaking lawyers and translators/interpreters in #{country_name}")
63-
end
64-
65-
calculate :has_extra_downloads do
66-
[police, judicial, consul, prison, lawyer, benefits, doc, pdf].count { |x|
67-
x != ""
68-
}.positive? || arrested_calc.countries_with_regions.include?(country)
69-
end
70-
71-
calculate :region_links do
72-
links = []
73-
if arrested_calc.countries_with_regions.include?(country)
74-
regions = arrested_calc.get_country_regions(country)
75-
regions.each_value do |val|
76-
links << "- [#{val['url_text']}](#{val['link']})"
77-
end
78-
end
79-
links
15+
on_response do |response|
16+
self.calculator = Calculators::ArrestedAbroad.new(response)
8017
end
8118

8219
next_node do |response|
@@ -90,19 +27,7 @@ def define
9027
end
9128
end
9229

93-
outcome :answer_one_generic do
94-
precalculate :iran do
95-
country_name == "Iran"
96-
end
97-
98-
precalculate :transfers_back_to_uk_treaty_change_countries do
99-
%w[austria belgium croatia denmark finland hungary italy latvia luxembourg malta netherlands slovakia]
100-
end
101-
102-
precalculate :region_downloads do
103-
region_links.join("\n")
104-
end
105-
end
30+
outcome :answer_one_generic
10631

10732
outcome :answer_three_syria
10833

‎lib/smart_answer_flows/help-if-you-are-arrested-abroad/outcomes/answer_one_generic.erb

+13-13
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,27 @@
3333
- helping British prisoners apply for a transfer to UK where possible
3434
- visiting prisoners when needed
3535

36-
<% if has_extra_downloads %>
36+
<% if calculator.has_extra_downloads %>
3737
##Download prisoner packs and other helpful information
3838

3939
The FCDO has produced guides explaining the legal and prison system in different countries for British nationals arrested abroad.
4040

41-
<%= pdf %>
42-
<%= doc %>
43-
<%= benefits %>
44-
<%= judicial %>
45-
<%= prison %>
46-
<%= police %>
47-
<%= consul %>
48-
<%= lawyer %>
41+
<%= calculator.pdf %>
42+
<%= calculator.doc %>
43+
<%= calculator.benefits %>
44+
<%= calculator.judicial %>
45+
<%= calculator.prison %>
46+
<%= calculator.police %>
47+
<%= calculator.consul %>
48+
<%= calculator.lawyer %>
4949

50-
<%= region_downloads %>
50+
<%= calculator.region_downloads %>
5151
<% end %>
5252

53-
<% if iran %>
54-
<%= render partial: 'iran_downloads', locals: { transfer_back: transfers_back_to_uk_treaty_change_countries.exclude?(country) } %>
53+
<% if calculator.country_name == "Iran" %>
54+
<%= render partial: 'iran_downloads', locals: { transfer_back: calculator.transfer_back } %>
5555
<% else %>
56-
<%= render partial: 'common_downloads', locals: { transfer_back: transfers_back_to_uk_treaty_change_countries.exclude?(country) } %>
56+
<%= render partial: 'common_downloads', locals: { transfer_back: calculator.transfer_back } %>
5757
<% end %>
5858

5959
##What the FCDO and British consulate can’t do

‎test/integration/smart_answer_flows/help_if_you_are_arrested_abroad_test.rb

+30-38
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class HelpIfYouAreArrestedAbroadTest < ActiveSupport::TestCase
77
include FlowTestHelper
88

99
setup do
10-
@location_slugs = %w[aruba belgium greece iran syria democratic-republic-of-the-congo]
10+
@location_slugs = %w[aruba belgium bermuda greece iran syria democratic-republic-of-the-congo]
1111
stub_world_locations(@location_slugs)
1212
setup_for_testing_flow SmartAnswer::HelpIfYouAreArrestedAbroadFlow
1313
end
@@ -18,56 +18,41 @@ class HelpIfYouAreArrestedAbroadTest < ActiveSupport::TestCase
1818

1919
context "In a country with a prisoner pack" do
2020
context "Answering with a country without any specific downloads / information" do
21-
context "Answering Aruba" do
22-
setup do
23-
add_response :aruba
24-
end
25-
26-
should "take the user to the generic answer" do
27-
assert_current_node :answer_one_generic
28-
end
29-
30-
should "correctly calculate and store the country variables" do
31-
assert_state_variable :country, "aruba"
32-
assert_state_variable :country_name, "Aruba"
33-
end
34-
35-
should "correctly set up phrase lists" do
36-
assert_state_variable :has_extra_downloads, false
37-
end
38-
end # context: Andorra
39-
end # context: country without specific info
21+
should "take the user to the generic answer" do
22+
add_response :aruba
23+
assert_current_node :answer_one_generic
24+
end
25+
end
4026

4127
context "Answering with a country that has specific downloads / information" do
4228
context "Answering Democratic Republic of the Congo" do
43-
setup do
44-
add_response :"democratic-republic-of-the-congo"
45-
end
46-
4729
should "take the user to the generic answer" do
30+
add_response :"democratic-republic-of-the-congo"
4831
assert_current_node :answer_one_generic
49-
end
50-
51-
should "set up the country specific downloads" do
52-
assert_state_variable :has_extra_downloads, true
32+
assert_select outcome_body, "h2", /Download prisoner packs/
5333
end
5434
end
5535

5636
context "Answering Greece" do
57-
setup do
58-
add_response :greece
59-
end
60-
6137
should "take the user to the generic answer" do
38+
add_response :greece
6239
assert_current_node :answer_one_generic
63-
end
64-
65-
should "set up the country specific downloads" do
66-
assert_state_variable :has_extra_downloads, true
40+
assert_select outcome_body, "h2", /Download prisoner packs/
6741
end
6842
end
69-
end # context: country with specific info
70-
end # context: non special case
43+
end
44+
end
45+
46+
context "In Iran" do
47+
setup do
48+
add_response :iran
49+
end
50+
51+
should "take the user to the generic anwser with Iran specific downloads" do
52+
assert_current_node :answer_one_generic
53+
assert_select outcome_body, "a", /imprisoned in Iran/
54+
end
55+
end
7156

7257
context "In Syria" do
7358
setup do
@@ -78,4 +63,11 @@ class HelpIfYouAreArrestedAbroadTest < ActiveSupport::TestCase
7863
assert_current_node :answer_three_syria
7964
end
8065
end
66+
67+
context "In a British overseas territory" do
68+
setup do
69+
add_response :bermuda
70+
assert_current_node :answer_three_british_overseas_territories
71+
end
72+
end
8173
end

‎test/test_helper.rb

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class ActiveSupport::TestCase
4343
include GdsApi::TestHelpers::Imminence
4444
include GdsApi::TestHelpers::PublishingApi
4545
include GdsApi::TestHelpers::Worldwide
46+
include ActionDispatch::Assertions
4647
parallelize workers: 6
4748
end
4849

‎test/unit/calculators/arrested_abroad_calculator_test.rb

+46-9
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,76 @@
33
module SmartAnswer::Calculators
44
class ArrestedAbroadCalculatorTest < ActiveSupport::TestCase
55
context ArrestedAbroad do
6-
setup do
7-
@calc = ArrestedAbroad.new
8-
end
9-
106
context "generating a URL" do
117
should "not error if the country doesn't exist" do
128
assert_nothing_raised do
13-
@calc.generate_url_for_download("doesntexist", "pdf", "hello world")
9+
calc = ArrestedAbroad.new("doesntexist")
10+
calc.generate_url_for_download("pdf", "hello world")
1411
end
1512
end
1613

1714
should "generate link if country exists" do
18-
link = @calc.generate_url_for_download("argentina", "pdf", "Prisoner pack")
15+
calc = ArrestedAbroad.new("argentina")
16+
link = calc.generate_url_for_download("pdf", "Prisoner pack")
1917
assert_equal "- [Prisoner pack](/government/publications/argentina-prisoner-pack)", link
2018
end
2119

2220
should "not include external tag if URL is internal" do
23-
link = @calc.generate_url_for_download("israel", "pdf", "Foo")
21+
calc = ArrestedAbroad.new("israel")
22+
link = calc.generate_url_for_download("pdf", "Foo")
2423
assert_not link.include?("{:rel=\"external\"}")
2524
end
2625
end
2726

27+
context "has extra downloads" do
28+
should "return true for countries with regions" do
29+
calc = ArrestedAbroad.new("cyprus")
30+
calc.stubs(:country_name).returns("Cyprus")
31+
assert calc.has_extra_downloads
32+
end
33+
34+
should "return false if not a country with regions nor has extra download links" do
35+
calc = ArrestedAbroad.new("bermuda")
36+
calc.stubs(:country_name).returns("Bermuda")
37+
assert_not calc.has_extra_downloads
38+
end
39+
40+
should "return true if country has extra download links" do
41+
calc = ArrestedAbroad.new("australia")
42+
calc.stubs(:country_name).returns("Australia")
43+
assert calc.has_extra_downloads
44+
end
45+
end
46+
47+
context "region downloads" do
48+
should "return list of region links for countries with regions" do
49+
calc = ArrestedAbroad.new("cyprus")
50+
calc.stubs(:get_country_regions).returns({
51+
"a": { "url_text" => "Text 1", "link" => "link1" },
52+
"b": { "url_text" => "Text 2", "link" => "link2" },
53+
})
54+
assert_equal calc.region_downloads, "- [Text 1](link1)\n- [Text 2](link2)"
55+
end
56+
57+
should "return empty for countries without regions" do
58+
calc = ArrestedAbroad.new("bermuda")
59+
assert_equal calc.region_downloads, ""
60+
end
61+
end
62+
2863
context "countries with regions" do
2964
should "pull out regions of the YML for Cyprus" do
30-
resp = @calc.get_country_regions("cyprus")
65+
calc = ArrestedAbroad.new("cyprus")
66+
resp = calc.get_country_regions
3167
assert resp["north"]
3268
assert resp["north_lawyer"]
3369
assert resp["republic"]
3470
assert resp["republic_lawyers"]
3571
end
3672

3773
should "pull the regions out of the YML for Cyprus" do
38-
resp = @calc.get_country_regions("cyprus")["north"]
74+
calc = ArrestedAbroad.new("cyprus")
75+
resp = calc.get_country_regions["north"]
3976
expected = {
4077
"link" => "/government/publications/cyprus-north-prisoner-pack",
4178
"url_text" => "Prisoner pack for the north of Cyprus",

0 commit comments

Comments
 (0)
Please sign in to comment.