Skip to content
This repository
Browse code

Added block-call style to link_to [Sam Stephenson/DHH]

  • Loading branch information...
commit 8190bce8bc7249b7b9f3680195336eb3ca9508ee 1 parent 22af62c
David Heinemeier Hansson authored June 17, 2008
6  actionpack/CHANGELOG
... ...
@@ -1,5 +1,11 @@
1 1
 *Edge*
2 2
 
  3
+* Added block-call style to link_to [Sam Stephenson/DHH]. Example:
  4
+
  5
+    <% link_to(@profile) do %>
  6
+      <strong><%= @profile.name %></strong> -- <span>Check it out!!</span>
  7
+    <% end %>
  8
+
3 9
 * Performance: integration test benchmarking and profiling.  [Jeremy Kemper]
4 10
 
5 11
 * Make caching more aware of mime types. Ensure request format is not considered while expiring cache.  [Jonathan del Strother]
60  actionpack/lib/action_view/helpers/url_helper.rb
@@ -90,6 +90,13 @@ def url_for(options = {})
90 90
       # link will be used in place of a referrer if none exists. If nil is passed as
91 91
       # a name, the link itself will become the name.
92 92
       #
  93
+      # ==== Signatures
  94
+      #
  95
+      #   link_to(name, options = {}, html_options = nil)
  96
+      #   link_to(options = {}, html_options = nil) do
  97
+      #     # name
  98
+      #   end
  99
+      #
93 100
       # ==== Options
94 101
       # * <tt>:confirm => 'question?'</tt> - This will add a JavaScript confirm
95 102
       #   prompt with the question specified. If the user accepts, the link is
@@ -147,6 +154,13 @@ def url_for(options = {})
147 154
       #   link_to "Profiles", :controller => "profiles"
148 155
       #   # => <a href="/profiles">Profiles</a>
149 156
       #
  157
+      # You can use a block as well if your link target is hard to fit into the name parameter. ERb example:
  158
+      #
  159
+      #   <% link_to(@profile) do %>
  160
+      #     <strong><%= @profile.name %></strong> -- <span>Check it out!!</span>
  161
+      #   <% end %>
  162
+      #   # => <a href="/profiles/1"><strong>David</strong> -- <span>Check it out!!</span></a>
  163
+      #
150 164
       # Classes and ids for CSS are easy to produce:
151 165
       #
152 166
       #   link_to "Articles", articles_path, :id => "news", :class => "article"
@@ -189,27 +203,37 @@ def url_for(options = {})
189 203
       #        f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;
190 204
       #        var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); 
191 205
       #        m.setAttribute('value', 'delete'); f.appendChild(m);f.submit(); };return false;">Delete Image</a>
192  
-      def link_to(name, options = {}, html_options = nil)
193  
-        url = case options
194  
-          when String
195  
-            options
196  
-          when :back
197  
-            @controller.request.env["HTTP_REFERER"] || 'javascript:history.back()'
  206
+      def link_to(*args, &block)
  207
+        if block_given?
  208
+          options      = args.first || {}
  209
+          html_options = args.second
  210
+          concat(link_to(capture(&block), options, html_options))
  211
+        else
  212
+          name         = args.first
  213
+          options      = args.second || {}
  214
+          html_options = args.third
  215
+
  216
+          url = case options
  217
+            when String
  218
+              options
  219
+            when :back
  220
+              @controller.request.env["HTTP_REFERER"] || 'javascript:history.back()'
  221
+            else
  222
+              self.url_for(options)
  223
+            end
  224
+
  225
+          if html_options
  226
+            html_options = html_options.stringify_keys
  227
+            href = html_options['href']
  228
+            convert_options_to_javascript!(html_options, url)
  229
+            tag_options = tag_options(html_options)
198 230
           else
199  
-            self.url_for(options)
  231
+            tag_options = nil
200 232
           end
201  
-
202  
-        if html_options
203  
-          html_options = html_options.stringify_keys
204  
-          href = html_options['href']
205  
-          convert_options_to_javascript!(html_options, url)
206  
-          tag_options = tag_options(html_options)
207  
-        else
208  
-          tag_options = nil
  233
+      
  234
+          href_attr = "href=\"#{url}\"" unless href
  235
+          "<a #{href_attr}#{tag_options}>#{name || url}</a>"
209 236
         end
210  
-        
211  
-        href_attr = "href=\"#{url}\"" unless href
212  
-        "<a #{href_attr}#{tag_options}>#{name || url}</a>"
213 237
       end
214 238
 
215 239
       # Generates a form containing a single button that submits to the URL created
8  actionpack/test/template/url_helper_test.rb
@@ -211,6 +211,14 @@ def test_link_tag_using_post_javascript_and_confirm
211 211
   def test_link_tag_using_post_javascript_and_popup
212 212
     assert_raises(ActionView::ActionViewError) { link_to("Hello", "http://www.example.com", :popup => true, :method => :post, :confirm => "Are you serious?") }
213 213
   end
  214
+
  215
+  def test_link_tag_using_block
  216
+    self.output_buffer = ''
  217
+
  218
+    link_to("http://example.com") { concat("Example site") }
  219
+
  220
+    assert_equal '<a href="http://example.com">Example site</a>', output_buffer
  221
+  end
214 222
   
215 223
   def test_link_to_unless
216 224
     assert_equal "Showing", link_to_unless(true, "Showing", :action => "show", :controller => "weblog")

3 notes on commit 8190bce

Sean Ouimet

Convenient :-)

Sean Ouimet

Convenient :-)

Mike Schwab

cool, this helps readability in one of the spots where I think readability is most crucial. sugar that encourages descriptive linking, love it

Please sign in to comment.
Something went wrong with that request. Please try again.