From 2ba588f3a890cc0298b2a220a6119a82bd0aa1c2 Mon Sep 17 00:00:00 2001 From: Christopher Winslett Date: Wed, 10 Jan 2024 06:36:58 -0600 Subject: [PATCH 1/2] add a tenant change hook --- lib/acts_as_tenant.rb | 10 ++++++++ lib/acts_as_tenant/configuration.rb | 7 ++++++ spec/acts_as_tenant/configuration_spec.rb | 29 +++++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/lib/acts_as_tenant.rb b/lib/acts_as_tenant.rb index 20bd25bb..4a9cb685 100644 --- a/lib/acts_as_tenant.rb +++ b/lib/acts_as_tenant.rb @@ -16,6 +16,16 @@ module ActsAsTenant class Current < ActiveSupport::CurrentAttributes attribute :current_tenant, :acts_as_tenant_unscoped + + def current_tenant=(tenant) + r = super + configuration.tenant_change_hook.call(tenant) if configuration.tenant_change_hook.present? + r + end + + def configuration + Module.nesting.last.class_variable_get("@@configuration") + end end class << self diff --git a/lib/acts_as_tenant/configuration.rb b/lib/acts_as_tenant/configuration.rb index e75ebcfc..051cdf44 100644 --- a/lib/acts_as_tenant/configuration.rb +++ b/lib/acts_as_tenant/configuration.rb @@ -1,6 +1,7 @@ module ActsAsTenant class Configuration attr_writer :require_tenant, :pkey + attr_reader :tenant_change_hook def require_tenant @require_tenant ||= false @@ -27,5 +28,11 @@ def job_scope=(scope) scope end end + + def tenant_change_hook=(hook) + raise(ArgumentError, "tenant_change_hook must be a Proc") unless hook.is_a?(Proc) + @tenant_change_hook = hook + end + end end diff --git a/spec/acts_as_tenant/configuration_spec.rb b/spec/acts_as_tenant/configuration_spec.rb index 9d1f33ac..0bbfdddf 100644 --- a/spec/acts_as_tenant/configuration_spec.rb +++ b/spec/acts_as_tenant/configuration_spec.rb @@ -59,5 +59,34 @@ expect(ActsAsTenant.should_require_tenant?).to eq(false) end + + it "runs a hook on current_tenant" do + truthy = false + ActsAsTenant.configure do |config| + config.tenant_change_hook = lambda do |tenant| + truthy = true + end + end + + ActsAsTenant.current_tenant = "foobar" + + expect(truthy).to eq(true) + end + + it "runs a hook on with_tenant" do + truthy = false + ActsAsTenant.configure do |config| + config.tenant_change_hook = lambda do |tenant| + truthy = true + end + end + + ActsAsTenant.with_tenant("foobar") do + # do nothing + end + + expect(truthy).to eq(true) + end + end end From df90fcc2510e6673d5e0d31f341572155b77f40c Mon Sep 17 00:00:00 2001 From: Christopher Winslett Date: Wed, 10 Jan 2024 13:59:06 -0600 Subject: [PATCH 2/2] cleanup syntax for standardrb --- lib/acts_as_tenant.rb | 8 ++++---- lib/acts_as_tenant/configuration.rb | 1 - spec/acts_as_tenant/configuration_spec.rb | 1 - 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/acts_as_tenant.rb b/lib/acts_as_tenant.rb index 4a9cb685..f4074015 100644 --- a/lib/acts_as_tenant.rb +++ b/lib/acts_as_tenant.rb @@ -18,13 +18,13 @@ class Current < ActiveSupport::CurrentAttributes attribute :current_tenant, :acts_as_tenant_unscoped def current_tenant=(tenant) - r = super - configuration.tenant_change_hook.call(tenant) if configuration.tenant_change_hook.present? - r + super.tap do + configuration.tenant_change_hook.call(tenant) if configuration.tenant_change_hook.present? + end end def configuration - Module.nesting.last.class_variable_get("@@configuration") + Module.nesting.last.class_variable_get(:@@configuration) end end diff --git a/lib/acts_as_tenant/configuration.rb b/lib/acts_as_tenant/configuration.rb index 051cdf44..c18685c2 100644 --- a/lib/acts_as_tenant/configuration.rb +++ b/lib/acts_as_tenant/configuration.rb @@ -33,6 +33,5 @@ def tenant_change_hook=(hook) raise(ArgumentError, "tenant_change_hook must be a Proc") unless hook.is_a?(Proc) @tenant_change_hook = hook end - end end diff --git a/spec/acts_as_tenant/configuration_spec.rb b/spec/acts_as_tenant/configuration_spec.rb index 0bbfdddf..c12edd4e 100644 --- a/spec/acts_as_tenant/configuration_spec.rb +++ b/spec/acts_as_tenant/configuration_spec.rb @@ -87,6 +87,5 @@ expect(truthy).to eq(true) end - end end