Anonymous avatar Anonymous committed b1eca65

[svn] * Finished sync'ing Rails ports (urls, tags) in WebHelpers. Closes #69.
url and prototype tests updated, url helpers updated to handle method
argument.

Comments (0)

Files changed (5)

 WebHelpers ChangeLog
 
 0.2 (**svn**)
+* Finished sync'ing Rails ports (urls, tags) in WebHelpers. Closes #69.
+  url and prototype tests updated, url helpers updated to handle method
+  argument.
 * Sync'd scriptaculous helper.
 * Sync'd javascript, prototype helpers and prototype.js to latest Rails
   modifications. Added more prototype tests.

tests/test_prototype.py

             link_to_remote("Remote outpost", dict(success="alert(request.reponseText)",url='http://www.example.com/whatnot')))
         self.assertEqual("""<a href="#" onclick="new Ajax.Request('http://www.example.com/whatnot', {asynchronous:true, evalScripts:true, onFailure:function(request){alert(request.reponseText)}}); return false;">Remote outpost</a>""",
             link_to_remote("Remote outpost", dict(failure="alert(request.reponseText)",url='http://www.example.com/whatnot')))
+        self.assertEqual("<a href=\"#\" onclick=\"new Ajax.Request('http://www.example.com/whatnot?a=10&amp;b=20', {asynchronous:true, evalScripts:true, onFailure:function(request){alert(request.reponseText)}}); return false;\">Remote outpost</a>",
+            link_to_remote("Remote outpost", dict(failure="alert(request.reponseText)",url ='http://www.example.com/whatnot?a=10&b=20')))
 
     def test_periodically_call_remote(self):
         self.assertEqual("""<script type="text/javascript">\n//<![CDATA[\nnew PeriodicalExecuter(function() {new Ajax.Updater('schremser_bier', 'http://www.example.com/mehr_bier', {asynchronous:true, evalScripts:true})}, 10)\n//]]>\n</script>""",

tests/test_url.py

                        confirm="Are you serious?"))
     
     def test_link_tag_using_post_javascript(self):
-        self.assertEqual("<a href=\"http://www.example.com\" onclick=\"f = document.createElement('form'); document.body.appendChild(f); f.method = 'POST'; f.action = this.href; f.submit();return false;\">Hello</a>",
+        self.assertEqual("<a href=\"http://www.example.com\" onclick=\"var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;f.submit();return false;\">Hello</a>",
                link_to("Hello", "http://www.example.com", post=True))
     
+    def test_link_tag_using_delete_javascript(self):
+        self.assertEqual("<a href=\"http://www.example.com\" onclick=\"var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); m.setAttribute('value', 'delete'); f.appendChild(m);f.submit();return false;\">Destroy</a>",
+                link_to("Destroy", "http://www.example.com", method='delete'))
+
     def test_link_tag_using_post_javascript_and_confirm(self):
-        self.assertEqual("<a href=\"http://www.example.com\" onclick=\"if (confirm('Are you serious?')) { f = document.createElement('form'); document.body.appendChild(f); f.method = 'POST'; f.action = this.href; f.submit(); };return false;\">Hello</a>",
+        self.assertEqual("<a href=\"http://www.example.com\" onclick=\"if (confirm('Are you serious?')) { var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;f.submit(); };return false;\">Hello</a>",
                link_to("Hello", "http://www.example.com", post=True, confirm="Are you serious?"))
     
     def test_mail_to(self):

webhelpers/rails/tags.py

-"""
-Tag Helpers
-"""
+"""Tag Helpers"""
+# Last synced with Rails copy at Revision 2543 on Aug 20th, 2006.
+
 from webhelpers.util import html_escape
 import re
 

webhelpers/rails/urls.py

-"""
-URL Helpers
-"""
+"""URL Helpers"""
+# Last synced with Rails copy at Revision 4374 on Aug 20th, 2006.
+
 import cgi
 import urllib
 
         >>> link_to("Delete this page", url(action="destroy", id=4), confirm="Are you sure?")
         >>> link_to("Help", url(action="help"), popup=True)
         >>> link_to("Busy loop", url(action="busy"), popup=['new_window', 'height=300,width=600'])
-        >>> link_to("Destroy account", url(action="destroy"), confirm="Are you sure?", post=True)
+        >>> link_to("Destroy account", url(action="destroy"), confirm="Are you sure?", method='delete')
     """
     if html_options:
         html_options = convert_options_to_javascript(**html_options)
             curopts.update(dict(parse_querystring(environ)))
     return url_for(**curopts)
 
-def convert_options_to_javascript(confirm=None, popup=None, post=None, **html_options):
-    if popup and post:
+def convert_options_to_javascript(confirm=None, popup=None, post=None, method=None, **html_options):
+    if method: method = method.lower()
+    
+    if post and not method:
+        method = 'post'
+    
+    if popup and method:
         raise "You can't use popup and post in the same link"
     elif confirm and popup:
         oc = "if (%s) { %s };return false;" % (confirm_javascript_function(confirm), 
                                                popup_javascript_function(popup))
-    elif confirm and post:
+    elif confirm and method:
         oc = "if (%s) { %s };return false;" % (confirm_javascript_function(confirm),
-                                               post_javascript_function())
+                                               method_javascript_function(method))
     elif confirm:
         oc = "return %s;" % confirm_javascript_function(confirm)
-    elif post:
-        oc = "%sreturn false;" % post_javascript_function()
+    elif method:
+        oc = "%sreturn false;" % method_javascript_function(method)
     elif popup:
         oc = popup_javascript_function(popup) + 'return false;'
     else:
     else:
         return "window.open(this.href);"
 
-def post_javascript_function():
-    return "f = document.createElement('form'); document.body.appendChild(f); f.method = 'POST'; f.action = this.href; f.submit();"
+def method_javascript_function(method):
+    submit_function = "var f = document.createElement('form'); f.style.display = 'none'; " + \
+        "this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;"
+    
+    if method != 'post':
+        submit_function += "var m = document.createElement('input'); m.setAttribute('type', 'hidden'); "
+        submit_function += "m.setAttribute('name', '_method'); m.setAttribute('value', '%s'); f.appendChild(m);" % method
+    
+    return submit_function + "f.submit();"
 
 
 def mail_to(email_address, name=None, cc=None, bcc=None, subject=None, 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.