From 1376f4cab1eb27cd54718a7e6d7f50215c7340e5 Mon Sep 17 00:00:00 2001 From: Sergey Nartimov Date: Wed, 30 May 2012 23:02:26 +0300 Subject: no need to pass an empty block to button_to helper --- actionpack/test/controller/request_forgery_protection_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/actionpack/test/controller/request_forgery_protection_test.rb b/actionpack/test/controller/request_forgery_protection_test.rb index 066cd523be..0289f4070b 100644 --- a/actionpack/test/controller/request_forgery_protection_test.rb +++ b/actionpack/test/controller/request_forgery_protection_test.rb @@ -9,7 +9,7 @@ module RequestForgeryProtectionActions end def show_button - render :inline => "<%= button_to('New', '/') {} %>" + render :inline => "<%= button_to('New', '/') %>" end def external_form @@ -79,7 +79,7 @@ class FreeCookieController < RequestForgeryProtectionController end def show_button - render :inline => "<%= button_to('New', '/') {} %>" + render :inline => "<%= button_to('New', '/') %>" end end -- cgit v1.2.3 From ab7a80ea22c94a006788eddfa3b92123b4031cb6 Mon Sep 17 00:00:00 2001 From: Sergey Nartimov Date: Wed, 30 May 2012 23:15:15 +0300 Subject: accept a block in button_to helper Make possible to use a block in button_to helper if button text is hard to fit into the name parameter, e.g.: <%= button_to [:make_happy, @user] do %> Make happy <%= @user.name %> <% end %> # => "
#
# #
#
" --- actionpack/CHANGELOG.md | 16 ++++++++++++ actionpack/lib/action_view/helpers/url_helper.rb | 31 +++++++++++++++++++++--- actionpack/test/template/url_helper_test.rb | 7 ++++++ 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index 2e7b3190fc..b907eea3c1 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,5 +1,21 @@ ## Rails 4.0.0 (unreleased) ## +* Make possible to use a block in button_to helper if button text is hard + to fit into the name parameter, e.g.: + + <%= button_to [:make_happy, @user] do %> + Make happy <%= @user.name %> + <% end %> + # => "
+ #
+ # + #
+ #
" + + *Sergey Nartimov* + * change a way of ordering helpers from several directories. Previously, when loading helpers from multiple paths, all of the helpers files were gathered into one array an then they were sorted. Helpers from different diff --git a/actionpack/lib/action_view/helpers/url_helper.rb b/actionpack/lib/action_view/helpers/url_helper.rb index 7e69547dab..a4b10bc68a 100644 --- a/actionpack/lib/action_view/helpers/url_helper.rb +++ b/actionpack/lib/action_view/helpers/url_helper.rb @@ -294,6 +294,16 @@ module ActionView # #
# # " # + # <%= button_to [:make_happy, @user] do %> + # Make happy <%= @user.name %> + # <% end %> + # # => "
+ # #
+ # # + # #
+ # #
" # # <%= button_to "New", :action => "new", :form_class => "new-thing" %> # # => "
@@ -331,7 +341,16 @@ module ActionView # # # #
" # # - def button_to(name, options = {}, html_options = {}) + def button_to(*args, &block) + if block_given? + options = args[0] || {} + html_options = args[1] || {} + else + name = args[0] + options = args[1] || {} + html_options = args[2] || {} + end + html_options = html_options.stringify_keys convert_boolean_attributes!(html_options, %w(disabled)) @@ -350,9 +369,15 @@ module ActionView request_token_tag = form_method == 'post' ? token_tag : '' html_options = convert_options_to_data_attributes(options, html_options) - html_options.merge!("type" => "submit", "value" => name || url) + html_options['type'] = 'submit' + + button = if block_given? + content_tag('button', html_options, &block) + else + tag('input', html_options.merge('value' => name || url)) + end - inner_tags = method_tag.safe_concat tag('input', html_options).safe_concat request_token_tag + inner_tags = method_tag.safe_concat(button).safe_concat(request_token_tag) content_tag('form', content_tag('div', inner_tags), form_options) end diff --git a/actionpack/test/template/url_helper_test.rb b/actionpack/test/template/url_helper_test.rb index fb5b35bac6..365a86ab91 100644 --- a/actionpack/test/template/url_helper_test.rb +++ b/actionpack/test/template/url_helper_test.rb @@ -144,6 +144,13 @@ class UrlHelperTest < ActiveSupport::TestCase ) end + def test_button_to_with_block + assert_dom_equal( + "
", + button_to("http://www.example.com") { content_tag(:span, 'Hello') } + ) + end + def test_link_tag_with_straight_url assert_dom_equal "Hello", link_to("Hello", "http://www.example.com") end -- cgit v1.2.3