aboutsummaryrefslogtreecommitdiffstats
path: root/actionview
diff options
context:
space:
mode:
Diffstat (limited to 'actionview')
-rw-r--r--actionview/lib/action_view/helpers/form_helper.rb7
-rw-r--r--actionview/test/template/form_helper_test.rb31
2 files changed, 35 insertions, 3 deletions
diff --git a/actionview/lib/action_view/helpers/form_helper.rb b/actionview/lib/action_view/helpers/form_helper.rb
index 41af6b6e3a..ac42de053c 100644
--- a/actionview/lib/action_view/helpers/form_helper.rb
+++ b/actionview/lib/action_view/helpers/form_helper.rb
@@ -9,7 +9,6 @@ require_relative "../model_naming"
require_relative "../record_identifier"
require "active_support/core_ext/module/attribute_accessors"
require "active_support/core_ext/hash/slice"
-require "active_support/core_ext/hash/compact"
require "active_support/core_ext/string/output_safety"
require "active_support/core_ext/string/inflections"
@@ -1194,7 +1193,7 @@ module ActionView
# file_field(:attachment, :file, class: 'file_input')
# # => <input type="file" id="attachment_file" name="attachment[file]" class="file_input" />
def file_field(object_name, method, options = {})
- Tags::FileField.new(object_name, method, self, convert_direct_upload_option_to_url(options)).render
+ Tags::FileField.new(object_name, method, self, convert_direct_upload_option_to_url(options.dup)).render
end
# Returns a textarea opening and closing tag set tailored for accessing a specified attribute (identified by +method+)
@@ -2319,7 +2318,9 @@ module ActionView
end
def convert_direct_upload_option_to_url(options)
- options.merge('data-direct-upload-url': options.delete(:direct_upload) ? rails_direct_uploads_url : nil).compact
+ if options.delete(:direct_upload)
+ options['data-direct-upload-url'] = rails_direct_uploads_url
+ end
end
end
end
diff --git a/actionview/test/template/form_helper_test.rb b/actionview/test/template/form_helper_test.rb
index 8d689aebeb..246f52588d 100644
--- a/actionview/test/template/form_helper_test.rb
+++ b/actionview/test/template/form_helper_test.rb
@@ -8,6 +8,16 @@ class FormHelperTest < ActionView::TestCase
tests ActionView::Helpers::FormHelper
+ class WithActiveStorageRoutesControllers < ActionController::Base
+ test_routes do
+ post "/rails/active_storage/direct_uploads" => "active_storage/direct_uploads#create", as: :rails_direct_uploads
+ end
+
+ def url_options
+ { host: "testtwo.host" }
+ end
+ end
+
def form_for(*)
@output_buffer = super
end
@@ -542,6 +552,27 @@ class FormHelperTest < ActionView::TestCase
assert_dom_equal expected, file_field("import", "file", multiple: true, name: "custom")
end
+ def test_file_field_with_direct_upload_when_rails_direct_uploads_url_is_not_defined
+ expected = '<input type="file" name="import[file]" id="import_file" />'
+ assert_dom_equal expected, file_field("import", "file", direct_upload: true)
+ end
+
+ def test_file_field_with_direct_upload_when_rails_direct_uploads_url_is_defined
+ @controller = WithActiveStorageRoutesControllers.new
+
+ expected = '<input data-direct-upload-url="http://testtwo.host/rails/active_storage/direct_uploads" type="file" name="import[file]" id="import_file" />'
+ assert_dom_equal expected, file_field("import", "file", direct_upload: true)
+ end
+
+ def test_file_field_with_direct_upload_dont_mutate_arguments
+ original_options = { class: "pix", direct_upload: true }
+
+ expected = '<input class="pix" type="file" name="import[file]" id="import_file" />'
+ assert_dom_equal expected, file_field("import", "file", original_options)
+
+ assert_equal({ class: "pix", direct_upload: true }, original_options)
+ end
+
def test_hidden_field
assert_dom_equal(
'<input id="post_title" name="post[title]" type="hidden" value="Hello World" />',