Commits

Anonymous committed 4034e89

[svn] add image support to button_to
Patch by Alex Conrad
Fixes #184

Comments (0)

Files changed (3)

 WebHelpers ChangeLog
 
 0.3.1 (**svn**)
+* Added image support to button_to. Patch by Alex Conrad. Fixes #184.
 * Fix radio_button and submit_to_remote not handling unicode values.
   Fixes #235.
 * Added support for the defer attribute to javascript_include_tag. Suggested by

tests/test_url.py

         self.assertEqual("<form method=\"GET\" action=\"http://www.example.com\" class=\"button-to\"><div><input type=\"submit\" value=\"Hello\" /></div></form>",
             button_to("Hello", "http://www.example.com", method='GET'))
 
+    def test_button_to_with_img(self):
+        self.assertEqual('<form method="POST" action="/content/edit/3" class="button-to"><div><input alt="Edit" src="/images/icon_delete.gif" type="image" value="Edit" /></div></form>',
+                         button_to("Edit", url(action='edit', id=3), type='image', src='icon_delete.gif'))
+        self.assertEqual('<form method="POST" action="/content/submit/3" class="button-to"><div><input alt="Complete the form" src="/images/submit.png" type="image" value="Submit" /></div></form>',
+                         button_to("Submit", url(action='submit', id=3), type='image', src='submit', alt='Complete the form'))
+
     def test_link_tag_with_straight_url(self):
         self.assertEqual("<a href=\"http://www.example.com\">Hello</a>", link_to("Hello", "http://www.example.com"))
     

webhelpers/rails/urls.py

 from routes import url_for, request_config
 
 import tags
+from asset_tag import compute_public_path
 from javascript import *
 from webhelpers.util import html_escape
 
     'button-to', to which you can attach CSS styles for display
     purposes.
     
+    The submit button itself will be displayed as an image if you provide both
+    ``type`` and ``src`` as followed:
+
+         type='image', src='icon_delete.gif'
+
+    The ``src`` path will be computed as the image_tag() computes it's ``source``
+    argument.
+
     Example 1::
     
         # inside of controller for "feeds"
             <input onclick="return confirm('Are you sure?');" value="Destroy" type="submit" />
         </div>
         </form>
+
+    Example 3::
+
+        # Button as an image.
+        >>> button_to("Edit", url(action='edit', id=3), type='image', src='icon_delete.gif')
+        <form method="POST" action="/feeds/edit/3" class="button-to">
+        <div><input alt="Edit" src="/images/icon_delete.gif" type="image" value="Edit" /></div>
+        </form>
     
     *NOTE*: This method generates HTML code that represents a form.
     Forms are "block" content, which means that you should not try to
     else:
         url, name = url, name or url
     
-    html_options.update(dict(type='submit', value=name))
+    submit_type = html_options.get('type')
+    img_source = html_options.get('src')
+    if submit_type == 'image' and img_source:
+        html_options.update(dict(type=submit_type, value=name,
+                                 alt=html_options.get('alt', name)))
+        html_options['src'] = compute_public_path(img_source, 'images', 'png')
+    else:
+        html_options.update(dict(type='submit', value=name))
     
     return """<form method="%s" action="%s" class="button-to"><div>""" % \
         (form_method, tags.escape_once(url)) + method_tag + \
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.