From 458269a2e4ef1105fece4bf419f7ef09cfc0b28c Mon Sep 17 00:00:00 2001 From: Lucas Zhang Date: Fri, 12 Jan 2024 10:23:48 +0800 Subject: [PATCH 1/2] Add `config.global_records_identifier` configuration --- lib/acts_as_tenant.rb | 4 ++++ lib/acts_as_tenant/configuration.rb | 6 +++++- lib/acts_as_tenant/model_extensions.rb | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/acts_as_tenant.rb b/lib/acts_as_tenant.rb index f4074015..56ff40e8 100644 --- a/lib/acts_as_tenant.rb +++ b/lib/acts_as_tenant.rb @@ -58,6 +58,10 @@ def self.add_global_record_model model @@models_with_global_records.push(model) end + def self.global_records_identifier + ActsAsTenant.configuration.global_records_identifier + end + def self.fkey "#{@@tenant_klass}_id" end diff --git a/lib/acts_as_tenant/configuration.rb b/lib/acts_as_tenant/configuration.rb index c18685c2..b782173e 100644 --- a/lib/acts_as_tenant/configuration.rb +++ b/lib/acts_as_tenant/configuration.rb @@ -1,6 +1,6 @@ module ActsAsTenant class Configuration - attr_writer :require_tenant, :pkey + attr_writer :require_tenant, :pkey, :global_records_identifier attr_reader :tenant_change_hook def require_tenant @@ -11,6 +11,10 @@ def pkey @pkey ||= :id end + def global_records_identifier + @global_records_identifier ||= nil + end + def job_scope @job_scope || ->(relation) { relation.all } end diff --git a/lib/acts_as_tenant/model_extensions.rb b/lib/acts_as_tenant/model_extensions.rb index 053b9b85..9c253feb 100644 --- a/lib/acts_as_tenant/model_extensions.rb +++ b/lib/acts_as_tenant/model_extensions.rb @@ -23,7 +23,7 @@ def acts_as_tenant(tenant = :account, scope = nil, **options) if ActsAsTenant.current_tenant keys = [ActsAsTenant.current_tenant.send(pkey)].compact - keys.push(nil) if options[:has_global_records] + keys.push(ActsAsTenant.global_records_identifier) if options[:has_global_records] if options[:through] query_criteria = {options[:through] => {fkey.to_sym => keys}} From f6f3fa65545f58ea6ef85f1f4c081b2fb98bcbe4 Mon Sep 17 00:00:00 2001 From: Lucas Zhang Date: Fri, 12 Jan 2024 10:24:26 +0800 Subject: [PATCH 2/2] Update document --- CHANGELOG.md | 10 ++++++++++ README.md | 1 + 2 files changed, 11 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ae8d19b..285ff4e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,16 @@ ActsAsTenant.configure do |config| end ``` +* Add `config.global_records_identifier` configuration. [#332](https://github.com/ErwinM/acts_as_tenant/pull/332) + +This is helpful when you want to use a different global records identifier instead of `nil`: + +```ruby +ActsAsTenant.configure do |config| + config.global_records_identifier = 1 +end +``` + 1.0.1 ----- diff --git a/README.md b/README.md index b51d9171..1ed8859b 100644 --- a/README.md +++ b/README.md @@ -253,6 +253,7 @@ are shown below with sample overrides following. In `config/initializers/acts_as ```ruby ActsAsTenant.configure do |config| config.require_tenant = false # true + config.global_records_identifier = nil # Customize the query for loading the tenant in background jobs config.job_scope = ->{ all }