-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathcountry_select.rb
49 lines (42 loc) · 1.3 KB
/
country_select.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
module SmartAnswer
module Question
class CountrySelect < Base
def initialize(flow, name, options = {}, &block)
@exclude_countries = options.delete(:exclude_countries)
@include_uk = options.delete(:include_uk)
@additional_countries = options.delete(:additional_countries)
super
end
def options
country_list
end
def country_list
@countries ||= load_countries
end
def country_in(countries)
->(response) { countries.include?(response) }
end
def valid_option?(option)
options.map { |v| v.slug }.include? (option.to_s)
end
def parse_input(raw_input)
raise SmartAnswer::InvalidResponse, "Illegal option #{raw_input} for #{name}", caller unless valid_option?(raw_input)
super
end
private
def load_countries
countries = WorldLocation.all
unless @include_uk
countries = countries.reject { |c| c.slug == 'united-kingdom' }
end
if @exclude_countries
countries = countries.reject { |c| @exclude_countries.include?(c.slug) }
end
if @additional_countries
countries = (countries + @additional_countries).sort { |a, b| a.name <=> b.name }
end
countries
end
end
end
end