public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Added block-call style to link_to [Sam Stephenson/DHH]
dhh (author)
Tue Jun 17 12:01:37 -0700 2008
commit  8190bce8bc7249b7b9f3680195336eb3ca9508ee
tree    f159b343e4893f08da5f9a3d92e1c88783323a83
parent  22af62cf486721ee2e45bb720c42ac2f4121faf4
...
1
2
 
 
 
 
 
 
3
4
5
...
1
2
3
4
5
6
7
8
9
10
11
0
@@ -1,5 +1,11 @@
0
 *Edge*
0
 
0
+* Added block-call style to link_to [Sam Stephenson/DHH]. Example:
0
+
0
+ <% link_to(@profile) do %>
0
+ <strong><%= @profile.name %></strong> -- <span>Check it out!!</span>
0
+ <% end %>
0
+
0
 * Performance: integration test benchmarking and profiling. [Jeremy Kemper]
0
 
0
 * Make caching more aware of mime types. Ensure request format is not considered while expiring cache. [Jonathan del Strother]
...
90
91
92
 
 
 
 
 
 
 
93
94
95
...
147
148
149
 
 
 
 
 
 
 
150
151
152
...
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
...
90
91
92
93
94
95
96
97
98
99
100
101
102
...
154
155
156
157
158
159
160
161
162
163
164
165
166
...
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
0
@@ -90,6 +90,13 @@ module ActionView
0
       # link will be used in place of a referrer if none exists. If nil is passed as
0
       # a name, the link itself will become the name.
0
       #
0
+ # ==== Signatures
0
+ #
0
+ # link_to(name, options = {}, html_options = nil)
0
+ # link_to(options = {}, html_options = nil) do
0
+ # # name
0
+ # end
0
+ #
0
       # ==== Options
0
       # * <tt>:confirm => 'question?'</tt> - This will add a JavaScript confirm
0
       # prompt with the question specified. If the user accepts, the link is
0
@@ -147,6 +154,13 @@ module ActionView
0
       # link_to "Profiles", :controller => "profiles"
0
       # # => <a href="/profiles">Profiles</a>
0
       #
0
+ # You can use a block as well if your link target is hard to fit into the name parameter. ERb example:
0
+ #
0
+ # <% link_to(@profile) do %>
0
+ # <strong><%= @profile.name %></strong> -- <span>Check it out!!</span>
0
+ # <% end %>
0
+ # # => <a href="/profiles/1"><strong>David</strong> -- <span>Check it out!!</span></a>
0
+ #
0
       # Classes and ids for CSS are easy to produce:
0
       #
0
       # link_to "Articles", articles_path, :id => "news", :class => "article"
0
@@ -189,27 +203,37 @@ module ActionView
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
       # m.setAttribute('value', 'delete'); f.appendChild(m);f.submit(); };return false;">Delete Image</a>
0
- def link_to(name, options = {}, html_options = nil)
0
- url = case options
0
- when String
0
- options
0
- when :back
0
- @controller.request.env["HTTP_REFERER"] || 'javascript:history.back()'
0
+ def link_to(*args, &block)
0
+ if block_given?
0
+ options = args.first || {}
0
+ html_options = args.second
0
+ concat(link_to(capture(&block), options, html_options))
0
+ else
0
+ name = args.first
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
+
0
+ if html_options
0
+ html_options = html_options.stringify_keys
0
+ href = html_options['href']
0
+ convert_options_to_javascript!(html_options, url)
0
+ tag_options = tag_options(html_options)
0
           else
0
- self.url_for(options)
0
+ tag_options = nil
0
           end
0
-
0
- if html_options
0
- html_options = html_options.stringify_keys
0
- href = html_options['href']
0
- convert_options_to_javascript!(html_options, url)
0
- tag_options = tag_options(html_options)
0
- else
0
- tag_options = nil
0
+
0
+ href_attr = "href=\"#{url}\"" unless href
0
+ "<a #{href_attr}#{tag_options}>#{name || url}</a>"
0
         end
0
-
0
- href_attr = "href=\"#{url}\"" unless href
0
- "<a #{href_attr}#{tag_options}>#{name || url}</a>"
0
       end
0
 
0
       # Generates a form containing a single button that submits to the URL created
...
211
212
213
 
 
 
 
 
 
 
 
214
215
216
...
211
212
213
214
215
216
217
218
219
220
221
222
223
224
0
@@ -211,6 +211,14 @@ class UrlHelperTest < ActionView::TestCase
0
   def test_link_tag_using_post_javascript_and_popup
0
     assert_raises(ActionView::ActionViewError) { link_to("Hello", "http://www.example.com", :popup => true, :method => :post, :confirm => "Are you serious?") }
0
   end
0
+
0
+ def test_link_tag_using_block
0
+ self.output_buffer = ''
0
+
0
+ link_to("http://example.com") { concat("Example site") }
0
+
0
+ assert_equal '<a href="http://example.com">Example site</a>', output_buffer
0
+ end
0
   
0
   def test_link_to_unless
0
     assert_equal "Showing", link_to_unless(true, "Showing", :action => "show", :controller => "weblog")

Comments

  • snostorm Wed Jun 18 12:30:01 -0700 2008

    Convenient :-)

  • snostorm Wed Jun 18 12:30:35 -0700 2008

    Convenient :-)

  • schwabsauce Wed Jun 18 18:51:45 -0700 2008

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