diff --git a/actionview/CHANGELOG.md b/actionview/CHANGELOG.md index 35afeffbe6..18a32982ea 100644 --- a/actionview/CHANGELOG.md +++ b/actionview/CHANGELOG.md @@ -1,7 +1,21 @@ +* Change `ActionView::Helpers::UrlHelper#button_to` to *always* render a + ` + + <%= button_to post_path(@post), method: :delete do %> + Delete + <% end %> + <%# =>
+ + *Sean Doyle, Dusan Orlovic* + * Add `config.action_view.preload_links_header` to allow disabling of the `Link` header being added by default when using `stylesheet_link_tag` and `javascript_include_tag`. - + *Andrew White* * The `translate` helper now resolves `default` values when a `nil` key is diff --git a/actionview/lib/action_view/helpers/url_helper.rb b/actionview/lib/action_view/helpers/url_helper.rb index 086917f8a5..6f163032ed 100644 --- a/actionview/lib/action_view/helpers/url_helper.rb +++ b/actionview/lib/action_view/helpers/url_helper.rb @@ -29,6 +29,8 @@ module ActionView end end + mattr_accessor :button_to_generates_button_tag, default: false + # Basic implementation of url_for to allow use helpers without routes existence def url_for(options = nil) # :nodoc: case options @@ -250,12 +252,12 @@ module ActionView # ==== Examples # <%= button_to "New", action: "new" %> # # => "
- # # + # # # #
" # # <%= button_to "New", new_article_path %> # # => "
- # # + # # # #
" # # <%= button_to [:make_happy, @user] do %> @@ -269,13 +271,13 @@ module ActionView # # <%= button_to "New", { action: "new" }, form_class: "new-thing" %> # # => "
- # # + # # # #
" # # # <%= button_to "Create", { action: "create" }, remote: true, form: { "data-type" => "json" } %> # # => "
- # # + # # # # # #
" # @@ -284,7 +286,7 @@ module ActionView # method: :delete, data: { confirm: "Are you sure?" } %> # # => "
# # - # # + # # # # # #
" # @@ -293,7 +295,7 @@ module ActionView # method: :delete, remote: true, data: { confirm: 'Are you sure?', disable_with: 'loading...' }) %> # # => "
# # - # # + # # # # # #
" # # @@ -327,8 +329,8 @@ module ActionView html_options = convert_options_to_data_attributes(options, html_options) html_options["type"] = "submit" - button = if block_given? - content_tag("button", html_options, &block) + button = if block_given? || button_to_generates_button_tag + content_tag("button", name || url, html_options, &block) else html_options["value"] = name || url tag("input", html_options) diff --git a/actionview/lib/action_view/railtie.rb b/actionview/lib/action_view/railtie.rb index c21f32ad14..b67482205d 100644 --- a/actionview/lib/action_view/railtie.rb +++ b/actionview/lib/action_view/railtie.rb @@ -81,6 +81,13 @@ module ActionView PartialRenderer.collection_cache = app.config.action_controller.cache_store end + initializer "action_view.button_to_generates_button_tag" do |app| + ActiveSupport.on_load(:action_view) do + ActionView::Helpers::UrlHelper.button_to_generates_button_tag = + app.config.action_view.delete(:button_to_generates_button_tag) + end + end + config.after_initialize do |app| enable_caching = if app.config.action_view.cache_template_loading.nil? app.config.cache_classes diff --git a/actionview/test/template/url_helper_test.rb b/actionview/test/template/url_helper_test.rb index 3a16b3423f..e7798e28fa 100644 --- a/actionview/test/template/url_helper_test.rb +++ b/actionview/test/template/url_helper_test.rb @@ -50,6 +50,8 @@ class UrlHelperTest < ActiveSupport::TestCase include RenderERBUtils setup :_prepare_context + setup { ActionView::Helpers::UrlHelper.button_to_generates_button_tag = @button_to_generates_button_tag = true } + teardown { ActionView::Helpers::UrlHelper.button_to_generates_button_tag = @button_to_generates_button_tag } def hash_for(options = {}) { controller: "foo", action: "bar" }.merge!(options) @@ -140,7 +142,7 @@ class UrlHelperTest < ActiveSupport::TestCase def test_button_to_without_protect_against_forgery_method self.class.undef_method(:protect_against_forgery?) assert_dom_equal( - %{
}, + %{
}, button_to("Hello", "http://www.example.com") ) ensure @@ -148,12 +150,12 @@ class UrlHelperTest < ActiveSupport::TestCase end def test_button_to_with_straight_url - assert_dom_equal %{
}, button_to("Hello", "http://www.example.com") + assert_dom_equal %{
}, button_to("Hello", "http://www.example.com") end def test_button_to_with_path assert_dom_equal( - %{
}, + %{
}, button_to("Hello", article_path("Hello")) ) end @@ -162,7 +164,7 @@ class UrlHelperTest < ActiveSupport::TestCase self.request_forgery = true assert_dom_equal( - %{
}, + %{
}, button_to("Hello", "http://www.example.com") ) ensure @@ -170,88 +172,92 @@ class UrlHelperTest < ActiveSupport::TestCase end def test_button_to_with_form_class - assert_dom_equal %{
}, button_to("Hello", "http://www.example.com", form_class: "custom-class") + assert_dom_equal %{
}, button_to("Hello", "http://www.example.com", form_class: "custom-class") end def test_button_to_with_form_class_escapes - assert_dom_equal %{
}, button_to("Hello", "http://www.example.com", form_class: "") + assert_dom_equal %{
}, button_to("Hello", "http://www.example.com", form_class: "") end def test_button_to_with_query - assert_dom_equal %{
}, button_to("Hello", "http://www.example.com/q1=v1&q2=v2") + assert_dom_equal %{
}, button_to("Hello", "http://www.example.com/q1=v1&q2=v2") + end + + def test_button_to_with_value + assert_dom_equal %{
}, button_to("Hello", "http://www.example.com", name: "key", value: "value") end def test_button_to_with_html_safe_URL - assert_dom_equal %{
}, button_to("Hello", raw("http://www.example.com/q1=v1&q2=v2")) + assert_dom_equal %{
}, button_to("Hello", raw("http://www.example.com/q1=v1&q2=v2")) end def test_button_to_with_query_and_no_name - assert_dom_equal %{
}, button_to(nil, "http://www.example.com?q1=v1&q2=v2") + assert_dom_equal %{
}, button_to(nil, "http://www.example.com?q1=v1&q2=v2") end def test_button_to_with_javascript_confirm assert_dom_equal( - %{
}, + %{
}, button_to("Hello", "http://www.example.com", data: { confirm: "Are you sure?" }) ) end def test_button_to_with_javascript_disable_with assert_dom_equal( - %{
}, + %{
}, button_to("Hello", "http://www.example.com", data: { disable_with: "Greeting..." }) ) end def test_button_to_with_remote_and_form_options assert_dom_equal( - %{
}, + %{
}, button_to("Hello", "http://www.example.com", remote: true, form: { class: "custom-class", "data-type" => "json" }) ) end def test_button_to_with_remote_and_javascript_confirm assert_dom_equal( - %{
}, + %{
}, button_to("Hello", "http://www.example.com", remote: true, data: { confirm: "Are you sure?" }) ) end def test_button_to_with_remote_and_javascript_disable_with assert_dom_equal( - %{
}, + %{
}, button_to("Hello", "http://www.example.com", remote: true, data: { disable_with: "Greeting..." }) ) end def test_button_to_with_remote_false assert_dom_equal( - %{
}, + %{
}, button_to("Hello", "http://www.example.com", remote: false) ) end def test_button_to_enabled_disabled assert_dom_equal( - %{
}, + %{
}, button_to("Hello", "http://www.example.com", disabled: false) ) assert_dom_equal( - %{
}, + %{
}, button_to("Hello", "http://www.example.com", disabled: true) ) end def test_button_to_with_method_delete assert_dom_equal( - %{
}, + %{
}, button_to("Hello", "http://www.example.com", method: :delete) ) end def test_button_to_with_method_get assert_dom_equal( - %{
}, + %{
}, button_to("Hello", "http://www.example.com", method: :get) ) end @@ -265,11 +271,23 @@ class UrlHelperTest < ActiveSupport::TestCase def test_button_to_with_params assert_dom_equal( - %{
}, + %{
}, button_to("Hello", "http://www.example.com", params: { foo: :bar, baz: "quux" }) ) end + def test_button_to_generates_input_when_button_to_generates_button_tag_false + old_value = ActionView::Helpers::UrlHelper.button_to_generates_button_tag + ActionView::Helpers::UrlHelper.button_to_generates_button_tag = false + + assert_dom_equal( + %{
}, + button_to("Save", "http://www.example.com") + ) + ensure + ActionView::Helpers::UrlHelper.button_to_generates_button_tag = old_value + end + class FakeParams def initialize(permitted = true) @permitted = permitted @@ -290,7 +308,7 @@ class UrlHelperTest < ActiveSupport::TestCase def test_button_to_with_permitted_strong_params assert_dom_equal( - %{
}, + %{
}, button_to("Hello", "http://www.example.com", params: FakeParams.new) ) end @@ -303,14 +321,14 @@ class UrlHelperTest < ActiveSupport::TestCase def test_button_to_with_nested_hash_params assert_dom_equal( - %{
}, + %{
}, button_to("Hello", "http://www.example.com", params: { foo: { bar: "baz" } }) ) end def test_button_to_with_nested_array_params assert_dom_equal( - %{
}, + %{
}, button_to("Hello", "http://www.example.com", params: { foo: ["bar"] }) ) end diff --git a/railties/lib/rails/application/configuration.rb b/railties/lib/rails/application/configuration.rb index b52775473b..47021ac75e 100644 --- a/railties/lib/rails/application/configuration.rb +++ b/railties/lib/rails/application/configuration.rb @@ -203,6 +203,10 @@ module Rails ActiveSupport.utc_to_local_returns_utc_offset_times = true when "6.2" load_defaults "6.1" + + if respond_to?(:action_view) + action_view.button_to_generates_button_tag = true + end else raise "Unknown version #{target_version.to_s.inspect}" end diff --git a/railties/lib/rails/generators/rails/app/templates/config/initializers/new_framework_defaults_6_2.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/initializers/new_framework_defaults_6_2.rb.tt index 9ada00d85c..2cf837fe26 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/initializers/new_framework_defaults_6_2.rb.tt +++ b/railties/lib/rails/generators/rails/app/templates/config/initializers/new_framework_defaults_6_2.rb.tt @@ -5,3 +5,6 @@ # Once upgraded flip defaults one by one to migrate to the new default. # # Read the Guide for Upgrading Ruby on Rails for more info on each option. + +# button_to view helpers consistently render