Use a different attachments folder per tenant
While this is not strictly necessary, it can help moving the data of one tenant to a different server or removing it. Note we're using subfolders inside the `tenants` subfolder. If we simply used subfolders with the schema names, if the schema names were, for instance, language codes like `es`, `en`, `it`, ... they would conflict with the default subfolders used by Active Storage.
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
local:
|
||||
service: Disk
|
||||
service: TenantDisk
|
||||
root: <%= Rails.root.join("storage") %>
|
||||
|
||||
test:
|
||||
service: Disk
|
||||
service: TenantDisk
|
||||
root: <%= Rails.root.join("tmp/storage") %>
|
||||
|
||||
# s3:
|
||||
|
||||
13
lib/active_storage/service/tenant_disk_service.rb
Normal file
13
lib/active_storage/service/tenant_disk_service.rb
Normal file
@@ -0,0 +1,13 @@
|
||||
require "active_storage/service/disk_service"
|
||||
|
||||
module ActiveStorage
|
||||
class Service::TenantDiskService < Service::DiskService
|
||||
def path_for(key)
|
||||
if Tenant.default?
|
||||
super
|
||||
else
|
||||
super.sub(root, File.join(root, "tenants", Tenant.current_schema))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
16
spec/models/attachable_spec.rb
Normal file
16
spec/models/attachable_spec.rb
Normal file
@@ -0,0 +1,16 @@
|
||||
require "rails_helper"
|
||||
|
||||
describe Attachable do
|
||||
it "stores attachments for the default tenant in the default folder" do
|
||||
file_path = build(:image).file_path
|
||||
|
||||
expect(file_path).to include "storage/"
|
||||
expect(file_path).not_to include "tenants"
|
||||
end
|
||||
|
||||
it "stores tenant attachments in a folder for the tenant" do
|
||||
allow(Tenant).to receive(:current_schema).and_return("image-master")
|
||||
|
||||
expect(build(:image).file_path).to include "storage/tenants/image-master/"
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user