Merge pull request #5492 from consuldemocracy/move_tenant_service_to_load_path

Move custom ActiveStorage service to $LOAD_PATH
This commit is contained in:
Javi Martín
2024-04-17 16:29:20 +02:00
committed by GitHub
4 changed files with 40 additions and 9 deletions

View File

@@ -112,10 +112,14 @@ class Tenant < ApplicationRecord
end end
def self.subfolder_path def self.subfolder_path
if default? subfolder_path_for(current_schema)
end
def self.subfolder_path_for(schema)
if schema == "public"
"" ""
else else
File.join("tenants", current_schema) File.join("tenants", schema)
end end
end end
@@ -178,12 +182,15 @@ class Tenant < ApplicationRecord
end end
def rename_storage 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) 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) File.rename(old_storage, new_storage)
end end

View File

@@ -2,11 +2,19 @@ require "active_storage/service/disk_service"
module ActiveStorage module ActiveStorage
class Service::TenantDiskService < Service::DiskService 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) def path_for(key)
if Tenant.default? if Tenant.default?
super super
else else
super.sub(root, File.join(root, Tenant.subfolder_path)) super.sub(root, tenant_root)
end end
end end
end end

View File

@@ -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

View File

@@ -424,9 +424,13 @@ describe Tenant do
FileUtils.rm_rf(File.join(ActiveStorage::Blob.service.root, "tenants", "notypo")) FileUtils.rm_rf(File.join(ActiveStorage::Blob.service.root, "tenants", "notypo"))
end end
it "does nothing when the active storage blob service is not a TenantDiskService" do it "does nothing when the active storage blob service cannot manage tenants" do
disk_service = ActiveStorage::Service::DiskService.new(root: ActiveStorage::Blob.service.root) allow(Rails.configuration.active_storage).to receive(:service_configurations) do
allow(ActiveStorage::Blob).to receive(:service).and_return(disk_service) 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") tenant = create(:tenant, schema: "typo")
expect(File).not_to receive(:rename) expect(File).not_to receive(:rename)