From a086418283b5220a06bf86e485f654d8552b5b0b Mon Sep 17 00:00:00 2001 From: Sean Doyle Date: Sat, 22 Feb 2020 02:14:42 -0500 Subject: [PATCH] Yield translation to `FormBuilder#button` block When translating a ` # + # button do |text| + # content_tag(:strong, text) + # end + # # => + # def button(value = nil, options = {}, &block) value, options = nil, value if value.is_a?(Hash) value ||= submit_default_value - @template.button_tag(value, options, &block) + + if block_given? + value = @template.capture { yield(value) } + end + + @template.button_tag(value, options) end def emitted_hidden_id? # :nodoc: diff --git a/actionview/test/template/form_helper/form_with_test.rb b/actionview/test/template/form_helper/form_with_test.rb index a24c0b69f5..9bf6fb97e4 100644 --- a/actionview/test/template/form_helper/form_with_test.rb +++ b/actionview/test/template/form_helper/form_with_test.rb @@ -365,6 +365,18 @@ class FormWithActsLikeFormForTest < FormWithTest assert_dom_equal expected, output_buffer end + def test_form_with_button_yields_translation + form_with(model: @post) do |f| + concat(f.button { |value| concat content_tag(:span, value) }) + end + + expected = whole_form("/posts/123", method: :patch) do + "" + end + + assert_dom_equal expected, output_buffer + end + def test_form_with_not_outputting_ids old_value = ActionView::Helpers::FormHelper.form_with_generates_ids ActionView::Helpers::FormHelper.form_with_generates_ids = false @@ -981,9 +993,7 @@ class FormWithActsLikeFormForTest < FormWithTest form_with(model: Post.new) do |f| concat( f.label(:title) do |builder| - concat tag.span(builder, { - class: ("new_record" unless builder.object.persisted?) - }) + concat tag.span(builder, class: ("new_record" unless builder.object.persisted?)) end ) end