Skip to content

Commit

Permalink
Make sure Action View doesn't break with Active Storage
Browse files Browse the repository at this point in the history
When Active Storage is not loaded and direct_upload is used on
file_field_tag we should not raise an exception.
  • Loading branch information
rafaelfranca committed Aug 3, 2017
1 parent d84a126 commit 422ec4c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion actionview/lib/action_view/helpers/form_tag_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,7 @@ def set_default_disable_with(value, tag_options)
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
Expand Down
12 changes: 12 additions & 0 deletions actionview/lib/action_view/test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,18 @@ def method_missing(selector, *args)
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
Expand Down
37 changes: 37 additions & 0 deletions actionview/test/template/form_tag_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ class FormTagHelperTest < ActionView::TestCase

tests ActionView::Helpers::FormTagHelper

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 setup
super
@controller = BasicController.new
Expand Down Expand Up @@ -178,6 +188,33 @@ def test_file_field_tag_with_options
assert_dom_equal "<input name=\"picsplz\" type=\"file\" id=\"picsplz\" class=\"pix\"/>", file_field_tag("picsplz", class: "pix")
end

def test_file_field_tag_with_direct_upload_when_rails_direct_uploads_url_is_not_defined
assert_dom_equal(
"<input name=\"picsplz\" type=\"file\" id=\"picsplz\" class=\"pix\"/>",
file_field_tag("picsplz", class: "pix", direct_upload: true)
)
end

def test_file_field_tag_with_direct_upload_when_rails_direct_uploads_url_is_defined
@controller = WithActiveStorageRoutesControllers.new

assert_dom_equal(
"<input name=\"picsplz\" type=\"file\" id=\"picsplz\" class=\"pix\" data-direct-upload-url=\"http://testtwo.host/rails/active_storage/direct_uploads\"/>",
file_field_tag("picsplz", class: "pix", direct_upload: true)
)
end

def test_file_field_tag_with_direct_upload_dont_mutate_arguments
original_options = { class: "pix", direct_upload: true }

assert_dom_equal(
"<input name=\"picsplz\" type=\"file\" id=\"picsplz\" class=\"pix\"/>",
file_field_tag("picsplz", original_options)
)

assert_equal({ class: "pix", direct_upload: true }, original_options)
end

def test_password_field_tag
actual = password_field_tag
expected = %(<input id="password" name="password" type="password" />)
Expand Down

0 comments on commit 422ec4c

Please sign in to comment.