acts_as_tenant 0.5.2 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 82765cfa1d00a63aa012f3ab5de9ddd60ae2c8041df405e08ae70ef20382d7ee
4
- data.tar.gz: ba1920a299f84a4e6424304539c05ca69515e3b17e73ffb1b6635fbce79ae77c
3
+ metadata.gz: f5bed52bb9172b8035bbe0f9ac6d837acf6bfdf9f4bcec542556dddf3b3b3f5d
4
+ data.tar.gz: fe3c6219243da29201e4965c5906398e06df9e80a7e9de948bbe53b702804d8f
5
5
  SHA512:
6
- metadata.gz: 62854b28ba5293d5437f5614032e992d914bc0741be3e8b925231116802d85b19d9631fc2e0ff2a7775e3259a998e4239f0b38c7ddf97c6817a0db96fbf488c5
7
- data.tar.gz: 2e6c875895dd15882ffc4454f0c0bfae4fc3e77df17deae58710835e92bf32761247ab3b60cdfe9a83b81f72e2ea698c59f0155c046dfe2af5c62450a4bd65ca
6
+ metadata.gz: b5a499156656e313c113d2dddc9c922d49447bcecc2bb76a421c01fc8401a1a0501cb4515ac6323b48b71b046197da52dc3850634ba12435c44a6b95bb6724fc
7
+ data.tar.gz: c046fa7c06351158b437b83b61030abce8b7b95d0a453c9ee44b93a4518b50a6acfef59a00840a8624b6515a09979c12c2cc31783a959a412c21dad0f35d9ca3
data/README.md CHANGED
@@ -137,6 +137,16 @@ end
137
137
  ```
138
138
  This is useful in shared routes such as admin panels or internal dashboards when `require_tenant` option is enabled throughout the app.
139
139
 
140
+ ### Allowing tenant updating for a block ###
141
+
142
+ ```ruby
143
+ ActsAsTenant.with_mutable_tenant do
144
+ # Tenant updating is enabled for all code in this block
145
+ end
146
+ ```
147
+
148
+ This will allow you to change the tenant of a model. This feature is useful for admin screens, where it is ok to allow certain users to change the tenant on existing models in specific cases.
149
+
140
150
  ### Require tenant to be set always ###
141
151
 
142
152
  If you want to require the tenant to be set at all times, you can configure acts_as_tenant to raise an error when a query is made without a tenant available. See below under configuration options.
@@ -5,11 +5,12 @@ module ActsAsTenant
5
5
  class_methods do
6
6
  def acts_as_tenant(tenant = :account, **options)
7
7
  ActsAsTenant.set_tenant_klass(tenant)
8
+ ActsAsTenant.mutable_tenant!(false)
8
9
 
9
10
  ActsAsTenant.add_global_record_model(self) if options[:has_global_records]
10
11
 
11
12
  # Create the association
12
- valid_options = options.slice(:foreign_key, :class_name, :inverse_of, :optional, :primary_key, :counter_cache)
13
+ valid_options = options.slice(:foreign_key, :class_name, :inverse_of, :optional, :primary_key, :counter_cache, :polymorphic)
13
14
  fkey = valid_options[:foreign_key] || ActsAsTenant.fkey
14
15
  pkey = valid_options[:primary_key] || ActsAsTenant.pkey
15
16
  polymorphic_type = valid_options[:foreign_type] || ActsAsTenant.polymorphic_type
@@ -77,13 +78,13 @@ module ActsAsTenant
77
78
  to_include = Module.new {
78
79
  define_method "#{fkey}=" do |integer|
79
80
  write_attribute(fkey.to_s, integer)
80
- raise ActsAsTenant::Errors::TenantIsImmutable if tenant_modified?
81
+ raise ActsAsTenant::Errors::TenantIsImmutable if !ActsAsTenant.mutable_tenant? && tenant_modified?
81
82
  integer
82
83
  end
83
84
 
84
85
  define_method "#{ActsAsTenant.tenant_klass}=" do |model|
85
86
  super(model)
86
- raise ActsAsTenant::Errors::TenantIsImmutable if tenant_modified?
87
+ raise ActsAsTenant::Errors::TenantIsImmutable if !ActsAsTenant.mutable_tenant? && tenant_modified?
87
88
  model
88
89
  end
89
90
 
@@ -105,9 +106,9 @@ module ActsAsTenant
105
106
 
106
107
  fkey = reflect_on_association(ActsAsTenant.tenant_klass).foreign_key
107
108
 
108
- validation_args = args.clone
109
+ validation_args = args.deep_dup
109
110
  validation_args[:scope] = if args[:scope]
110
- Array(args[:scope]) << fkey
111
+ Array(args[:scope]) + [fkey]
111
112
  else
112
113
  fkey
113
114
  end
@@ -1,6 +1,14 @@
1
1
  module ActsAsTenant::Sidekiq
2
+ class BaseMiddleware
3
+ def self.sidekiq_7_and_up?
4
+ Gem::Version.new(Sidekiq::VERSION) >= Gem::Version.new("7")
5
+ end
6
+ end
7
+
2
8
  # Get the current tenant and store in the message to be sent to Sidekiq.
3
- class Client
9
+ class Client < BaseMiddleware
10
+ include Sidekiq::ClientMiddleware if sidekiq_7_and_up?
11
+
4
12
  def call(worker_class, msg, queue, redis_pool)
5
13
  if ActsAsTenant.current_tenant.present?
6
14
  msg["acts_as_tenant"] ||=
@@ -15,7 +23,9 @@ module ActsAsTenant::Sidekiq
15
23
  end
16
24
 
17
25
  # Pull the tenant out and run the current thread with it.
18
- class Server
26
+ class Server < BaseMiddleware
27
+ include Sidekiq::ServerMiddleware if sidekiq_7_and_up?
28
+
19
29
  def call(worker_class, msg, queue)
20
30
  if msg.has_key?("acts_as_tenant")
21
31
  account = msg["acts_as_tenant"]["class"].constantize.find msg["acts_as_tenant"]["id"]
@@ -1,3 +1,3 @@
1
1
  module ActsAsTenant
2
- VERSION = "0.5.2"
2
+ VERSION = "0.6.0"
3
3
  end
@@ -12,6 +12,7 @@ module ActsAsTenant
12
12
  @@configuration = nil
13
13
  @@tenant_klass = nil
14
14
  @@models_with_global_records = []
15
+ @@mutable_tenant = false
15
16
 
16
17
  class << self
17
18
  attr_writer :default_tenant
@@ -87,6 +88,14 @@ module ActsAsTenant
87
88
  @default_tenant unless unscoped
88
89
  end
89
90
 
91
+ def self.mutable_tenant!(toggle)
92
+ @@mutable_tenant = toggle
93
+ end
94
+
95
+ def self.mutable_tenant?
96
+ @@mutable_tenant
97
+ end
98
+
90
99
  def self.with_tenant(tenant, &block)
91
100
  if block.nil?
92
101
  raise ArgumentError, "block required"
@@ -120,6 +129,13 @@ module ActsAsTenant
120
129
  self.unscoped = old_unscoped
121
130
  end
122
131
 
132
+ def self.with_mutable_tenant(&block)
133
+ ActsAsTenant.mutable_tenant!(true)
134
+ without_tenant(&block)
135
+ ensure
136
+ ActsAsTenant.mutable_tenant!(false)
137
+ end
138
+
123
139
  def self.should_require_tenant?
124
140
  if configuration.require_tenant.respond_to?(:call)
125
141
  !!configuration.require_tenant.call
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_tenant
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Erwin Matthijssen
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2022-09-06 00:00:00.000000000 Z
12
+ date: 2022-12-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: request_store
@@ -171,7 +171,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
171
171
  - !ruby/object:Gem::Version
172
172
  version: '0'
173
173
  requirements: []
174
- rubygems_version: 3.3.7
174
+ rubygems_version: 3.3.26
175
175
  signing_key:
176
176
  specification_version: 4
177
177
  summary: Add multi-tenancy to Rails applications using a shared db strategy