Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add global_records_identifier configuration #332

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
4 changes: 4 additions & 0 deletions lib/acts_as_tenant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion lib/acts_as_tenant/configuration.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/acts_as_tenant/model_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down