/
page_title_helper.rb
79 lines (63 loc) · 1.58 KB
/
page_title_helper.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
module PageTitleHelper
class PageTitleBuilder
class << self
def build(template)
new(template).build
end
end
attr_reader :template
delegate :assigns, to: :template
delegate :params, to: :template
delegate :translate, to: :template
delegate :[], :has_key?, to: :assigns
def initialize(template)
@template = template
end
def build
translate key, options
end
private
def controller
@controller ||= params[:controller].tr('/', '_')
end
def action
@action ||= params[:action]
end
def key
@key ||= :"#{controller}.#{action}"
end
def options
{}.tap do |opts|
opts[:scope] = :page_titles
opts[:default] = [:"#{controller}.default", :default]
opts[:constituency] = constituency.name if constituency?
opts[:postcode] = formatted_postcode if postcode?
if postcode?
opts[:count] = 1
else
opts[:count] = 0
end
if petition?
opts[:petition] = petition.action
unless petition.is_a?(Archived::Petition)
opts[:creator] = petition.creator.name
end
end
end
end
%w[constituency petition postcode].each do |object|
define_method :"#{object}?" do
send(:"#{object}").present?
end
define_method :"#{object}" do
send(:[], object)
end
end
def formatted_postcode
postcode.gsub(/\A(.+)(.{3})\z/, '\1 \2')
end
end
def page_title
PageTitleBuilder.build(self)
end
end