Andrea Fomera - Pushing the boundaries with ActiveStorage - Rails World 2024

Explore advanced ActiveStorage features in Rails: custom providers, media libraries, performance optimization, security, and third-party integrations with Andrea Fomera.

Key takeaways
  • ActiveStorage provides a flexible system for file management in Rails applications, supporting local storage and cloud providers like S3, Google Cloud, and Azure

  • Custom providers can be created to integrate third-party services like Wistia and Pexels, extending ActiveStorage’s functionality beyond traditional file storage

  • Adding a user_id to active_storage_attachments table enables tracking file ownership and building user-specific media libraries

  • The providers_for method allows defining multiple providers for a single attachment, making it possible to handle files from different sources

  • Media libraries can be built by:

    • Adding search functionality for filenames
    • Implementing user-specific file filtering
    • Creating custom upload interfaces
    • Integrating third-party services seamlessly
  • Performance optimizations can include:

    • Background job processing for file operations
    • Preventing duplicate file uploads
    • Managing file variations efficiently
  • Security considerations:

    • Use short-lived expiring tokens for file access
    • Implement proper user authentication
    • Control file deletion permissions
  • Custom services should implement key methods:

    • upload
    • delete
    • download
    • download_chunk
    • public_url
  • Integration with third-party services can be achieved through:

    • Custom providers
    • JavaScript API integration
    • Webhook handling
    • Metadata management
  • ActiveStorage’s architecture relies on three main tables:

    • active_storage_blobs
    • active_storage_attachments
    • active_storage_variant_records