From ba12811db22de23e935b43f39cac8da523fa0ded Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 31 Jul 2017 16:23:37 -0500 Subject: Move the direct_upload: true convenience option from the activestorage helper into actionview --- actionview/lib/action_view/helpers/form_helper.rb | 6 +++++- actionview/lib/action_view/helpers/form_tag_helper.rb | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) (limited to 'actionview/lib/action_view') diff --git a/actionview/lib/action_view/helpers/form_helper.rb b/actionview/lib/action_view/helpers/form_helper.rb index 4eac086a87..314a2e3f44 100644 --- a/actionview/lib/action_view/helpers/form_helper.rb +++ b/actionview/lib/action_view/helpers/form_helper.rb @@ -1193,7 +1193,7 @@ module ActionView # file_field(:attachment, :file, class: 'file_input') # # => def file_field(object_name, method, options = {}) - Tags::FileField.new(object_name, method, self, options).render + Tags::FileField.new(object_name, method, self, convert_direct_upload_option_to_url(options)).render end # Returns a textarea opening and closing tag set tailored for accessing a specified attribute (identified by +method+) @@ -2316,6 +2316,10 @@ module ActionView options[:include_id] = !options.delete(:skip_id) end 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 + end end end diff --git a/actionview/lib/action_view/helpers/form_tag_helper.rb b/actionview/lib/action_view/helpers/form_tag_helper.rb index 91046acbf8..83b600d871 100644 --- a/actionview/lib/action_view/helpers/form_tag_helper.rb +++ b/actionview/lib/action_view/helpers/form_tag_helper.rb @@ -274,7 +274,7 @@ module ActionView # file_field_tag 'file', accept: 'text/html', class: 'upload', value: 'index.html' # # => def file_field_tag(name, options = {}) - text_field_tag(name, nil, options.merge(type: :file)) + text_field_tag(name, nil, convert_direct_upload_option_to_url(options.merge(type: :file))) end # Creates a password field, a masked text field that will hide the users input behind a mask character. @@ -904,6 +904,10 @@ module ActionView tag_options.delete("data-disable-with") 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 + end end end end -- cgit v1.2.3 From 7ddec686550d6ceb5a0c2e7e9dcd23bd31567ac3 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Tue, 1 Aug 2017 16:25:33 -0500 Subject: Add require --- actionview/lib/action_view/helpers/form_helper.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'actionview/lib/action_view') diff --git a/actionview/lib/action_view/helpers/form_helper.rb b/actionview/lib/action_view/helpers/form_helper.rb index 314a2e3f44..41af6b6e3a 100644 --- a/actionview/lib/action_view/helpers/form_helper.rb +++ b/actionview/lib/action_view/helpers/form_helper.rb @@ -9,6 +9,7 @@ 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" -- cgit v1.2.3 From 422ec4cb78cc9c10af97ddf958d477ce604b4506 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Thu, 3 Aug 2017 16:22:58 -0400 Subject: Make sure Action View doesn't break with Active Storage When Active Storage is not loaded and direct_upload is used on file_field_tag we should not raise an exception. --- actionview/lib/action_view/helpers/form_tag_helper.rb | 2 +- actionview/lib/action_view/test_case.rb | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) (limited to 'actionview/lib/action_view') diff --git a/actionview/lib/action_view/helpers/form_tag_helper.rb b/actionview/lib/action_view/helpers/form_tag_helper.rb index 83b600d871..fd7184f8d3 100644 --- a/actionview/lib/action_view/helpers/form_tag_helper.rb +++ b/actionview/lib/action_view/helpers/form_tag_helper.rb @@ -906,7 +906,7 @@ 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 + options.merge('data-direct-upload-url': options.delete(:direct_upload) && respond_to?(:rails_direct_uploads_url) ? rails_direct_uploads_url : nil).compact end end end diff --git a/actionview/lib/action_view/test_case.rb b/actionview/lib/action_view/test_case.rb index efe8c87b9b..6913c31a20 100644 --- a/actionview/lib/action_view/test_case.rb +++ b/actionview/lib/action_view/test_case.rb @@ -281,6 +281,18 @@ module ActionView super end end + + def respond_to_missing?(name, include_private = false) + begin + routes = @controller.respond_to?(:_routes) && @controller._routes + rescue + # Dont call routes, if there is an error on _routes call + end + + routes && + (routes.named_routes.route_defined?(name) || + routes.mounted_helpers.method_defined?(name)) + end end include Behavior -- cgit v1.2.3 From f1648f6539a2fd74c9c53a08f886813b5f1c14ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Thu, 3 Aug 2017 16:35:55 -0400 Subject: Refactor convert_direct_upload_option_to_url Also make sure file_field doesn't mutate the original options passed in. --- actionview/lib/action_view/helpers/form_helper.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'actionview/lib/action_view') 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') # # => 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 -- cgit v1.2.3 From 7a5a84ba9c8648cd6fe5eeb9af2ada56118d8b57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Thu, 3 Aug 2017 16:39:43 -0400 Subject: Remove duplicated convert_direct_upload_option_to_url FormHelper includes FormTagHelper so we don't need to define two methods --- actionview/lib/action_view/helpers/form_helper.rb | 6 ------ actionview/lib/action_view/helpers/form_tag_helper.rb | 5 ++++- 2 files changed, 4 insertions(+), 7 deletions(-) (limited to 'actionview/lib/action_view') diff --git a/actionview/lib/action_view/helpers/form_helper.rb b/actionview/lib/action_view/helpers/form_helper.rb index ac42de053c..8b6322f8ad 100644 --- a/actionview/lib/action_view/helpers/form_helper.rb +++ b/actionview/lib/action_view/helpers/form_helper.rb @@ -2316,12 +2316,6 @@ module ActionView options[:include_id] = !options.delete(:skip_id) end end - - def convert_direct_upload_option_to_url(options) - if options.delete(:direct_upload) - options['data-direct-upload-url'] = rails_direct_uploads_url - end - end end end diff --git a/actionview/lib/action_view/helpers/form_tag_helper.rb b/actionview/lib/action_view/helpers/form_tag_helper.rb index fd7184f8d3..2519ff2837 100644 --- a/actionview/lib/action_view/helpers/form_tag_helper.rb +++ b/actionview/lib/action_view/helpers/form_tag_helper.rb @@ -906,7 +906,10 @@ module ActionView end def convert_direct_upload_option_to_url(options) - options.merge('data-direct-upload-url': options.delete(:direct_upload) && respond_to?(:rails_direct_uploads_url) ? rails_direct_uploads_url : nil).compact + if options.delete(:direct_upload) && respond_to?(:rails_direct_uploads_url) + options["data-direct-upload-url"] = rails_direct_uploads_url + end + options end end end -- cgit v1.2.3