-
Notifications
You must be signed in to change notification settings - Fork 9
/
helpers.rb
106 lines (87 loc) · 2.73 KB
/
helpers.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
# encoding: utf-8
require "solr_wrapper"
module Helpers
HIGHLIGHT_START = SolrWrapper::HIGHLIGHT_START
HIGHLIGHT_END = SolrWrapper::HIGHLIGHT_END
include Rack::Utils
alias_method :h, :escape_html
def proposition
settings.slimmer_headers[:proposition]
end
def capped_search_set_size
[@results.count, (settings.top_results + settings.max_more_results)].min
end
def base_url
return "https://www.gov.uk" if ENV['FACTER_govuk_platform'] == 'production'
"https://www.#{ENV['FACTER_govuk_platform']}.alphagov.co.uk"
end
def top_results
results = non_recommended_results[0..(settings.top_results-1)]
results.empty? ? nil : results
end
def more_results
non_recommended_results[settings.top_results..(settings.top_results + settings.max_more_results-1)]
end
def recommended_results
@results.select { |r| r.format == settings.recommended_format }[0, settings.max_recommended_results]
end
def non_recommended_results
@results.select { |r| r.format != settings.recommended_format }
end
def pluralize(singular, plural)
@results.count == 1 ? singular : plural
end
def formatted_format_name(name)
alt = settings.format_name_alternatives[name]
return alt if alt
return "#{name.capitalize}s"
end
def include(name)
begin
File.open("views/_#{name}.html").read
rescue Errno::ENOENT
end
end
def simple_json_result(ok)
content_type :json
if ok
result = "OK"
else
result = "error"
status 500
end
JSON.dump("result" => result)
end
def apply_highlight(s)
s = s.strip
return "" if s.empty?
just_text = s.gsub(/#{HIGHLIGHT_START}|#{HIGHLIGHT_END}/, "")
[ just_text.match(/\A[[:upper:]]/) ? "" : "… ",
s.gsub(HIGHLIGHT_START, %{<strong class="highlight">}).
gsub(HIGHLIGHT_END, %{</strong>}),
just_text.match(/[\.\?!]\z/) ? "" : " …"
].join
end
def map_section_name(slug)
map = {
"life-in-the-uk" => "Life in the UK",
"housing-benefits-grants-and-schemes" => "Housing benefits, grants and schemes",
"work-related-benefits-and-schemes" => "Work-related benefits and schemes",
"buying-selling-a-vehicle" => "Buying/selling a vehicle",
"owning-a-car-motorbike" => "Owning a car/motorbike",
"council-and-housing-association-homes" => "Council and Housing Association homes",
"animals-food-and-plants" => "Animals, food and plants",
"mot" => "MOT"
}
return map[slug] ? map[slug] : false
end
def humanize_section_name(slug)
slug.gsub('-', ' ').capitalize
end
def formatted_section_name(slug)
map_section_name(slug) ? map_section_name(slug) : humanize_section_name(slug)
end
end
class HelperAccessor
include Helpers
end