mongoid-multitenancy 1.2.0 → 2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +2 -6
- data/CHANGELOG.md +14 -1
- data/Gemfile +2 -2
- data/README.md +24 -19
- data/lib/mongoid/multitenancy/document.rb +1 -0
- data/lib/mongoid/multitenancy/validators/tenant_uniqueness.rb +2 -8
- data/lib/mongoid/multitenancy/version.rb +1 -1
- data/mongoid-multitenancy.gemspec +1 -1
- data/spec/immutable_spec.rb +1 -1
- data/spec/indexable_spec.rb +4 -4
- data/spec/models/immutable.rb +1 -1
- data/spec/models/indexable.rb +1 -1
- data/spec/models/mandatory.rb +1 -1
- data/spec/models/mutable.rb +1 -1
- data/spec/models/no_scopable.rb +1 -1
- data/spec/models/optional.rb +1 -1
- data/spec/models/optional_exclude.rb +1 -1
- data/spec/mutable_child_spec.rb +1 -1
- data/spec/mutable_spec.rb +1 -1
- data/spec/optional_spec.rb +2 -2
- data/spec/spec_helper.rb +2 -1
- data/spec/support/shared_examples.rb +8 -8
- metadata +7 -10
- data/gemfiles/Gemfile.mongoid-4.0 +0 -17
- data/gemfiles/Gemfile.mongoid-5.0 +0 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 431219928a15faa3df3004d3b81b840bc8650691
|
4
|
+
data.tar.gz: 41c7101a950c1ae7539912038befff3c9e4cac63
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6c7e4c5902e9da5050c1bf14797a93d06a1303fa61fed4ad9cb6b32ea57309f6abbe8ecef530f37457e9474fc18bd2eeb99812229c67abbaf9e8a7b3cceec1fe
|
7
|
+
data.tar.gz: 14df29dad2f6946275bbc152823003a04c701b44f3bac4774fdbbf11faabe7d4d5aad0b1f49b7f544d298c7ef6d0ed056874fe8a26c047b09de7986ddc1c13b6
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,16 @@
|
|
1
|
+
## 2.0
|
2
|
+
|
3
|
+
### New Features
|
4
|
+
|
5
|
+
* Add support for mongoid 6
|
6
|
+
* Remove support for mongoid 4 & 5
|
7
|
+
|
8
|
+
## 1.2
|
9
|
+
|
10
|
+
### New Features
|
11
|
+
|
12
|
+
* Add *exclude_shared* option for the TenantUniquenessValidator
|
13
|
+
|
1
14
|
## 1.1
|
2
15
|
|
3
16
|
### New Features
|
@@ -12,7 +25,7 @@
|
|
12
25
|
|
13
26
|
### New Features
|
14
27
|
|
15
|
-
*
|
28
|
+
* Add support for mongoid 5
|
16
29
|
|
17
30
|
### Major Changes (Backwards Incompatible)
|
18
31
|
|
data/Gemfile
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
source 'https://rubygems.org'
|
2
2
|
|
3
|
-
gem 'mongoid', '~>
|
3
|
+
gem 'mongoid', '~> 6.0'
|
4
4
|
|
5
5
|
gem 'rake', '~> 11.0'
|
6
6
|
|
@@ -9,7 +9,7 @@ group :test do
|
|
9
9
|
gem 'coveralls', require: false
|
10
10
|
gem 'rspec', '~> 3.1'
|
11
11
|
gem 'yard', '~> 0.8'
|
12
|
-
gem 'mongoid-rspec', '
|
12
|
+
gem 'mongoid-rspec', git: 'https://github.com/mongoid-rspec/mongoid-rspec.git'
|
13
13
|
gem 'rubocop', require: false
|
14
14
|
end
|
15
15
|
|
data/README.md
CHANGED
@@ -12,12 +12,17 @@ In addition, mongoid-multitenancy:
|
|
12
12
|
* is thread safe
|
13
13
|
* redefines some mongoid functions like `index`, `validates_with` and `delete_all` to take in account the multitenancy.
|
14
14
|
|
15
|
+
Compatibility
|
16
|
+
===============
|
17
|
+
|
18
|
+
mongoid-multitenancy 2.0 is only compatible with mongoid 6. For mongoid 4 & 5 compatiblity, use mongoid-multitenancy 1.2.
|
19
|
+
|
15
20
|
Installation
|
16
21
|
===============
|
17
22
|
|
18
23
|
Add this line to your application's Gemfile:
|
19
24
|
|
20
|
-
gem 'mongoid-multitenancy'
|
25
|
+
gem 'mongoid-multitenancy', '~> 2.0'
|
21
26
|
|
22
27
|
And then execute:
|
23
28
|
|
@@ -74,7 +79,7 @@ class Article
|
|
74
79
|
include Mongoid::Document
|
75
80
|
include Mongoid::Multitenancy::Document
|
76
81
|
|
77
|
-
tenant(:
|
82
|
+
tenant(:tenant)
|
78
83
|
|
79
84
|
field :title, :type => String
|
80
85
|
end
|
@@ -101,15 +106,15 @@ Some examples to illustrate this behavior:
|
|
101
106
|
Mongoid::Multitenancy.current_tenant = Client.find_by(:name => 'Perfect Memory') # => <#Client _id:50ca04b86c82bfc125000025, :name: "Perfect Memory">
|
102
107
|
|
103
108
|
# All searches are scoped by the tenant, the following searches will only return objects belonging to the current client.
|
104
|
-
Article.all # => all articles where
|
109
|
+
Article.all # => all articles where tenant_id => 50ca04b86c82bfc125000025
|
105
110
|
|
106
111
|
# New objects are scoped to the current tenant
|
107
112
|
article = Article.new(:title => 'New blog')
|
108
|
-
article.save # => <#Article _id: 50ca04b86c82bfc125000044, title: 'New blog',
|
113
|
+
article.save # => <#Article _id: 50ca04b86c82bfc125000044, title: 'New blog', tenant_id: 50ca04b86c82bfc125000025>
|
109
114
|
|
110
115
|
# It can make the tenant field immutable once it is persisted to avoid inconsistency
|
111
116
|
article.persisted? # => true
|
112
|
-
article.
|
117
|
+
article.tenant = another_client
|
113
118
|
article.valid? # => false
|
114
119
|
```
|
115
120
|
|
@@ -124,21 +129,21 @@ class Article
|
|
124
129
|
include Mongoid::Document
|
125
130
|
include Mongoid::Multitenancy::Document
|
126
131
|
|
127
|
-
tenant(:
|
132
|
+
tenant(:tenant, optional: true)
|
128
133
|
|
129
134
|
field :title, :type => String
|
130
135
|
end
|
131
136
|
|
132
137
|
Mongoid::Multitenancy.with_tenant(client_instance) do
|
133
|
-
Article.all # => all articles where
|
138
|
+
Article.all # => all articles where tenant_id.in [50ca04b86c82bfc125000025, nil]
|
134
139
|
article = Article.new(:title => 'New article')
|
135
|
-
article.save # => <#Article _id: 50ca04b86c82bfc125000044, title: 'New blog',
|
140
|
+
article.save # => <#Article _id: 50ca04b86c82bfc125000044, title: 'New blog', tenant_id: 50ca04b86c82bfc125000025>
|
136
141
|
|
137
142
|
# tenant needs to be set manually to nil
|
138
|
-
article = Article.new(:title => 'New article', :
|
139
|
-
article.save # => <#Article _id: 50ca04b86c82bfc125000044, title: 'New blog',
|
143
|
+
article = Article.new(:title => 'New article', :tenant => nil)
|
144
|
+
article.save # => <#Article _id: 50ca04b86c82bfc125000044, title: 'New blog', tenant_id: 50ca04b86c82bfc125000025>
|
140
145
|
article.tenant = nil
|
141
|
-
article.save => <#Article _id: 50ca04b86c82bfc125000044, title: 'New blog',
|
146
|
+
article.save => <#Article _id: 50ca04b86c82bfc125000044, title: 'New blog', tenant_id: nil>
|
142
147
|
end
|
143
148
|
```
|
144
149
|
|
@@ -177,7 +182,7 @@ class Article
|
|
177
182
|
include Mongoid::Document
|
178
183
|
include Mongoid::Multitenancy::Document
|
179
184
|
|
180
|
-
tenant :
|
185
|
+
tenant :tenant
|
181
186
|
|
182
187
|
field :slug
|
183
188
|
|
@@ -197,7 +202,7 @@ class Article
|
|
197
202
|
include Mongoid::Document
|
198
203
|
include Mongoid::Multitenancy::Document
|
199
204
|
|
200
|
-
tenant :
|
205
|
+
tenant :tenant, optional: true
|
201
206
|
|
202
207
|
field :slug
|
203
208
|
|
@@ -206,14 +211,14 @@ end
|
|
206
211
|
```
|
207
212
|
|
208
213
|
In the following case, 2 private articles can have the same slug if they belongs to 2 different clients even if a shared
|
209
|
-
article already uses that same slug, like a `validates_uniqueness_of scope: :
|
214
|
+
article already uses that same slug, like a `validates_uniqueness_of scope: :tenant` does.
|
210
215
|
|
211
216
|
```ruby
|
212
217
|
class Article
|
213
218
|
include Mongoid::Document
|
214
219
|
include Mongoid::Multitenancy::Document
|
215
220
|
|
216
|
-
tenant :
|
221
|
+
tenant :tenant, optional: true
|
217
222
|
|
218
223
|
field :slug
|
219
224
|
|
@@ -230,14 +235,14 @@ To create a single index on the tenant field, you can use the option `index: tru
|
|
230
235
|
|
231
236
|
On the example below, only one indexe will be created:
|
232
237
|
|
233
|
-
* { 'title_id' => 1, '
|
238
|
+
* { 'title_id' => 1, 'tenant_id' => 1 }
|
234
239
|
|
235
240
|
```ruby
|
236
241
|
class Article
|
237
242
|
include Mongoid::Document
|
238
243
|
include Mongoid::Multitenancy::Document
|
239
244
|
|
240
|
-
tenant :
|
245
|
+
tenant :tenant, full_indexes: true
|
241
246
|
|
242
247
|
field :title
|
243
248
|
|
@@ -247,7 +252,7 @@ end
|
|
247
252
|
|
248
253
|
On the example below, 2 indexes will be created:
|
249
254
|
|
250
|
-
* { '
|
255
|
+
* { 'tenant_id' => 1 }
|
251
256
|
* { 'title_id' => 1 }
|
252
257
|
|
253
258
|
```ruby
|
@@ -255,7 +260,7 @@ class Article
|
|
255
260
|
include Mongoid::Document
|
256
261
|
include Mongoid::Multitenancy::Document
|
257
262
|
|
258
|
-
tenant :
|
263
|
+
tenant :tenant, index: true
|
259
264
|
|
260
265
|
field :title
|
261
266
|
|
@@ -39,14 +39,8 @@ module Mongoid
|
|
39
39
|
criteria = with_tenant_criterion(criteria, klass, document)
|
40
40
|
criteria = criteria.merge(options[:conditions].call) if options[:conditions]
|
41
41
|
|
42
|
-
if
|
43
|
-
|
44
|
-
add_error(document, attribute, value)
|
45
|
-
end
|
46
|
-
else
|
47
|
-
if criteria.with(criteria.persistence_options).read(mode: :primary).exists?
|
48
|
-
add_error(document, attribute, value)
|
49
|
-
end
|
42
|
+
if criteria.read(mode: :primary).exists?
|
43
|
+
add_error(document, attribute, value)
|
50
44
|
end
|
51
45
|
end
|
52
46
|
|
data/spec/immutable_spec.rb
CHANGED
data/spec/indexable_spec.rb
CHANGED
@@ -11,25 +11,25 @@ describe 'tenant' do
|
|
11
11
|
|
12
12
|
context 'without index: true' do
|
13
13
|
it 'does not create an index' do
|
14
|
-
expect(Immutable).not_to have_index_for(
|
14
|
+
expect(Immutable).not_to have_index_for(tenant_id: 1)
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
18
|
context 'with index: true' do
|
19
19
|
it 'creates an index' do
|
20
|
-
expect(Indexable).to have_index_for(
|
20
|
+
expect(Indexable).to have_index_for(tenant_id: 1)
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
24
|
context 'with full_indexes: true' do
|
25
25
|
it 'add the tenant field on each index' do
|
26
|
-
expect(Immutable).to have_index_for(
|
26
|
+
expect(Immutable).to have_index_for(tenant_id: 1, title: 1)
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
30
|
context 'with full_indexes: false' do
|
31
31
|
it 'does not add the tenant field on each index' do
|
32
|
-
expect(Indexable).not_to have_index_for(
|
32
|
+
expect(Indexable).not_to have_index_for(tenant_id: 1, title: 1)
|
33
33
|
end
|
34
34
|
end
|
35
35
|
end
|
data/spec/models/immutable.rb
CHANGED
data/spec/models/indexable.rb
CHANGED
data/spec/models/mandatory.rb
CHANGED
data/spec/models/mutable.rb
CHANGED
@@ -2,7 +2,7 @@ class Mutable
|
|
2
2
|
include Mongoid::Document
|
3
3
|
include Mongoid::Multitenancy::Document
|
4
4
|
|
5
|
-
tenant :
|
5
|
+
tenant :tenant, class_name: 'Account', immutable: false, optional: false
|
6
6
|
|
7
7
|
field :slug, type: String
|
8
8
|
field :title, type: String
|
data/spec/models/no_scopable.rb
CHANGED
data/spec/models/optional.rb
CHANGED
data/spec/mutable_child_spec.rb
CHANGED
data/spec/mutable_spec.rb
CHANGED
data/spec/optional_spec.rb
CHANGED
@@ -24,13 +24,13 @@ describe Optional do
|
|
24
24
|
|
25
25
|
context 'when persisted' do
|
26
26
|
before do
|
27
|
-
item.
|
27
|
+
item.tenant = nil
|
28
28
|
item.save!
|
29
29
|
end
|
30
30
|
|
31
31
|
it 'does not override the client' do
|
32
32
|
item.reload
|
33
|
-
expect(Optional.last.
|
33
|
+
expect(Optional.last.tenant).to be_nil
|
34
34
|
end
|
35
35
|
end
|
36
36
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -30,7 +30,8 @@ Mongoid.configure do |config|
|
|
30
30
|
config.connect_to 'mongoid_multitenancy'
|
31
31
|
end
|
32
32
|
|
33
|
-
Mongoid.logger =
|
33
|
+
Mongoid.logger.level = Logger::INFO
|
34
|
+
Mongo::Logger.logger.level = Logger::INFO
|
34
35
|
|
35
36
|
RSpec.configure do |config|
|
36
37
|
config.include Mongoid::Matchers
|
@@ -1,6 +1,6 @@
|
|
1
1
|
shared_examples_for 'a tenantable model' do
|
2
|
-
it { is_expected.to belong_to(:
|
3
|
-
it { is_expected.to have_index_for(
|
2
|
+
it { is_expected.to belong_to(:tenant) }
|
3
|
+
it { is_expected.to have_index_for(tenant_id: 1, title: 1) }
|
4
4
|
|
5
5
|
describe '#initialize' do
|
6
6
|
context 'within a client context' do
|
@@ -9,7 +9,7 @@ shared_examples_for 'a tenantable model' do
|
|
9
9
|
end
|
10
10
|
|
11
11
|
it 'set the client' do
|
12
|
-
expect(item.
|
12
|
+
expect(item.tenant).to eq client
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
@@ -19,7 +19,7 @@ shared_examples_for 'a tenantable model' do
|
|
19
19
|
end
|
20
20
|
|
21
21
|
it 'does not set any client' do
|
22
|
-
expect(item.
|
22
|
+
expect(item.tenant).to be_nil
|
23
23
|
end
|
24
24
|
end
|
25
25
|
end
|
@@ -32,7 +32,7 @@ shared_examples_for 'a tenantable model' do
|
|
32
32
|
|
33
33
|
context 'with the client id' do
|
34
34
|
before do
|
35
|
-
item.
|
35
|
+
item.tenant = client
|
36
36
|
end
|
37
37
|
|
38
38
|
it 'is valid' do
|
@@ -42,7 +42,7 @@ shared_examples_for 'a tenantable model' do
|
|
42
42
|
|
43
43
|
context 'with another client id' do
|
44
44
|
before do
|
45
|
-
item.
|
45
|
+
item.tenant = another_client
|
46
46
|
end
|
47
47
|
|
48
48
|
it 'is not valid' do
|
@@ -58,7 +58,7 @@ shared_examples_for 'a tenantable model' do
|
|
58
58
|
|
59
59
|
context 'with the client id' do
|
60
60
|
before do
|
61
|
-
item.
|
61
|
+
item.tenant = client
|
62
62
|
end
|
63
63
|
|
64
64
|
it 'is valid' do
|
@@ -68,7 +68,7 @@ shared_examples_for 'a tenantable model' do
|
|
68
68
|
|
69
69
|
context 'with another client id' do
|
70
70
|
before do
|
71
|
-
item.
|
71
|
+
item.tenant = another_client
|
72
72
|
end
|
73
73
|
|
74
74
|
it 'is valid' do
|
metadata
CHANGED
@@ -1,29 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid-multitenancy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: '2.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:
|
11
|
+
date: 2017-07-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mongoid
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '6.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '6.0'
|
27
27
|
description: MultiTenancy with Mongoid
|
28
28
|
email:
|
29
29
|
- aymeric.brisse@mperfect-memory.com
|
@@ -39,8 +39,6 @@ files:
|
|
39
39
|
- LICENSE.TXT
|
40
40
|
- README.md
|
41
41
|
- Rakefile
|
42
|
-
- gemfiles/Gemfile.mongoid-4.0
|
43
|
-
- gemfiles/Gemfile.mongoid-5.0
|
44
42
|
- lib/mongoid-multitenancy.rb
|
45
43
|
- lib/mongoid/multitenancy.rb
|
46
44
|
- lib/mongoid/multitenancy/document.rb
|
@@ -90,7 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
90
88
|
version: '0'
|
91
89
|
requirements: []
|
92
90
|
rubyforge_project:
|
93
|
-
rubygems_version: 2.
|
91
|
+
rubygems_version: 2.6.11
|
94
92
|
signing_key:
|
95
93
|
specification_version: 4
|
96
94
|
summary: Support of a multi-tenant database with Mongoid
|
@@ -117,4 +115,3 @@ test_files:
|
|
117
115
|
- spec/spec_helper.rb
|
118
116
|
- spec/support/mongoid_matchers.rb
|
119
117
|
- spec/support/shared_examples.rb
|
120
|
-
has_rdoc:
|
@@ -1,17 +0,0 @@
|
|
1
|
-
source 'https://rubygems.org'
|
2
|
-
|
3
|
-
gem 'mongoid', '~> 4.0'
|
4
|
-
|
5
|
-
gem 'rake', '~> 11.0'
|
6
|
-
|
7
|
-
group :test do
|
8
|
-
gem 'database_cleaner'
|
9
|
-
gem 'coveralls', require: false
|
10
|
-
gem 'rspec', '~> 3.1'
|
11
|
-
gem 'yard', '~> 0.8'
|
12
|
-
gem 'mongoid-rspec', '~> 2.1'
|
13
|
-
gem 'rubocop', require: false
|
14
|
-
end
|
15
|
-
|
16
|
-
# Specify your gem's dependencies in mongoid-multitenancy.gemspec
|
17
|
-
gemspec :path => '..'
|
@@ -1,17 +0,0 @@
|
|
1
|
-
source 'https://rubygems.org'
|
2
|
-
|
3
|
-
gem 'mongoid', '~> 5.1'
|
4
|
-
|
5
|
-
gem 'rake', '~> 11.0'
|
6
|
-
|
7
|
-
group :test do
|
8
|
-
gem 'database_cleaner'
|
9
|
-
gem 'coveralls', require: false
|
10
|
-
gem 'rspec', '~> 3.1'
|
11
|
-
gem 'yard', '~> 0.8'
|
12
|
-
gem 'mongoid-rspec', '~> 3.0'
|
13
|
-
gem 'rubocop', require: false
|
14
|
-
end
|
15
|
-
|
16
|
-
# Specify your gem's dependencies in mongoid-multitenancy.gemspec
|
17
|
-
gemspec :path => '..'
|