👩💻
Pushing the Boundaries with ActiveStorage
Andrea Fomera - Rails World 2024
Today we’ll cover
Today we’ll cover
Hello, I’m Andrea Fomera 👋
Hello, I’m Andrea Fomera 👋
Hello, I’m Andrea Fomera 👋
Hello, I’m Andrea Fomera 👋
Before we get started
Everything written for this talk
Let’s talk about ActiveStorage
“Active Storage facilitates uploading files to a cloud storage service like Amazon S3, Google Cloud Storage, or Microsoft Azure Storage and attaching those files to Active Record objects. It comes with a local disk-based service for development and testing and supports mirroring files to subordinate services for backups and migrations.”
ActiveStorage Simplifies File Uploads
ActiveStorage’s Core Tables
active_storage_blobs
Attribute | Type |
key | String |
filename | String |
metadata | Text |
service_name | String |
byte_size | BigInt |
checksum | String |
created_at | DateTime |
active_storage_attachments
Attribute | Type |
name | String |
record_type | String |
record_id | BigInt |
created_at | DateTime |
active_storage_variant_records
Attribute | Type |
blob_id | BigInt |
variation_digest | String |
* used only if you have track_variants enabled
How’s this work in practice?
How’s this work in practice?
Can we be more efficient?
Media Library to the Rescue
Building Custom Services with ActiveStorage
Create your own Service
Requirements for your own Service
Let’s implement Wistia as a Service
Handling Uploads
LET’S BREAK ALL THE RULES.
Let’s… push the boundaries
wistia:hashed_id:file_name
Introducing ActiveStorage::Providers
What is a provider?
Developer API
Developer API
Developer API
Implementation
providers_for class method
Recap of ActiveStorage Providers
You can define multiple providers…
Product Owners introduce new Requirements
Product Owners introduce new Requirements
Let’s extend add_provider_hooks
Frontend for Pexels
Demo Video 1
How do we implement this?
1. Custom Form Builder
2. Single Image Uploader View
2. Single Image Uploader View
3. PexelsController
4. Displaying & Selecting an Image
pexels:photoURL
Some Thoughts
Providers are Reusable!
Building a Media Library
First a demo video
This code may not be production ready.
ActiveStorage Schema (revisited!)
ActiveStorage Schema (revisited!)
Adding a User Reference
Adding a User Reference
Minimum Viable Product approach
Minimum Viable Product approach
Benefits of this approach
Benefits of this approach
Querying a User’s blobs
Leverage what Rails provides
It’s Expandable
Selecting from the Media Library just uses the existing Blob
Using ActiveStorage at Scale
Searching / Filtering
Extending ActiveStorage::Blob
Well Known Blobs
Preventing Deletion on Staging / QA
(Override the delete method and include the Blob when on staging)
Things to know
Set Current.user in Background Jobs / Rake Tasks
Enable providers on the has_one_attached association
Takeaways
Think outside the boundaries
Custom Services can allow you to do unique and new things
Thank you for your time
Slides / Code
Slides / Speaking Notes
Code with working demos:
https://github.com/afomera/rails-world-2024
Find me online: