Extract methods to get tenant subfolder/file paths

We were using the same logic in many different places, so we're
simplifying the code. I'm not convinced about the method names, though,
so we might change them in the future.

Note using this method for the default tenant in the `TenantDiskService`
class resulted in a `//` in the path, which is probably harmless but
very ugly and it also generates a different key than the one we got
until now. I've added an extra test to make sure that isn't the case.
This commit is contained in:
Javi Martín
2022-10-19 20:18:06 +02:00
parent 2f312bf474
commit edd47877c2
6 changed files with 17 additions and 21 deletions

View File

@@ -89,15 +89,7 @@ class MachineLearning
end end
def tenant_data_folder def tenant_data_folder
File.join(tenant_subfolder, "machine_learning", "data").delete_prefix("/") Tenant.path_with_subfolder("machine_learning/data")
end
def tenant_subfolder
if Tenant.default?
""
else
File.join("tenants", Tenant.current_schema)
end
end end
def data_output_files def data_output_files

View File

@@ -71,6 +71,18 @@ class Tenant < ApplicationRecord
end end
end end
def self.subfolder_path
if default?
""
else
File.join("tenants", current_schema)
end
end
def self.path_with_subfolder(filename_or_folder)
File.join(subfolder_path, filename_or_folder).delete_prefix("/")
end
def self.default? def self.default?
current_schema == "public" current_schema == "public"
end end

View File

@@ -14,8 +14,4 @@ Disallow: /*?*locale-switcher
Disallow: /*?*filter Disallow: /*?*filter
Disallow: user_id Disallow: user_id
<% if Tenant.default? %> Sitemap: <%= "#{root_url}#{Tenant.path_with_subfolder("sitemap.xml")}" %>
Sitemap: <%= "#{root_url}sitemap.xml" %>
<% else %>
Sitemap: <%= "#{root_url}tenants/#{Tenant.current_schema}/sitemap.xml" %>
<% end %>

View File

@@ -10,12 +10,7 @@ SitemapGenerator::Sitemap.verbose = false if Rails.env.test?
Tenant.run_on_each do Tenant.run_on_each do
SitemapGenerator::Sitemap.default_host = root_url(Tenant.current_url_options) SitemapGenerator::Sitemap.default_host = root_url(Tenant.current_url_options)
SitemapGenerator::Sitemap.sitemaps_path = Tenant.subfolder_path
if Tenant.default?
SitemapGenerator::Sitemap.sitemaps_path = nil
else
SitemapGenerator::Sitemap.sitemaps_path = "tenants/#{Tenant.current_schema}"
end
SitemapGenerator::Sitemap.create do SitemapGenerator::Sitemap.create do
add help_path add help_path

View File

@@ -6,7 +6,7 @@ module ActiveStorage
if Tenant.default? if Tenant.default?
super super
else else
super.sub(root, File.join(root, "tenants", Tenant.current_schema)) super.sub(root, File.join(root, Tenant.subfolder_path))
end end
end end
end end

View File

@@ -5,6 +5,7 @@ describe Attachable do
file_path = build(:image).file_path file_path = build(:image).file_path
expect(file_path).to include "storage/" expect(file_path).to include "storage/"
expect(file_path).not_to include "storage//"
expect(file_path).not_to include "tenants" expect(file_path).not_to include "tenants"
end end