diff --git a/app/models/tenant.rb b/app/models/tenant.rb index ed810c223..e39d3cd9c 100644 --- a/app/models/tenant.rb +++ b/app/models/tenant.rb @@ -112,10 +112,14 @@ class Tenant < ApplicationRecord end def self.subfolder_path - if default? + subfolder_path_for(current_schema) + end + + def self.subfolder_path_for(schema) + if schema == "public" "" else - File.join("tenants", current_schema) + File.join("tenants", schema) end end @@ -178,12 +182,15 @@ class Tenant < ApplicationRecord end def rename_storage - return unless ActiveStorage::Blob.service.is_a?(ActiveStorage::Service::TenantDiskService) + service = ActiveStorage::Blob.service + + return unless service.respond_to?(:tenant_root_for) + + old_storage = service.tenant_root_for(schema_before_last_save) - old_storage = File.join(ActiveStorage::Blob.service.root, "tenants", schema_before_last_save) return unless File.directory?(old_storage) - new_storage = File.join(ActiveStorage::Blob.service.root, "tenants", schema) + new_storage = service.tenant_root_for(schema) File.rename(old_storage, new_storage) end diff --git a/app/lib/active_storage/service/tenant_disk_service.rb b/lib/active_storage/service/tenant_disk_service.rb similarity index 52% rename from app/lib/active_storage/service/tenant_disk_service.rb rename to lib/active_storage/service/tenant_disk_service.rb index 836983d03..3d7410eea 100644 --- a/app/lib/active_storage/service/tenant_disk_service.rb +++ b/lib/active_storage/service/tenant_disk_service.rb @@ -2,11 +2,19 @@ require "active_storage/service/disk_service" module ActiveStorage class Service::TenantDiskService < Service::DiskService + def tenant_root + File.join(root, Tenant.subfolder_path) + end + + def tenant_root_for(...) + File.join(root, Tenant.subfolder_path_for(...)) + end + def path_for(key) if Tenant.default? super else - super.sub(root, File.join(root, Tenant.subfolder_path)) + super.sub(root, tenant_root) end end end diff --git a/spec/lib/active_storage/service/tenant_disk_service_spec.rb b/spec/lib/active_storage/service/tenant_disk_service_spec.rb new file mode 100644 index 000000000..28ad722d1 --- /dev/null +++ b/spec/lib/active_storage/service/tenant_disk_service_spec.rb @@ -0,0 +1,12 @@ +require "rails_helper" +require "active_storage/service/tenant_disk_service" + +describe ActiveStorage::Service::TenantDiskService do + describe "#tenant_root_for" do + it "returns the folder of the given tenant" do + service = ActiveStorage::Service::TenantDiskService.new(root: Rails.root.join("tmp", "storage")) + + expect(service.tenant_root_for("megawonder")).to eq "#{Rails.root}/tmp/storage/tenants/megawonder" + end + end +end diff --git a/spec/models/tenant_spec.rb b/spec/models/tenant_spec.rb index ca6be56be..e7244c226 100644 --- a/spec/models/tenant_spec.rb +++ b/spec/models/tenant_spec.rb @@ -424,9 +424,13 @@ describe Tenant do FileUtils.rm_rf(File.join(ActiveStorage::Blob.service.root, "tenants", "notypo")) end - it "does nothing when the active storage blob service is not a TenantDiskService" do - disk_service = ActiveStorage::Service::DiskService.new(root: ActiveStorage::Blob.service.root) - allow(ActiveStorage::Blob).to receive(:service).and_return(disk_service) + it "does nothing when the active storage blob service cannot manage tenants" do + allow(Rails.configuration.active_storage).to receive(:service_configurations) do + ActiveSupport::ConfigurationFile.parse(Rails.root.join("config/storage.yml")).tap do |config| + config[Rails.configuration.active_storage.service.to_s]["service"] = "Disk" + end + end + tenant = create(:tenant, schema: "typo") expect(File).not_to receive(:rename)