From 2d6e23f3be929f3041980b0a82a6bfa9147dc6cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kadu=20Di=C3=B3genes?= Date: Mon, 11 Feb 2019 13:15:58 -0200 Subject: [PATCH] Adds option to support direct uploads (#14) * Adds option to support direct uploads * Adds documentation for direct_upload option --- README.md | 14 ++++++++++++++ app/views/fields/active_storage/_form.html.erb | 2 +- lib/administrate/field/active_storage.rb | 4 ++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a286182..1109622 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,20 @@ class ModelDashboard < Administrate::BaseDashboard end ``` +### direct_upload +If you want to upload directly from the browser to the cloud you can use direct_upload +```ruby +class ModelDashboard < Administrate::BaseDashboard + ATTRIBUTE_TYPES = { + attachments: Field::ActiveStorage.with_options({direct_upload: true}), + # ... + } + # ... +end +``` + +Don't forget to include [ActiveStorage JavaScript](https://edgeguides.rubyonrails.org/active_storage_overview.html#direct-uploads). You can use `rails generate administrate:assets:javascripts` to be able to customize Administrate JavaScripts in your application. + ## Things To Do: - [x] upload single file - [x] adding image support through url_for to support 3rd party cloud storage diff --git a/app/views/fields/active_storage/_form.html.erb b/app/views/fields/active_storage/_form.html.erb index c5f9932..8bc9723 100644 --- a/app/views/fields/active_storage/_form.html.erb +++ b/app/views/fields/active_storage/_form.html.erb @@ -26,5 +26,5 @@ By default, the input is a text field for the image's URL. Add: <% end %> - <%= f.file_field field.attribute, multiple: field.many? %> + <%= f.file_field field.attribute, multiple: field.many?, direct_upload: field.direct? %> diff --git a/lib/administrate/field/active_storage.rb b/lib/administrate/field/active_storage.rb index 3ec6213..8ad9ebd 100644 --- a/lib/administrate/field/active_storage.rb +++ b/lib/administrate/field/active_storage.rb @@ -20,6 +20,10 @@ def many? data.class.name == "ActiveStorage::Attached::Many" end + def direct? + options.fetch(:direct_upload, false) + end + # def destroy_path? # options.fetch(:destroy_path, false).present? # end