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
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
|
# app/helpers/link_to_helper.rb:
# This module is included into all controllers via controllers/application.rb
# -
#
# Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved.
# Email: francis@mysociety.org; WWW: http://www.mysociety.org/
#
# $Id: link_to_helper.rb,v 1.52 2009-04-08 05:34:51 francis Exp $
module LinkToHelper
# Links to various models
# Requests
def request_url(info_request, extra_params={})
params = {:url_title => info_request.url_title, :only_path => true}
return show_request_url(params.merge(extra_params))
end
def request_link(info_request, cls=nil )
link_to h(info_request.title), request_url(info_request), :class => cls
end
def request_admin_url(info_request)
return admin_url('request/show/' + info_request.id.to_s)
end
def request_admin_link(info_request, name="admin", cls=nil)
link_to name, request_admin_url(info_request), :class => cls
end
def request_both_links(info_request)
link_to(h(info_request.title), main_url(request_url(info_request))) + " (" + link_to("admin", request_admin_url(info_request)) + ")"
end
def request_similar_url(info_request)
return similar_request_url(:url_title => info_request.url_title, :only_path => true)
end
def request_details_url(info_request)
return details_request_url(:url_title => info_request.url_title, :only_path => true)
end
# Incoming / outgoing messages
def incoming_message_url(incoming_message)
return request_url(incoming_message.info_request)+"#incoming-"+incoming_message.id.to_s
end
def outgoing_message_url(outgoing_message)
return request_url(outgoing_message.info_request)+"#outgoing-"+outgoing_message.id.to_s
end
def comment_url(comment)
return request_url(comment.info_request)+"#comment-"+comment.id.to_s
end
# Respond to request
def respond_to_last_url(info_request)
last_response = info_request.get_last_response
if last_response.nil?
respond_url = show_response_no_followup_url(:id => info_request.id)
else
respond_url = show_response_url(:id => info_request.id, :incoming_message_id => last_response.id)
end
return respond_url
end
# Public bodies
def public_body_url(public_body)
public_body.url_name.nil? ? '' : show_public_body_url(:url_name => public_body.url_name, :only_path => true)
end
def public_body_link_short(public_body)
link_to h(public_body.short_or_long_name), public_body_url(public_body)
end
def public_body_link(public_body, cls=nil)
link_to h(public_body.name), public_body_url(public_body), :class => cls
end
def public_body_link_absolute(public_body) # e.g. for in RSS
link_to h(public_body.name), main_url(public_body_url(public_body))
end
def public_body_admin_url(public_body)
return admin_url('body/show/' + public_body.id.to_s)
end
def public_body_both_links(public_body)
link_to(h(public_body.name), main_url(public_body_url(public_body))) + " (" + link_to("admin", public_body_admin_url(public_body)) + ")"
end
def list_public_bodies_default
list_public_bodies_url(:tag => 'all')
end
# Users
def user_url(user)
return show_user_url(:url_name => user.url_name, :only_path => true)
end
def user_link(user, cls=nil)
link_to h(user.name), user_url(user), :class => cls
end
def user_link_absolute(user)
link_to h(user.name), main_url(user_url(user))
end
def request_user_link_absolute(request)
if request.is_external?
request.external_user_name || _("Anonymous user")
else
user_link_absolute(request.user)
end
end
def user_or_you_link(user)
if @user && user == @user
link_to h("you"), user_url(user)
else
link_to h(user.name), user_url(user)
end
end
def user_or_you_capital(user)
if @user && user == @user
return h("You")
else
return h(user.name)
end
end
def user_or_you_capital_link(user)
link_to user_or_you_capital(user), user_url(user)
end
def user_admin_url(user)
return admin_url('user/show/' + user.id.to_s)
end
def user_admin_link(user, name="admin", cls=nil)
link_to name, user_admin_url(user), :class => cls
end
def user_both_links(user)
link_to(h(user.name), main_url(user_url(user))) + " (" + link_to("admin", user_admin_url(user)) + ")"
end
# Tracks. feed can be 'track' or 'feed'
def do_track_url(track_thing, feed = 'track')
if track_thing.track_type == 'request_updates'
track_request_url(:url_title => track_thing.info_request.url_title, :feed => feed)
elsif track_thing.track_type == 'all_new_requests'
track_list_url(:view => 'recent', :feed => feed)
elsif track_thing.track_type == 'all_successful_requests'
track_list_url(:view => 'successful', :feed => feed)
elsif track_thing.track_type == 'public_body_updates'
track_public_body_url(:url_name => track_thing.public_body.url_name, :feed => feed)
elsif track_thing.track_type == 'user_updates'
track_user_url(:url_name => track_thing.tracked_user.url_name, :feed => feed)
elsif track_thing.track_type == 'search_query'
track_search_url(:query_array => track_thing.track_query, :feed => feed)
else
raise "unknown tracking type " + track_thing.track_type
end
end
# General pages.
def search_url(query, params = nil)
if query.kind_of?(Array)
query = query - ["", nil]
query = query.join("/")
end
routing_info = {:controller => 'general',
:action => 'search',
:combined => query,
:view => nil}
if !params.nil?
routing_info = params.merge(routing_info)
end
url = url_for(routing_info)
# Here we can't escape the slashes, as RFC 2396 doesn't allow slashes
# within a path component. Rails is assuming when generating URLs that
# either there aren't slashes, or we are in a query part where you can
# have escaped slashes. Apache complains if you do include slashes
# within a path component.
# See http://www.webmasterworld.com/apache/3279075.htm
# and also 3.3 of http://www.ietf.org/rfc/rfc2396.txt
# It turns out this is a regression in Rails 2.1, caused by this bug fix:
# http://rails.lighthouseapp.com/projects/8994/tickets/144-patch-bug-in-rails-route-globbing
url = url.gsub("%2F", "/")
return url
end
def search_link(query, variety_postfix = nil, sort_postfix = nil, advanced = nil)
link_to h(query), search_url(query)
end
# Admin pages
def admin_url(relative_path)
admin_url_prefix = MySociety::Config.get("ADMIN_BASE_URL", "")
admin_url_prefix = admin_general_index_path+"/" if admin_url_prefix.empty?
return admin_url_prefix + relative_path
end
# About page URLs
def about_url
return help_general_url(:action => 'about')
end
def unhappy_url(info_request = nil)
if info_request.nil?
return help_general_url(:action => 'unhappy')
else
return help_unhappy_url(:url_title => info_request.url_title)
end
end
def main_url(relative_path, append = nil)
url_prefix = "http://" + MySociety::Config.get("DOMAIN", '127.0.0.1:3000')
url = url_prefix + relative_path
if !append.nil?
begin
env = Rack::MockRequest.env_for(url)
req = Rack::Request.new(env)
req.path_info += append
url = req.url
rescue URI::InvalidURIError
# don't append to it
end
end
return url
end
# Basic date format
def simple_date(date)
date_format = _("simple_date_format")
date_format = :long if date_format == "simple_date_format"
return I18n.l(date.to_date, :format => date_format)
end
def simple_time(date)
return date.strftime("%H:%M:%S").strip
end
def year_from_date(date)
return date.strftime("%Y").strip
end
#I18n locale switcher
def locale_switcher(locale, params)
params['locale'] = locale
return url_for(params)
end
end
|