Use initializer to configure tenant mode

This commit is contained in:
Kevin McConnell
2025-12-10 17:26:53 +00:00
parent a5580666a7
commit 0e443d3602
7 changed files with 38 additions and 8 deletions
+5 -1
View File
@@ -1,9 +1,13 @@
module MultiTenant
extend ActiveSupport::Concern
included do
cattr_accessor :multi_tenant, default: false
end
class_methods do
def accepting_signups?
ENV.fetch("MULTI_TENANT", "false") == "true" || Account.none?
multi_tenant || Account.none?
end
end
end
+3
View File
@@ -67,4 +67,7 @@ Rails.application.configure do
# Load test helpers
config.autoload_paths += %w[ test/test_helpers ]
# Enable multi-tenant mode for tests
config.x.multi_tenant = true
end
+3
View File
@@ -0,0 +1,3 @@
Rails.application.config.after_initialize do
Account.multi_tenant = ENV["MULTI_TENANT"] == "true" || Rails.configuration.x.multi_tenant == true
end
+1 -1
View File
@@ -67,7 +67,7 @@ class SignupsControllerTest < ActionDispatch::IntegrationTest
test "redirects to session#new when single_tenant and user exists" do
users(:david)
in_single_tenant_mode do
with_multi_tenant_mode(false) do
untenanted do
get new_signup_path
+22
View File
@@ -0,0 +1,22 @@
require "test_helper"
class MultiTenantTest < ActiveSupport::TestCase
test "accepting_signups? is true when multi_tenant is enabled" do
with_multi_tenant_mode(true) do
assert Account.accepting_signups?
end
end
test "accepting_signups? is false when multi_tenant is disabled and accounts exist" do
with_multi_tenant_mode(false) do
assert_not Account.accepting_signups?
end
end
test "accepting_signups? is true when multi_tenant is disabled but no accounts exist" do
with_multi_tenant_mode(false) do
Account.delete_all
assert Account.accepting_signups?
end
end
end
-2
View File
@@ -1,6 +1,4 @@
ENV["RAILS_ENV"] ||= "test"
ENV["MULTI_TENANT"] ||= "true"
require_relative "../config/environment"
require "rails/test_help"
+4 -4
View File
@@ -58,11 +58,11 @@ module SessionTestHelper
integration_session.default_url_options[:script_name] = original_script_name
end
def in_single_tenant_mode
previous_multi_tenant = ENV["MULTI_TENANT"]
ENV["MULTI_TENANT"] = "false"
def with_multi_tenant_mode(enabled)
previous = Account.multi_tenant
Account.multi_tenant = enabled
yield
ensure
ENV["MULTI_TENANT"] = previous_multi_tenant
Account.multi_tenant = previous
end
end