All Projects â†’ Dreamersoul â†’ Administrate Field Active_storage

Dreamersoul / Administrate Field Active_storage

Licence: mit
support active storage in administrate

Programming Languages

ruby
36898 projects - #4 most used programming language

Labels

Projects that are alternatives of or similar to Administrate Field Active storage

Stimulus reflex expo
StimulusReflex demos
Stars: ✭ 85 (-7.61%)
Mutual labels:  rails
Instuigram
🎓 Learning Ruby on Rails through building the Instagram Application.
Stars: ✭ 88 (-4.35%)
Mutual labels:  rails
Translate enum
Easily Translate Enums in Rails
Stars: ✭ 90 (-2.17%)
Mutual labels:  rails
Rails React Boilerplate
Ruby on Rails, React, Webpack 4 boilerplate app.
Stars: ✭ 86 (-6.52%)
Mutual labels:  rails
Covid Volunteers
Organizing and matching volunteers with COVID-19 projects
Stars: ✭ 87 (-5.43%)
Mutual labels:  rails
Muvee
Ξv: (mew-vee) Netflix, for your home. WIP
Stars: ✭ 89 (-3.26%)
Mutual labels:  rails
Rails Security Checklist
🔑 Community-driven Rails Security Checklist (see our GitHub Issues for the newest checks that aren't yet in the README)
Stars: ✭ 1,265 (+1275%)
Mutual labels:  rails
Liquid Rails
Renders liquid templates with layout and partial support
Stars: ✭ 91 (-1.09%)
Mutual labels:  rails
Simpleadmin Rails
SimpleAdmin - Dashboard for modern website without wasting time
Stars: ✭ 87 (-5.43%)
Mutual labels:  rails
Reactchat
A chat app built with React.js and ActionCable in Ruby on Rails 5.1
Stars: ✭ 90 (-2.17%)
Mutual labels:  rails
Rails or
Cleaner syntax for writing OR Query in Rails 5, 6. And also add #or support to Rails 3 and 4.
Stars: ✭ 86 (-6.52%)
Mutual labels:  rails
Csso Rails
CSS Optimizer(csso) ruby wrapper for Rails Asset pipeline
Stars: ✭ 86 (-6.52%)
Mutual labels:  rails
Jquery Slick Rails
Integrates Slick carousel, a jQuery plugin, into your Rails app.
Stars: ✭ 89 (-3.26%)
Mutual labels:  rails
Chaskiq
A full featured Live Chat, Support & Marketing platform, alternative to Intercom, Drift, Crisp, etc ...
Stars: ✭ 1,263 (+1272.83%)
Mutual labels:  rails
Activeadmin
The administration framework for Ruby on Rails applications.
Stars: ✭ 9,096 (+9786.96%)
Mutual labels:  rails
Graphjin
GraphJin - Build APIs in 5 minutes with GraphQL. An instant GraphQL to SQL compiler.
Stars: ✭ 1,264 (+1273.91%)
Mutual labels:  rails
Errdo
A simple plugin to handle, log, and customize production errors in Rails applications
Stars: ✭ 88 (-4.35%)
Mutual labels:  rails
Validates formatting of
Common Rails validations wrapped in a gem.
Stars: ✭ 91 (-1.09%)
Mutual labels:  rails
Javlibrary Rails
Elegant way to build your own javlibrary database.
Stars: ✭ 91 (-1.09%)
Mutual labels:  rails
Gorailsyourself
A suite of useful functions needed when porting/mixing Go/Rails code.
Stars: ✭ 89 (-3.26%)
Mutual labels:  rails

Administrate::Field::ActiveStorage

rails CI

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 by basecamp

How To Use:

Add administrate-field-active_storage and mini_magick to your Gemfile (rails 6):

gem 'administrate-field-active_storage'
gem "image_processing"

for rails 5.x use the following

gem 'administrate-field-active_storage', "0.1.8"

Install:

$ bundle install

has_one_attached:

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.

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:

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.

Note: Rails 6 introduced a new config to determine the behavior on updates to has_many_attached. Setting Rails.application.config.active_storage.replace_on_assign_to_many to true will overwrite any existing values (purging the old ones), and setting it to false will append the new values.

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

module Admin
  class UsersController < ApplicationController
    def scoped_resource
      resource_class.with_attached_avatars
    end
  end
end

Removing/Deleting an Attachment

Administrate::Field::ActiveStorage expects the presence of a route DELETE /<namespace>/<resource>/:id/:attachment_name, which will receive an optional parameter attachment_id in the case of an ActiveStorage::Attached::Many. For instance:

# routes.rb
...
namespace :admin do
  ...
  resources :users do
    delete :avatars, on: :member, action: :destroy_avatar
  end
end

# app/controllers/admin/users_controller.rb
module Admin
  class UsersController < ApplicationController

    # For illustrative purposes only.
    #
    # **SECURITY NOTICE**: first verify whether current user is authorized to perform the action.
    def destroy_avatar
      avatar = requested_resource.avatars.find(params[:attachment_id])
      avatar.purge
      redirect_back(fallback_location: requested_resource)
    end
  end
end

For has_one_attached cases, you will use:

# routes.rb
...
namespace :admin do
  ...
  resources :users do
    delete :avatar, on: :member, action: :destroy_avatar
  end
end

# app/controllers/admin/users_controller.rb
module Admin
  class UsersController < ApplicationController

    # For illustrative purposes only.
    #
    # **SECURITY NOTICE**: first verify whether current user is authorized to perform the action.
    def destroy_avatar
      avatar = requested_resource.avatar
      avatar.purge
      redirect_back(fallback_location: requested_resource)
    end
  end
end

This route can be customized with destroy_url. The option expects a proc receiving 3 arguments: the Administrate namespace, the resource, and the attachment. The proc can return anything accepted by link_to:

# routes.rb
delete :custom_user_avatar_destroy, to: 'users#destroy_avatar'

# user_dashboard.rb
class UserDashboard < Administrate::BaseDashboard
  ATTRIBUTE_TYPES = {
    avatars: Field::ActiveStorage.with_options(
      destroy_url: proc do |namespace, resource, attachment|
        [:custom_user_avatar_destroy, { attachment_id: attachment.id }]
      end
    ),
    # ...
  end
  # ...
end

Options

Various options can be passed to Administrate::Field::ActiveStorage#with_options as illustrated below:

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 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. 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 template, and inspired by Administrate::Field::Paperclip.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].