acts_as_multi_tenant 2.0.1 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 14e72db4db911ca0e43798db62f98a5e67382854dacea598ee6e6a16803b156f
4
- data.tar.gz: fd548e99029862dd91832605f1d315d25778f80836fe883886b7dc217e59a292
3
+ metadata.gz: 6c71beedb1daf50d84359687c3cd836db5a164b9155dec367cc7fa2603da30b3
4
+ data.tar.gz: d1e4917dd3aa6c00adacf881aba8f8416ced51e062ad3943b26d07e53e89dcd9
5
5
  SHA512:
6
- metadata.gz: 0ce9937e616e3ad74bcf85f75453edb755c478ce6d2864b6a3b0dd0f2d059ff301c6c99929c90b26a4ca62ba1ec7e2d89510952bf5e8abc3b98ab1166ca9581c
7
- data.tar.gz: 54c16fd1e4b5d1df10876ac451ae5e5dd50a99cd617f29806539a7d108c41d39220fc18d0647c4c9991a972d94173232206737b48061c7ce57147a2ea2faf443
6
+ metadata.gz: 32e329436b4f368e8488552a82c50d16669212f43eebbf8728089e9b8104e72894251003c918dc1a66007761266366f29376c378d85a7cb0cd9076d18c8ce308
7
+ data.tar.gz: dc3529111832862ad25020c62a3be7574e4b372b554ba37c3f31609d88cc73db8b5c0426cf2a0a0b85e2bf54ddb3c661a44a1761037da5c4f6f430923999aaeb
@@ -8,4 +8,29 @@ require_relative 'multi_tenant/middleware'
8
8
 
9
9
  module MultiTenant
10
10
  NotImplemented = Class.new(StandardError)
11
+
12
+ class TenantsNotFound < RuntimeError
13
+ attr_reader :tenant_class
14
+
15
+ def initialize(tenant_class, identifiers, found_records)
16
+ @tenant_class = tenant_class
17
+ @identifiers = identifiers
18
+ @found_records = found_records
19
+ end
20
+
21
+ # Returns an array of the tenant identifiers that could not be found
22
+ def not_found
23
+ @not_found ||= @identifiers.map(&:to_s) - @found_records.map { |tenant|
24
+ tenant.send(@tenant_class.tenant_identifier).to_s
25
+ }
26
+ end
27
+
28
+ def to_s
29
+ message
30
+ end
31
+
32
+ def message
33
+ "The following #{@tenant_class.name} tenants could not be found: #{not_found.join ", "}"
34
+ end
35
+ end
11
36
  end
@@ -12,9 +12,10 @@ module MultiTenant
12
12
  # @param using [String] (optional) column that contains the unique lookup identifier. Defaults to :code.
13
13
  #
14
14
  def acts_as_tenant(using: :code)
15
- cattr_accessor :tenant_identifier, :tenant_thread_var
15
+ cattr_accessor :tenant_identifier, :tenant_thread_var, :raise_on_tenant_not_found
16
16
  self.tenant_identifier = using
17
17
  self.tenant_thread_var = "current_tenant_#{object_id}".freeze # allows there to be multiple tenant classes
18
+ self.raise_on_tenant_not_found = true
18
19
  self.extend MultiTenant::ActsAsTenant::TenantGetters
19
20
  self.extend MultiTenant::ActsAsTenant::TenantSetters
20
21
  self.extend MultiTenant::ActsAsTenant::TenantHelpers
@@ -102,11 +103,15 @@ module MultiTenant
102
103
  # @param records_or_identifiers array of the records or identifiers in the 'tenant_identifier' column.
103
104
  #
104
105
  def current_tenants=(records_or_identifiers)
105
- records, identifiers = Array(records_or_identifiers).partition { |x|
106
+ records, identifiers = Array(records_or_identifiers.uniq).partition { |x|
106
107
  x.class.respond_to?(:table_name) && x.class.table_name == self.table_name
107
108
  }
108
109
  tenants = if identifiers.any?
109
- records + where({tenant_identifier => identifiers}).to_a
110
+ queried_records = where({tenant_identifier => identifiers}).to_a
111
+ if queried_records.size != identifiers.size and raise_on_tenant_not_found
112
+ raise ::MultiTenant::TenantsNotFound.new(self, identifiers, queried_records)
113
+ end
114
+ records + queried_records
110
115
  else
111
116
  records
112
117
  end
@@ -96,6 +96,10 @@ module MultiTenant
96
96
  ids = identifiers records_or_identifiers
97
97
  return not_found.(id_resp.is_a?(Array) ? ids : ids[0])
98
98
  end
99
+
100
+ rescue ::MultiTenant::TenantsNotFound => e
101
+ ids = e.not_found
102
+ not_found.(id_resp.is_a?(Array) ? ids : ids[0])
99
103
  ensure
100
104
  tenant_class.current = nil
101
105
  end
@@ -1,4 +1,7 @@
1
1
  module MultiTenant
2
+ # An exception indicating a tenant with a missing or invalid proxy identifier
3
+ NilProxyError = Class.new(RuntimeError)
4
+
2
5
  #
3
6
  # Helpers for setting a proxy model to your tenant model. So your records can `acts_as_tenant` to the proxy model instead of directly to the tenant.
4
7
  #
@@ -84,7 +87,14 @@ module MultiTenant
84
87
  def current_tenants
85
88
  proxied_tenant_class
86
89
  .current_tenants
87
- .map(&proxied_tenant_inverse_assoc)
90
+ .map { |tenant|
91
+ if (proxy = tenant.send(proxied_tenant_inverse_assoc))
92
+ proxy
93
+ else
94
+ tenant_id = tenant.send(proxied_tenant_class.primary_key)
95
+ raise ::MultiTenant::NilProxyError, "Missing proxy for tenant #{proxied_tenant_class.name}##{tenant_id}"
96
+ end
97
+ }
88
98
  end
89
99
  end
90
100
 
@@ -1,4 +1,4 @@
1
1
  module MultiTenant
2
2
  # Gem version
3
- VERSION = '2.0.1'.freeze
3
+ VERSION = '2.1.0'.freeze
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_multi_tenant
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jordan Hollinger
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2019-01-23 00:00:00.000000000 Z
13
+ date: 2019-05-28 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord
@@ -81,7 +81,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
81
81
  version: '0'
82
82
  requirements: []
83
83
  rubyforge_project:
84
- rubygems_version: 2.7.6
84
+ rubygems_version: 2.7.6.2
85
85
  signing_key:
86
86
  specification_version: 4
87
87
  summary: An ActiveRecord plugin for multi-tenant databases