If you using Apartment and use ActiveStorage disk as a storage service, you might want to organise storage folder per-tenant or something like this:
storage/tenant1/
storage/tenant2/
storage/other_tenant/
This can be achieved by extending the default ActiveStorage disk service class
# lib/active_storage/service/tenant_service.rb
# extend disk service
require "active_storage/service/disk_service"
module ActiveStorage
class Service::TenantService < Service::DiskService
def path_for(key)
current_tenant = Apartment::Tenant.current
File.join root, current_tenant, folder_for(key), key
end
end
end
Change storage config for local using new service previously created
# config/storage.yml
test:
service: Disk
root: <%= Rails.root.join("tmp/storage") %>
local:
service: Tenant
root: <%= Rails.root.join("storage") %>
** Don't forget to restart rails server
Benefits:
- Easy to cleanup storage when tenant dropped
Apartment::Tenant.drop('tenant_name')
`rm -rf storage/tenant_name`
- check disk usage per tenant
- per tenant backup?
Top comments (2)
Hey Adnan, thanks for the post, for S3 service I just solved it this way:
Then:
That awesome 🤩 !