aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2017-04-21 12:23:49 -0400
committerRafael Mendonça França <rafaelmfranca@gmail.com>2017-04-21 12:23:49 -0400
commit128b804c6ce40fcbde744f294f8cb98654f6efec (patch)
tree37f69f303bf4b5015fdbfcbd9bc4e60de2d8c0e4
parentd766e3dc5dde086ef1b391e30c8c4b6d8b682a35 (diff)
downloadrails-128b804c6ce40fcbde744f294f8cb98654f6efec.tar.gz
rails-128b804c6ce40fcbde744f294f8cb98654f6efec.tar.bz2
rails-128b804c6ce40fcbde744f294f8cb98654f6efec.zip
Configure form_with_generates_remote_forms in its own initializer
This configuration is not present in ActionView::Base so we can't let the action_view.set_configs initializer set it. Also add tests to make sure this config works. Fixes #28824
-rw-r--r--actionview/lib/action_view/railtie.rb7
-rw-r--r--actionview/test/template/form_helper/form_with_test.rb22
-rw-r--r--railties/test/application/configuration_test.rb32
3 files changed, 61 insertions, 0 deletions
diff --git a/actionview/lib/action_view/railtie.rb b/actionview/lib/action_view/railtie.rb
index 0939584786..bc474f453e 100644
--- a/actionview/lib/action_view/railtie.rb
+++ b/actionview/lib/action_view/railtie.rb
@@ -17,6 +17,13 @@ module ActionView
end
end
+ initializer "action_view.form_with_generates_remote_forms" do |app|
+ ActiveSupport.on_load(:action_view) do
+ ActionView::Helpers::FormHelper.form_with_generates_remote_forms =
+ app.config.action_view.delete(:form_with_generates_remote_forms)
+ end
+ end
+
initializer "action_view.logger" do
ActiveSupport.on_load(:action_view) { self.logger ||= Rails.logger }
end
diff --git a/actionview/test/template/form_helper/form_with_test.rb b/actionview/test/template/form_helper/form_with_test.rb
index a4a2966ff9..a09dd468dd 100644
--- a/actionview/test/template/form_helper/form_with_test.rb
+++ b/actionview/test/template/form_helper/form_with_test.rb
@@ -729,6 +729,28 @@ class FormWithActsLikeFormForTest < FormWithTest
assert_dom_equal expected, output_buffer
end
+ def test_form_is_not_remote_by_default_if_form_with_generates_remote_forms_is_false
+ old_value = ActionView::Helpers::FormHelper.form_with_generates_remote_forms
+ ActionView::Helpers::FormHelper.form_with_generates_remote_forms = false
+
+ form_with(model: @post, url: "/", id: "create-post", method: :patch) do |f|
+ concat f.text_field(:title)
+ concat f.text_area(:body)
+ concat f.check_box(:secret)
+ end
+
+ expected = whole_form("/", "create-post", method: "patch", local: true) do
+ "<input name='post[title]' type='text' value='Hello World' />" \
+ "<textarea name='post[body]'>\nBack to the hill and over it again!</textarea>" \
+ "<input name='post[secret]' type='hidden' value='0' />" \
+ "<input name='post[secret]' checked='checked' type='checkbox' value='1' />"
+ end
+
+ assert_dom_equal expected, output_buffer
+ ensure
+ ActionView::Helpers::FormHelper.form_with_generates_remote_forms = old_value
+ end
+
def test_form_with_skip_enforcing_utf8_true
form_with(scope: :post, skip_enforcing_utf8: true) do |f|
concat f.text_field(:title)
diff --git a/railties/test/application/configuration_test.rb b/railties/test/application/configuration_test.rb
index 14433fbba0..32eab74190 100644
--- a/railties/test/application/configuration_test.rb
+++ b/railties/test/application/configuration_test.rb
@@ -693,6 +693,38 @@ module ApplicationTests
assert_match(/label/, last_response.body)
end
+ test "form_with can be configured with form_with_generates_remote_forms" do
+ app_file "config/initializers/form_builder.rb", <<-RUBY
+ Rails.configuration.action_view.form_with_generates_remote_forms = false
+ RUBY
+
+ app_file "app/models/post.rb", <<-RUBY
+ class Post
+ include ActiveModel::Model
+ attr_accessor :name
+ end
+ RUBY
+
+ app_file "app/controllers/posts_controller.rb", <<-RUBY
+ class PostsController < ApplicationController
+ def index
+ render inline: "<%= begin; form_with(model: Post.new) {|f| f.text_field(:name)}; rescue => e; e.to_s; end %>"
+ end
+ end
+ RUBY
+
+ add_to_config <<-RUBY
+ routes.prepend do
+ resources :posts
+ end
+ RUBY
+
+ app "development"
+
+ get "/posts"
+ assert_no_match(/data-remote/, last_response.body)
+ end
+
test "default method for update can be changed" do
app_file "app/models/post.rb", <<-RUBY
class Post