Use initializer to configure tenant mode
This commit is contained in:
@@ -1,9 +1,13 @@
|
|||||||
module MultiTenant
|
module MultiTenant
|
||||||
extend ActiveSupport::Concern
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
|
included do
|
||||||
|
cattr_accessor :multi_tenant, default: false
|
||||||
|
end
|
||||||
|
|
||||||
class_methods do
|
class_methods do
|
||||||
def accepting_signups?
|
def accepting_signups?
|
||||||
ENV.fetch("MULTI_TENANT", "false") == "true" || Account.none?
|
multi_tenant || Account.none?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -67,4 +67,7 @@ Rails.application.configure do
|
|||||||
|
|
||||||
# Load test helpers
|
# Load test helpers
|
||||||
config.autoload_paths += %w[ test/test_helpers ]
|
config.autoload_paths += %w[ test/test_helpers ]
|
||||||
|
|
||||||
|
# Enable multi-tenant mode for tests
|
||||||
|
config.x.multi_tenant = true
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
Rails.application.config.after_initialize do
|
||||||
|
Account.multi_tenant = ENV["MULTI_TENANT"] == "true" || Rails.configuration.x.multi_tenant == true
|
||||||
|
end
|
||||||
@@ -67,7 +67,7 @@ class SignupsControllerTest < ActionDispatch::IntegrationTest
|
|||||||
test "redirects to session#new when single_tenant and user exists" do
|
test "redirects to session#new when single_tenant and user exists" do
|
||||||
users(:david)
|
users(:david)
|
||||||
|
|
||||||
in_single_tenant_mode do
|
with_multi_tenant_mode(false) do
|
||||||
untenanted do
|
untenanted do
|
||||||
get new_signup_path
|
get new_signup_path
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -1,6 +1,4 @@
|
|||||||
ENV["RAILS_ENV"] ||= "test"
|
ENV["RAILS_ENV"] ||= "test"
|
||||||
ENV["MULTI_TENANT"] ||= "true"
|
|
||||||
|
|
||||||
require_relative "../config/environment"
|
require_relative "../config/environment"
|
||||||
|
|
||||||
require "rails/test_help"
|
require "rails/test_help"
|
||||||
|
|||||||
@@ -58,11 +58,11 @@ module SessionTestHelper
|
|||||||
integration_session.default_url_options[:script_name] = original_script_name
|
integration_session.default_url_options[:script_name] = original_script_name
|
||||||
end
|
end
|
||||||
|
|
||||||
def in_single_tenant_mode
|
def with_multi_tenant_mode(enabled)
|
||||||
previous_multi_tenant = ENV["MULTI_TENANT"]
|
previous = Account.multi_tenant
|
||||||
ENV["MULTI_TENANT"] = "false"
|
Account.multi_tenant = enabled
|
||||||
yield
|
yield
|
||||||
ensure
|
ensure
|
||||||
ENV["MULTI_TENANT"] = previous_multi_tenant
|
Account.multi_tenant = previous
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user