public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Removed handling of string parameter in link_to to have all URL generation 
done by url_for

Signed-off-by: Joshua Peek <josh@joshpeek.com>
clemens (author)
Sat Jul 19 11:06:43 -0700 2008
josh (committer)
Sat Jul 19 11:06:43 -0700 2008
commit  c98692abcfd3576ee5fcde3910330d1eb39a18a5
tree    97812fd62532a9881ce5327e7b0c80e470d8eceb
parent  938caf4e6b2448b45939d36824794ea0aa5e1804
...
3
4
5
6
7
 
 
8
9
10
...
33
34
35
36
37
 
 
38
39
40
...
62
63
64
 
 
 
 
 
 
 
 
 
 
 
65
66
67
 
 
 
 
68
69
70
71
72
73
74
 
 
 
 
75
76
77
 
78
79
80
...
116
117
118
119
120
 
 
121
122
123
...
141
142
143
144
145
 
 
146
147
148
...
197
198
199
200
 
201
202
 
203
204
205
...
211
212
213
214
215
216
217
218
219
220
221
 
222
223
224
...
228
229
230
231
 
232
233
234
...
260
261
262
263
 
264
265
266
...
286
287
288
289
 
290
291
292
293
294
 
295
296
297
...
309
310
311
312
 
313
314
315
...
336
337
338
339
 
340
341
342
 
 
343
344
345
 
 
346
347
348
...
359
360
361
362
 
363
364
365
 
366
367
368
...
391
392
393
394
 
395
396
397
 
398
399
400
...
431
432
433
434
 
435
436
437
 
438
439
440
 
441
442
443
 
444
445
446
447
 
448
449
450
...
3
4
5
 
 
6
7
8
9
10
...
33
34
35
 
 
36
37
38
39
40
...
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
...
130
131
132
 
 
133
134
135
136
137
...
155
156
157
 
 
158
159
160
161
162
...
211
212
213
 
214
215
 
216
217
218
219
...
225
226
227
 
 
 
 
 
 
 
 
228
229
230
231
...
235
236
237
 
238
239
240
241
...
267
268
269
 
270
271
272
273
...
293
294
295
 
296
297
298
299
300
 
301
302
303
304
...
316
317
318
 
319
320
321
322
...
343
344
345
 
346
347
 
 
348
349
350
 
 
351
352
353
354
355
...
366
367
368
 
369
370
371
 
372
373
374
375
...
398
399
400
 
401
402
403
 
404
405
406
407
...
438
439
440
 
441
442
443
 
444
445
446
 
447
448
449
 
450
451
452
453
 
454
455
456
457
0
@@ -3,8 +3,8 @@ require 'action_view/helpers/javascript_helper'
0
 module ActionView
0
   module Helpers #:nodoc:
0
     # Provides a set of methods for making links and getting URLs that
0
- # depend on the routing subsystem (see ActionController::Routing).
0
- # This allows you to use the same format for links in views
0
+ # depend on the routing subsystem (see ActionController::Routing).
0
+ # This allows you to use the same format for links in views
0
     # and controllers.
0
     module UrlHelper
0
       include JavaScriptHelper
0
@@ -33,8 +33,8 @@ module ActionView
0
       #
0
       # If you instead of a hash pass a record (like an Active Record or Active Resource) as the options parameter,
0
       # you'll trigger the named route for that record. The lookup will happen on the name of the class. So passing
0
- # a Workshop object will attempt to use the workshop_path route. If you have a nested route, such as
0
- # admin_workshop_path you'll have to call that explicitly (it's impossible for url_for to guess that route).
0
+ # a Workshop object will attempt to use the workshop_path route. If you have a nested route, such as
0
+ # admin_workshop_path you'll have to call that explicitly (it's impossible for url_for to guess that route).
0
       #
0
       # ==== Examples
0
       # <%= url_for(:action => 'index') %>
0
@@ -62,19 +62,33 @@ module ActionView
0
       # <%= url_for(@workshop) %>
0
       # # calls @workshop.to_s
0
       # # => /workshops/5
0
+ #
0
+ # <%= url_for("http://www.example.com") %>
0
+ # # => http://www.example.com
0
+ #
0
+ # <%= url_for(:back) %>
0
+ # # if request.env["HTTP_REFERER"] is set to "http://www.example.com"
0
+ # # => http://www.example.com
0
+ #
0
+ # <%= url_for(:back) %>
0
+ # # if request.env["HTTP_REFERER"] is not set or is blank
0
+ # # => javascript:history.back()
0
       def url_for(options = {})
0
         options ||= {}
0
- case options
0
+ url = case options
0
+ when String
0
+ escape = true
0
+ options
0
         when Hash
0
           options = { :only_path => options[:host].nil? }.update(options.symbolize_keys)
0
           escape = options.key?(:escape) ? options.delete(:escape) : true
0
- url = @controller.send(:url_for, options)
0
- when String
0
- escape = true
0
- url = options
0
+ @controller.send(:url_for, options)
0
+ when :back
0
+ escape = false
0
+ @controller.request.env["HTTP_REFERER"] || 'javascript:history.back()'
0
         else
0
           escape = false
0
- url = polymorphic_path(options)
0
+ polymorphic_path(options)
0
         end
0
 
0
         escape ? escape_once(url) : url
0
@@ -116,8 +130,8 @@ module ActionView
0
       #
0
       # Note that if the user has JavaScript disabled, the request will fall back
0
       # to using GET. If <tt>:href => '#'</tt> is used and the user has JavaScript disabled
0
- # clicking the link will have no effect. If you are relying on the POST
0
- # behavior, your should check for it in your controller's action by using the
0
+ # clicking the link will have no effect. If you are relying on the POST
0
+ # behavior, your should check for it in your controller's action by using the
0
       # request object's methods for <tt>post?</tt>, <tt>delete?</tt> or <tt>put?</tt>.
0
       #
0
       # You can mix and match the +html_options+ with the exception of
0
@@ -141,8 +155,8 @@ module ActionView
0
       #
0
       # link_to "Profile", :controller => "profiles", :action => "show", :id => @profile
0
       # # => <a href="/profiles/show/1">Profile</a>
0
- #
0
- # Similarly,
0
+ #
0
+ # Similarly,
0
       #
0
       # link_to "Profiles", profiles_path
0
       # # => <a href="/profiles">Profiles</a>
0
@@ -197,9 +211,9 @@ module ActionView
0
       # # => <a href="/images/9" onclick="window.open(this.href,'new_window_name','height=300,width=600');return false;">View Image</a>
0
       #
0
       # link_to "Delete Image", @image, :confirm => "Are you sure?", :method => :delete
0
- # # => <a href="/images/9" onclick="if (confirm('Are you sure?')) { var f = document.createElement('form');
0
+ # # => <a href="/images/9" onclick="if (confirm('Are you sure?')) { var f = document.createElement('form');
0
       # f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;
0
- # var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method');
0
+ # var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method');
0
       # m.setAttribute('value', 'delete'); f.appendChild(m);f.submit(); };return false;">Delete Image</a>
0
       def link_to(*args, &block)
0
         if block_given?
0
@@ -211,14 +225,7 @@ module ActionView
0
           options = args.second || {}
0
           html_options = args.third
0
 
0
- url = case options
0
- when String
0
- options
0
- when :back
0
- @controller.request.env["HTTP_REFERER"] || 'javascript:history.back()'
0
- else
0
- self.url_for(options)
0
- end
0
+ url = url_for(options)
0
 
0
           if html_options
0
             html_options = html_options.stringify_keys
0
@@ -228,7 +235,7 @@ module ActionView
0
           else
0
             tag_options = nil
0
           end
0
-
0
+
0
           href_attr = "href=\"#{url}\"" unless href
0
           "<a #{href_attr}#{tag_options}>#{name || url}</a>"
0
         end
0
@@ -260,7 +267,7 @@ module ActionView
0
       # * <tt>:confirm</tt> - This will add a JavaScript confirm
0
       # prompt with the question specified. If the user accepts, the link is
0
       # processed normally, otherwise no action is taken.
0
- #
0
+ #
0
       # ==== Examples
0
       # <%= button_to "New", :action => "new" %>
0
       # # => "<form method="post" action="/controller/new" class="button-to">
0
@@ -286,12 +293,12 @@ module ActionView
0
         end
0
 
0
         form_method = method.to_s == 'get' ? 'get' : 'post'
0
-
0
+
0
         request_token_tag = ''
0
         if form_method == 'post' && protect_against_forgery?
0
           request_token_tag = tag(:input, :type => "hidden", :name => request_forgery_protection_token.to_s, :value => form_authenticity_token)
0
         end
0
-
0
+
0
         if confirm = html_options.delete("confirm")
0
           html_options["onclick"] = "return #{confirm_javascript_function(confirm)};"
0
         end
0
@@ -309,7 +316,7 @@ module ActionView
0
       # Creates a link tag of the given +name+ using a URL created by the set of
0
       # +options+ unless the current request URI is the same as the links, in
0
       # which case only the name is returned (or the given block is yielded, if
0
- # one exists). You can give link_to_unless_current a block which will
0
+ # one exists). You can give link_to_unless_current a block which will
0
       # specialize the default behavior (e.g., show a "Start Here" link rather
0
       # than the link's text).
0
       #
0
@@ -336,13 +343,13 @@ module ActionView
0
       # </ul>
0
       #
0
       # The implicit block given to link_to_unless_current is evaluated if the current
0
- # action is the action given. So, if we had a comments page and wanted to render a
0
+ # action is the action given. So, if we had a comments page and wanted to render a
0
       # "Go Back" link instead of a link to the comments page, we could do something like this...
0
- #
0
- # <%=
0
+ #
0
+ # <%=
0
       # link_to_unless_current("Comment", { :controller => 'comments', :action => 'new}) do
0
- # link_to("Go back", { :controller => 'posts', :action => 'index' })
0
- # end
0
+ # link_to("Go back", { :controller => 'posts', :action => 'index' })
0
+ # end
0
       # %>
0
       def link_to_unless_current(name, options = {}, html_options = {}, &block)
0
         link_to_unless current_page?(options), name, options, html_options, &block
0
@@ -359,10 +366,10 @@ module ActionView
0
       # # If the user is logged in...
0
       # # => <a href="/controller/reply/">Reply</a>
0
       #
0
- # <%=
0
+ # <%=
0
       # link_to_unless(@current_user.nil?, "Reply", { :action => "reply" }) do |name|
0
       # link_to(name, { :controller => "accounts", :action => "signup" })
0
- # end
0
+ # end
0
       # %>
0
       # # If the user is logged in...
0
       # # => <a href="/controller/reply/">Reply</a>
0
@@ -391,10 +398,10 @@ module ActionView
0
       # # If the user isn't logged in...
0
       # # => <a href="/sessions/new/">Login</a>
0
       #
0
- # <%=
0
+ # <%=
0
       # link_to_if(@current_user.nil?, "Login", { :controller => "sessions", :action => "new" }) do
0
       # link_to(@current_user.login, { :controller => "accounts", :action => "show", :id => @current_user })
0
- # end
0
+ # end
0
       # %>
0
       # # If the user isn't logged in...
0
       # # => <a href="/sessions/new/">Login</a>
0
@@ -431,20 +438,20 @@ module ActionView
0
       # * <tt>:bcc</tt> - Blind Carbon Copy additional recipients on the email.
0
       #
0
       # ==== Examples
0
- # mail_to "me@domain.com"
0
+ # mail_to "me@domain.com"
0
       # # => <a href="mailto:me@domain.com">me@domain.com</a>
0
       #
0
- # mail_to "me@domain.com", "My email", :encode => "javascript"
0
+ # mail_to "me@domain.com", "My email", :encode => "javascript"
0
       # # => <script type="text/javascript">eval(unescape('%64%6f%63...%6d%65%6e'))</script>
0
       #
0
- # mail_to "me@domain.com", "My email", :encode => "hex"
0
+ # mail_to "me@domain.com", "My email", :encode => "hex"
0
       # # => <a href="mailto:%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d">My email</a>
0
       #
0
- # mail_to "me@domain.com", nil, :replace_at => "_at_", :replace_dot => "_dot_", :class => "email"
0
+ # mail_to "me@domain.com", nil, :replace_at => "_at_", :replace_dot => "_dot_", :class => "email"
0
       # # => <a href="mailto:me@domain.com" class="email">me_at_domain_dot_com</a>
0
       #
0
       # mail_to "me@domain.com", "My email", :cc => "ccaddress@domain.com",
0
- # :subject => "This is an example email"
0
+ # :subject => "This is an example email"
0
       # # => <a href="mailto:me@domain.com?cc=ccaddress@domain.com&subject=This%20is%20an%20example%20email">My email</a>
0
       def mail_to(email_address, name = nil, html_options = {})
0
         html_options = html_options.stringify_keys
...
28
29
30
 
 
 
 
 
 
 
 
 
 
31
32
33
...
419
420
421
422
423
424
425
...
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
...
429
430
431
 
432
433
434
0
@@ -28,6 +28,16 @@ class UrlHelperTest < ActionView::TestCase
0
     assert_equal "http://www.example.com?a=b&amp;c=d", url_for("http://www.example.com?a=b&amp;c=d")
0
   end
0
 
0
+ def test_url_for_with_back
0
+ @controller.request = RequestMock.new("http://www.example.com/weblog/show", nil, nil, {'HTTP_REFERER' => 'http://www.example.com/referer'})
0
+ assert_equal 'http://www.example.com/referer', url_for(:back)
0
+ end
0
+
0
+ def test_url_for_with_back_and_no_referer
0
+ @controller.request = RequestMock.new("http://www.example.com/weblog/show", nil, nil, {})
0
+ assert_equal 'javascript:history.back()', url_for(:back)
0
+ end
0
+
0
   # todo: missing test cases
0
   def test_button_to_with_straight_url
0
     assert_dom_equal "<form method=\"post\" action=\"http://www.example.com\" class=\"button-to\"><div><input type=\"submit\" value=\"Hello\" /></div></form>", button_to("Hello", "http://www.example.com")
0
@@ -419,7 +429,6 @@ class LinkToUnlessCurrentWithControllerTest < ActionView::TestCase
0
     end
0
 end
0
 
0
-
0
 class Workshop
0
   attr_accessor :id, :new_record
0
 

Comments

    No one has commented yet.