- To preview pdf files you need to install
mupdf
orPoppler
. - To preview video files you need to install
ffmpeg
.
Add administrate-field-active_storage
to your Gemfile:
gem 'administrate-field-active_storage'
Install:
$ bundle install
Assuming your model name is Model
and field name is attachment
class ModelDashboard < Administrate::BaseDashboard
ATTRIBUTE_TYPES = {
attachment: Field::ActiveStorage,
}
# ...
Then add :attachment
to FORM_ATTRIBUTES
and SHOW_PAGE_ATTRIBUTES
.
Adding :attachment
COLLECTION_ATTRIBUTES
will work but will probably look too big.
Assuming your model name is Model
and field name is attachments
the process is identical the only issue is that the form field isn't being permitted, in order to permit it we apply the following method to the dashboard:
class ModelDashboard < Administrate::BaseDashboard
ATTRIBUTE_TYPES = {
attachments: Field::ActiveStorage,
}
# ...
FORM_ATTRIBUTES = {
#...
:attachments
}
# permitted for has_many_attached
def permitted_attributes
super + [:attachments => []]
end
I know it is not ideal, if you have a workaround please submit a PR.
In order to prevent N+1 queries from active storage you have to modify your admin model controller, below an example for a model called User
and with attached avatars
module Admin
class UsersController < Admin::ApplicationController
def index
super
@resources = User.with_attached_avatars.
page(params[:page]).
per(10)
end
end
end
In order to allow the user to delete an attachment using the admin dashboard you need to do the following:
- create a controller action with a
delete
route - point the
Field::ActiveStorage
field to that route
here is an example (send the route name as a symbol):
class ModelDashboard < Administrate::BaseDashboard
ATTRIBUTE_TYPES = {
attachment: Field::ActiveStorage.with_options({destroy_path: :custom_active_storage_destroy_path}),
}
# ...
Your routes.rb
file must point to a controller action with method delete
which should contain the following piece of code (you can modify to your own liking).
FOR SECURITY REASONS please check if the current user is allowed to remove such file
def remove_attachment
attachment = ActiveStorage::Attachment.find(params[:attachment_id])
attachment.purge
redirect_back(fallback_location: "/")
end
Only the following needs to change in order for the field to be url_only
class ModelDashboard < Administrate::BaseDashboard
ATTRIBUTE_TYPES = {
attachments: Field::ActiveStorage.with_options({url_only: true}),
# ...
}
# ...
end
This will preview thumbnails in the index page and if you're using has_many
it will show the first one as a thumbnail and a count of the total attached files
class ModelDashboard < Administrate::BaseDashboard
ATTRIBUTE_TYPES = {
attachments: Field::ActiveStorage.with_options({show_in_index: true}),
# ...
}
# ...
end
Supply the size of the image preview inside the show page check out the mini_magic documentation for resize
class ModelDashboard < Administrate::BaseDashboard
ATTRIBUTE_TYPES = {
attachments: Field::ActiveStorage.with_options({show_preview_size: '150x200>'}),
# ...
}
# ...
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. You can use rails generate administrate:assets:javascripts
to be able to customize Administrate JavaScripts in your application.
- upload single file
- adding image support through url_for to support 3rd party cloud storage
- use html 5 video element for video files
- use html audio element for audio files
- download link to other files
- preview videos
- preview pdfs
- upload multiple files
- find a way to delete attachments
- preview office files as pictures
- contributers are welcome (code, suggestions, and bugs).
- please document your code.
- add your name to the
contribute.md
.
Please note that this is my first gem :) i might have gotten some stuff wrong PR's are always welcome.
Based on the Administrate::Field::Image template, and inspired by Administrate::Field::Paperclip.