Rails Active Storage

Rails introduced active storage recently has of the writing of this post. Active Storage is a built-in gem included on Rails 5.2 that handles file uploads to storage services from Amazon, Google, and Microsoft. Saving to local disk is also possible for development and test environments. Although there always been different methods of implementing this feature in Rails, such as with the paperclip Carrierwave, Dragonfly. It’s a more holistic approach implementing Active Storage into your application since its a baked right into the Rails framework already. Active storage does require external gems such as MiniMagick in order to perform transformations and previews of the user’s uploaded attachments.

Active Storage works by requiring two additional tables order to intergrate into your Rails application. These tables are active_storage_blobs and active_storage_attachments. The required tables are generated by running

rails active_storage: install

and

rails db: migrate

to execute the migration. This migation creates to tables active_storage_attachments and active_storage_blobs

Blob stores metadata like filename, content-type, byte size and checksum. The actual file is stored in the storage service or disk depending on your settings. A Blob can be attached to one or more Active Record objects through the Attachment join model.

Storing file attachments within your application provider could become expensive quick as a result it is recommended to use a cloud service such as AWS, Google Cloud and Microsoft Azures to name a few. Rails facilitate the configurations of these services by config/storage.yml file. Some cloud services provider require additional configurations in order to work with Active Storage.

By default, your local storage is used.

config/environments/development.rb

tells Rails which provider to use.

Once the proper tables are setup and the application is configured properly all that is left is the proper associations to be made within your models.

Active Storage supports has_one, has_many and many more associations by adding the suffix attached to the association such as has_one_attached.

These associations allow for images to be attached to the object identified in the association by calling attach. Other methods can be called such as attached? to determine if an object has many attachments, detach to delete the attachment but leaving its reference, while purge completely removes the attachment alone with its blob. You also have access to purge_later to remove the attachment through a queuing system.

There will be times where you would want to link an attachment on your page. To generate a download link one would call rails_blob_path(atttachment_name, disposition: "attachment")

Transforming attachments requires the use of third-party gems such as mini_magick. Mini Magick allows for certain files such as pdf to be preview and images to be resized and transform by calling variant and preview methods on the object. Both variant and preview accepts hash arguments. When previewing videos the first frame for the video is used.

Active Storage also allows for direct uploads transferring any attachment from the client directly to cloud services. In order to activate this feature Rails asset pipeline must be setup correctly by including activestorage.js in your application bundle. Once its enable using either npm or via Rail’s asset pipeline direct_upload: true is passed from the view to allow direct upload. Direct upload is integrated with a Javascript framework or if you want to customize a drag and drop feature its possible by calling the DirectUpload class. Direct upload Javascript events allow progress indication while the file upload.

Since Active Storage is a fairly new library its code base is easier to navigate and explore for the curious among us.

You could find the source code on github or by running bundle open activestorage from your commandline if you are using bundler.