mongoid-multitenancy 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -0
- data/lib/mongoid/multitenancy/document.rb +9 -6
- data/lib/mongoid/multitenancy/version.rb +1 -1
- data/lib/mongoid/multitenancy.rb +3 -6
- data/spec/optional_spec.rb +103 -18
- data/spec/spec_helper.rb +1 -1
- data/spec/support/database_cleaner.rb +0 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e24dad42cdd8d6692c7c37ae3d370c11615423ed
|
4
|
+
data.tar.gz: 0a6b110bab9bd521fe3be96a76cc7f10e9ef4957
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5ffaa31b8280e405b4b0e7acd7b5ad84e8c15ae3815be55a962526ef6cf9d918ea76d144041856d0f281153e31dbea025bdf5cee6ab0d1546436751022fa777c
|
7
|
+
data.tar.gz: 68fa75bceb0e1103a0307edafb1e0b2d4110bb1f76473b13a823d560dd35e5ca00a8bbc2c94413fcb358d77eb24e608cc9bdf8efe36139b8347051188cca5ecf
|
data/CHANGELOG.md
CHANGED
@@ -34,13 +34,13 @@ module Mongoid
|
|
34
34
|
belongs_to association, assoc_options
|
35
35
|
|
36
36
|
# Get the tenant model and its foreign key
|
37
|
-
self.tenant_field = reflect_on_association(association).foreign_key
|
37
|
+
self.tenant_field = reflect_on_association(association).foreign_key.to_sym
|
38
38
|
self.tenant_options = multitenant_options
|
39
39
|
|
40
40
|
# Validates the tenant field
|
41
41
|
validates_tenancy_of tenant_field, multitenant_options
|
42
42
|
|
43
|
-
|
43
|
+
define_scopes
|
44
44
|
define_initializer association
|
45
45
|
define_inherited association, options
|
46
46
|
define_index if multitenant_options[:index]
|
@@ -120,7 +120,7 @@ module Mongoid
|
|
120
120
|
def define_initializer(association)
|
121
121
|
# Apply the default value when the default scope is complex (optional tenant)
|
122
122
|
after_initialize lambda {
|
123
|
-
if Multitenancy.current_tenant && send(association.to_sym).nil?
|
123
|
+
if Multitenancy.current_tenant && send(association.to_sym).nil? && new_record?
|
124
124
|
send "#{association}=".to_sym, Multitenancy.current_tenant
|
125
125
|
end
|
126
126
|
}
|
@@ -138,14 +138,14 @@ module Mongoid
|
|
138
138
|
|
139
139
|
# @private
|
140
140
|
#
|
141
|
-
#
|
142
|
-
def
|
141
|
+
# Define the scopes
|
142
|
+
def define_scopes
|
143
143
|
# Set the default_scope to scope to current tenant
|
144
144
|
default_scope lambda {
|
145
145
|
if Multitenancy.current_tenant
|
146
146
|
tenant_id = Multitenancy.current_tenant.id
|
147
147
|
if tenant_options[:optional]
|
148
|
-
where(tenant_field.
|
148
|
+
where(tenant_field.in => [tenant_id, nil])
|
149
149
|
else
|
150
150
|
where(tenant_field => tenant_id)
|
151
151
|
end
|
@@ -153,6 +153,9 @@ module Mongoid
|
|
153
153
|
where(nil)
|
154
154
|
end
|
155
155
|
}
|
156
|
+
|
157
|
+
scope :shared, -> { where(tenant_field => nil) }
|
158
|
+
scope :unshared, -> { where(tenant_field => Multitenancy.current_tenant.id) }
|
156
159
|
end
|
157
160
|
|
158
161
|
# @private
|
data/lib/mongoid/multitenancy.rb
CHANGED
@@ -20,16 +20,13 @@ module Mongoid
|
|
20
20
|
|
21
21
|
# Affects a tenant temporary for a block execution
|
22
22
|
def with_tenant(tenant, &block)
|
23
|
-
if block.nil?
|
24
|
-
raise ArgumentError, 'block required'
|
25
|
-
end
|
23
|
+
raise ArgumentError, 'block required' if block.nil?
|
26
24
|
|
27
25
|
old_tenant = self.current_tenant
|
28
26
|
self.current_tenant = tenant
|
29
|
-
|
30
|
-
block.call
|
31
|
-
|
27
|
+
result = block.call
|
32
28
|
self.current_tenant = old_tenant
|
29
|
+
result
|
33
30
|
end
|
34
31
|
end
|
35
32
|
end
|
data/spec/optional_spec.rb
CHANGED
@@ -17,45 +17,130 @@ describe Optional do
|
|
17
17
|
it_behaves_like 'a tenantable model'
|
18
18
|
it { is_expected.to validate_tenant_uniqueness_of(:slug) }
|
19
19
|
|
20
|
+
describe '#initialize' do
|
21
|
+
context 'within a client context' do
|
22
|
+
before do
|
23
|
+
Mongoid::Multitenancy.current_tenant = client
|
24
|
+
end
|
25
|
+
|
26
|
+
context 'when persisted' do
|
27
|
+
before do
|
28
|
+
item.client = nil
|
29
|
+
item.save!
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'does not override the client' do
|
33
|
+
item.reload
|
34
|
+
expect(Optional.last.client).to be_nil
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
20
40
|
describe '.default_scope' do
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
41
|
+
let!(:item_a) do
|
42
|
+
Mongoid::Multitenancy.with_tenant(client) do
|
43
|
+
Optional.create!(title: 'title A', slug: 'article-a')
|
44
|
+
end
|
25
45
|
end
|
26
46
|
|
27
|
-
|
28
|
-
|
29
|
-
|
47
|
+
let!(:item_b) do
|
48
|
+
Mongoid::Multitenancy.with_tenant(another_client) do
|
49
|
+
Optional.create!(title: 'title B', slug: 'article-b')
|
30
50
|
end
|
51
|
+
end
|
52
|
+
|
53
|
+
let!(:shared_item) do
|
54
|
+
Optional.create!(title: 'title C', slug: 'article-c')
|
55
|
+
end
|
31
56
|
|
57
|
+
context 'with a current tenant' do
|
32
58
|
it 'filters on the current tenant / free-tenant items' do
|
33
|
-
|
59
|
+
Mongoid::Multitenancy.with_tenant(another_client) do
|
60
|
+
expect(Optional.all.to_a).to match_array [shared_item, item_b]
|
61
|
+
end
|
34
62
|
end
|
35
63
|
end
|
36
64
|
|
37
65
|
context 'without a current tenant' do
|
38
|
-
|
39
|
-
|
66
|
+
it 'does not filter on any tenant' do
|
67
|
+
expect(Optional.all.to_a).to match_array [item_a, item_b, shared_item]
|
40
68
|
end
|
69
|
+
end
|
70
|
+
end
|
41
71
|
|
42
|
-
|
43
|
-
|
72
|
+
describe '.shared' do
|
73
|
+
let!(:item_a) do
|
74
|
+
Mongoid::Multitenancy.with_tenant(client) do
|
75
|
+
Optional.create!(title: 'title A', slug: 'article-a')
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
let!(:item_b) do
|
80
|
+
Mongoid::Multitenancy.with_tenant(another_client) do
|
81
|
+
Optional.create!(title: 'title B', slug: 'article-b')
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
let!(:shared_item) do
|
86
|
+
Optional.create!(title: 'title C', slug: 'article-c')
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'returns only the shared items' do
|
90
|
+
Mongoid::Multitenancy.with_tenant(another_client) do
|
91
|
+
expect(Optional.shared.to_a).to match_array [shared_item]
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
describe '.unshared' do
|
97
|
+
let!(:item_a) do
|
98
|
+
Mongoid::Multitenancy.with_tenant(client) do
|
99
|
+
Optional.create!(title: 'title A', slug: 'article-a')
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
let!(:item_b) do
|
104
|
+
Mongoid::Multitenancy.with_tenant(another_client) do
|
105
|
+
Optional.create!(title: 'title B', slug: 'article-b')
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
let!(:shared_item) do
|
110
|
+
Optional.create!(title: 'title C', slug: 'article-c')
|
111
|
+
end
|
112
|
+
|
113
|
+
it 'returns only the shared items' do
|
114
|
+
Mongoid::Multitenancy.with_tenant(another_client) do
|
115
|
+
expect(Optional.unshared.to_a).to match_array [item_b]
|
44
116
|
end
|
45
117
|
end
|
46
118
|
end
|
47
119
|
|
48
120
|
describe '#delete_all' do
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
121
|
+
let!(:item_a) do
|
122
|
+
Mongoid::Multitenancy.with_tenant(client) do
|
123
|
+
Optional.create!(title: 'title A', slug: 'article-a')
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
let!(:item_b) do
|
128
|
+
Mongoid::Multitenancy.with_tenant(another_client) do
|
129
|
+
Optional.create!(title: 'title B', slug: 'article-b')
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
let!(:shared_item) do
|
134
|
+
Optional.create!(title: 'title C', slug: 'article-c')
|
53
135
|
end
|
54
136
|
|
55
137
|
context 'with a current tenant' do
|
56
138
|
it 'only deletes the current tenant / free-tenant items' do
|
57
|
-
Mongoid::Multitenancy.with_tenant(another_client)
|
58
|
-
|
139
|
+
Mongoid::Multitenancy.with_tenant(another_client) do
|
140
|
+
Optional.delete_all
|
141
|
+
end
|
142
|
+
|
143
|
+
expect(Optional.all.to_a).to match_array [item_a]
|
59
144
|
end
|
60
145
|
end
|
61
146
|
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid-multitenancy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aymeric Brisse
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-10-
|
11
|
+
date: 2015-10-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mongoid
|