BetterStorage

  • Prefix ActiveStorage uploads with dev and customized datetime format.
  • Protect production files from deletion in development environment.
  • public_url method for ActiveStorage attachments and blobs.

Usage

# better_storage.rb initializer

BetterStorage.configure do |config|
  config.namespace = "storage" # prefix for ActiveStorage uploads default: nil
  config.protect_production_files = true # default: Rails.env.development?
  config.prefix_date_format = "%Y/%m/%d" # default: "%Y%m"
end
class User < ApplicationRecord
  has_one_attached :avatar do |attachable|
    attachable.variant :thumb, resize_to_limit: [120, 120]
  end
end

user = User.first
user.avatar.public_url
user.avatar.public_url(:thumb)

Installation

Add this line to your application's Gemfile:

gem "better_storage"

Warning

ActiveStorage.track_variants = true is assumed. This gem is not tested with ActiveStorage.track_variants = false. With the false setting, variant_class is ActiveStorage::Variant instead of ActiveStorage::VariantWithRecord. ActiveStorage::Variant implements its own special blob key.

Contributing

Contribution directions go here.

License

The gem is available as open source under the terms of the MIT License.