# Administrate::Field::ActiveStorage ![rails](https://img.shields.io/badge/rails-%3E%3D5.2.0-red.svg) ## Things To Know: - To preview pdf files you need to install `mupdf` or `Poppler`. - To preview video files you need to install `ffmpeg`. - To preview Office files as pictures you need to install [activestorage-office-previewer](https://github.com/basecamp/activestorage-office-previewer) by basecamp ## How To Use: Add `administrate-field-active_storage` and `mini_magick` to your Gemfile (rails 6): ```ruby gem 'administrate-field-active_storage' gem 'mini_magick' ``` for rails 5.x use the following ```ruby gem 'administrate-field-active_storage' -v 0.1.8 ``` Install: ``` $ bundle install ``` ### `has_one_attached`: Assuming your model name is `Model` and field name is `attachment` ```ruby 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. ### `has_many_attached`: 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: ```ruby 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. ### Prevent N+1 queries 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 ```ruby module Admin class UsersController < Admin::ApplicationController def scoped_resource resource_class.with_attached_avatars end end end ``` ### Removing/Deleting an Attachment In order to allow the user to delete an attachment using the admin dashboard you need to do the following: 1. create a controller action with a `delete` route 2. point the `Field::ActiveStorage` field to that route here is an example (send the route name as a symbol): ```ruby 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 ```ruby def remove_attachment attachment = ActiveStorage::Attachment.find(params[:attachment_id]) attachment.purge redirect_back(fallback_location: "/") end ``` ## Options Various options can be passed to `Administrate::Field::ActiveStorage#with_options` as illustrated below: ```rb class ModelDashboard < Administrate::BaseDashboard ATTRIBUTE_TYPES = { attachments: Field::ActiveStorage.with_options(index_display_preview: false), # ... } # ... end ``` ### show_display_preview Display attachment preview. Defaults to `true`. ### index_display_preview Displays the first attachment (which is the only attachment in case of `has_one`) in the `index` action. Defaults to `true`. ### index_preview_size and show_preview_size Indicate the size of the image preview for the `index` and `show` actions, respectively. Refer to [mini_magic#resize_to_limit](https://github.com/janko/image_processing/blob/master/doc/minimagick.md#methods) for documentation. Default to `[150, 150]` and `[800, 800]`, respectively. ### index_display_count Displays the number of attachments in the `index` action. Defaults to `true` if number of attachments is not 1. ### direct_upload Enables direct upload from the browser to the cloud. Defaults to `false`. 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 - [x] use html 5 video element for video files - [x] use html audio element for audio files - [x] download link to other files - [x] preview videos - [x] preview pdfs - [x] upload multiple files - [x] find a way to delete attachments - [x] preview office files as pictures ## Contribution Guide: 1. contributers are welcome (code, suggestions, and bugs). 2. please document your code. 3. add your name to the `contribute.md`. --- Based on the [Administrate::Field::Image](https://github.com/thoughtbot/administrate-field-image) template, and inspired by [Administrate::Field::Paperclip](https://github.com/picandocodigo/administrate-field-paperclip).