mongoid-multitenancy-2 2.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/.rspec +3 -0
- data/.travis.yml +12 -0
- data/CHANGELOG.md +54 -0
- data/Gemfile +15 -0
- data/LICENSE.TXT +20 -0
- data/README.md +283 -0
- data/Rakefile +8 -0
- data/gemfiles/Gemfile.mongoid-6 +14 -0
- data/gemfiles/Gemfile.mongoid-7 +14 -0
- data/lib/mongoid-multitenancy.rb +1 -0
- data/lib/mongoid/multitenancy.rb +34 -0
- data/lib/mongoid/multitenancy/document.rb +176 -0
- data/lib/mongoid/multitenancy/validators/tenancy.rb +36 -0
- data/lib/mongoid/multitenancy/validators/tenant_uniqueness.rb +67 -0
- data/lib/mongoid/multitenancy/version.rb +6 -0
- data/mongoid-multitenancy.gemspec +19 -0
- data/spec/immutable_spec.rb +46 -0
- data/spec/indexable_spec.rb +35 -0
- data/spec/inheritance_spec.rb +22 -0
- data/spec/mandatory_spec.rb +116 -0
- data/spec/models/account.rb +5 -0
- data/spec/models/immutable.rb +15 -0
- data/spec/models/indexable.rb +10 -0
- data/spec/models/mandatory.rb +15 -0
- data/spec/models/mutable.rb +15 -0
- data/spec/models/mutable_child.rb +9 -0
- data/spec/models/no_scopable.rb +11 -0
- data/spec/models/optional.rb +15 -0
- data/spec/models/optional_exclude.rb +15 -0
- data/spec/mongoid-multitenancy_spec.rb +37 -0
- data/spec/mutable_child_spec.rb +46 -0
- data/spec/mutable_spec.rb +50 -0
- data/spec/optional_exclude_spec.rb +71 -0
- data/spec/optional_spec.rb +207 -0
- data/spec/scopable_spec.rb +29 -0
- data/spec/spec_helper.rb +44 -0
- data/spec/support/mongoid_matchers.rb +17 -0
- data/spec/support/shared_examples.rb +80 -0
- metadata +119 -0
@@ -0,0 +1,207 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Optional do
|
4
|
+
let(:client) do
|
5
|
+
Account.create!(name: 'client')
|
6
|
+
end
|
7
|
+
|
8
|
+
let(:another_client) do
|
9
|
+
Account.create!(name: 'another client')
|
10
|
+
end
|
11
|
+
|
12
|
+
let(:item) do
|
13
|
+
Optional.new(title: 'title X', slug: 'page-x')
|
14
|
+
end
|
15
|
+
|
16
|
+
it_behaves_like 'a tenantable model'
|
17
|
+
it { is_expected.to validate_tenant_uniqueness_of(:slug) }
|
18
|
+
|
19
|
+
describe '#initialize' do
|
20
|
+
context 'within a client context' do
|
21
|
+
before do
|
22
|
+
Mongoid::Multitenancy.current_tenant = client
|
23
|
+
end
|
24
|
+
|
25
|
+
context 'when persisted' do
|
26
|
+
before do
|
27
|
+
item.tenant = nil
|
28
|
+
item.save!
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'does not override the client' do
|
32
|
+
item.reload
|
33
|
+
expect(Optional.last.tenant).to be_nil
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe '.default_scope' do
|
40
|
+
let!(:item_a) do
|
41
|
+
Mongoid::Multitenancy.with_tenant(client) do
|
42
|
+
Optional.create!(title: 'title A', slug: 'article-a')
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
let!(:item_b) do
|
47
|
+
Mongoid::Multitenancy.with_tenant(another_client) do
|
48
|
+
Optional.create!(title: 'title B', slug: 'article-b')
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
let!(:shared_item) do
|
53
|
+
Optional.create!(title: 'title C', slug: 'article-c')
|
54
|
+
end
|
55
|
+
|
56
|
+
context 'with a current tenant' do
|
57
|
+
it 'filters on the current tenant / free-tenant items' do
|
58
|
+
Mongoid::Multitenancy.with_tenant(another_client) do
|
59
|
+
expect(Optional.all.to_a).to match_array [shared_item, item_b]
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
context 'without a current tenant' do
|
65
|
+
it 'does not filter on any tenant' do
|
66
|
+
expect(Optional.all.to_a).to match_array [item_a, item_b, shared_item]
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
describe '.shared' do
|
72
|
+
let!(:item_a) do
|
73
|
+
Mongoid::Multitenancy.with_tenant(client) do
|
74
|
+
Optional.create!(title: 'title A', slug: 'article-a')
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
let!(:item_b) do
|
79
|
+
Mongoid::Multitenancy.with_tenant(another_client) do
|
80
|
+
Optional.create!(title: 'title B', slug: 'article-b')
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
let!(:shared_item) do
|
85
|
+
Optional.create!(title: 'title C', slug: 'article-c')
|
86
|
+
end
|
87
|
+
|
88
|
+
it 'returns only the shared items' do
|
89
|
+
Mongoid::Multitenancy.with_tenant(another_client) do
|
90
|
+
expect(Optional.shared.to_a).to match_array [shared_item]
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
describe '.unshared' do
|
96
|
+
let!(:item_a) do
|
97
|
+
Mongoid::Multitenancy.with_tenant(client) do
|
98
|
+
Optional.create!(title: 'title A', slug: 'article-a')
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
let!(:item_b) do
|
103
|
+
Mongoid::Multitenancy.with_tenant(another_client) do
|
104
|
+
Optional.create!(title: 'title B', slug: 'article-b')
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
let!(:shared_item) do
|
109
|
+
Optional.create!(title: 'title C', slug: 'article-c')
|
110
|
+
end
|
111
|
+
|
112
|
+
it 'returns only the shared items' do
|
113
|
+
Mongoid::Multitenancy.with_tenant(another_client) do
|
114
|
+
expect(Optional.unshared.to_a).to match_array [item_b]
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
describe '#delete_all' do
|
120
|
+
let!(:item_a) do
|
121
|
+
Mongoid::Multitenancy.with_tenant(client) do
|
122
|
+
Optional.create!(title: 'title A', slug: 'article-a')
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
let!(:item_b) do
|
127
|
+
Mongoid::Multitenancy.with_tenant(another_client) do
|
128
|
+
Optional.create!(title: 'title B', slug: 'article-b')
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
let!(:shared_item) do
|
133
|
+
Optional.create!(title: 'title C', slug: 'article-c')
|
134
|
+
end
|
135
|
+
|
136
|
+
context 'with a current tenant' do
|
137
|
+
it 'only deletes the current tenant / free-tenant items' do
|
138
|
+
Mongoid::Multitenancy.with_tenant(another_client) do
|
139
|
+
Optional.delete_all
|
140
|
+
end
|
141
|
+
|
142
|
+
expect(Optional.all.to_a).to match_array [item_a]
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
context 'without a current tenant' do
|
147
|
+
it 'deletes all the pages' do
|
148
|
+
Optional.delete_all
|
149
|
+
expect(Optional.all.to_a).to be_empty
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
describe '#valid?' do
|
155
|
+
context 'with a tenant' do
|
156
|
+
before do
|
157
|
+
Mongoid::Multitenancy.current_tenant = client
|
158
|
+
end
|
159
|
+
|
160
|
+
it 'is valid' do
|
161
|
+
expect(item).to be_valid
|
162
|
+
end
|
163
|
+
|
164
|
+
context 'with a uniqueness constraint' do
|
165
|
+
let(:duplicate) do
|
166
|
+
Optional.new(title: 'title Y', slug: 'page-x')
|
167
|
+
end
|
168
|
+
|
169
|
+
before do
|
170
|
+
item.save!
|
171
|
+
end
|
172
|
+
|
173
|
+
it 'does not allow duplicates on the same tenant' do
|
174
|
+
expect(duplicate).not_to be_valid
|
175
|
+
end
|
176
|
+
|
177
|
+
it 'allow duplicates on a different same tenant' do
|
178
|
+
Mongoid::Multitenancy.with_tenant(another_client) do
|
179
|
+
expect(duplicate).to be_valid
|
180
|
+
end
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
context 'without a tenant' do
|
186
|
+
it 'is valid' do
|
187
|
+
expect(item).to be_valid
|
188
|
+
end
|
189
|
+
|
190
|
+
context 'with a uniqueness constraint' do
|
191
|
+
let(:duplicate) do
|
192
|
+
Optional.new(title: 'title Y', slug: 'page-x')
|
193
|
+
end
|
194
|
+
|
195
|
+
before do
|
196
|
+
item.save!
|
197
|
+
end
|
198
|
+
|
199
|
+
it 'does not allow duplicates on any client' do
|
200
|
+
Mongoid::Multitenancy.with_tenant(client) do
|
201
|
+
expect(duplicate).not_to be_valid
|
202
|
+
end
|
203
|
+
end
|
204
|
+
end
|
205
|
+
end
|
206
|
+
end
|
207
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe NoScopable do
|
4
|
+
it_behaves_like 'a tenantable model' do
|
5
|
+
let(:client) do
|
6
|
+
Account.create!(name: 'client')
|
7
|
+
end
|
8
|
+
|
9
|
+
let(:another_client) do
|
10
|
+
Account.create!(name: 'another client')
|
11
|
+
end
|
12
|
+
|
13
|
+
let(:item) do
|
14
|
+
NoScopable.new(title: 'title X', slug: 'page-x')
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '.shared' do
|
19
|
+
it 'is not defined' do
|
20
|
+
expect(NoScopable).not_to respond_to(:shared)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe '.unshared' do
|
25
|
+
it 'is not defined' do
|
26
|
+
expect(NoScopable).not_to respond_to(:unshared)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
MODELS = File.join(File.dirname(__FILE__), 'models')
|
2
|
+
|
3
|
+
require 'simplecov'
|
4
|
+
require 'coveralls'
|
5
|
+
require 'database_cleaner'
|
6
|
+
|
7
|
+
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
|
8
|
+
SimpleCov::Formatter::HTMLFormatter,
|
9
|
+
Coveralls::SimpleCov::Formatter
|
10
|
+
]
|
11
|
+
SimpleCov.start
|
12
|
+
|
13
|
+
require 'rspec'
|
14
|
+
require 'mongoid'
|
15
|
+
require 'mongoid-multitenancy'
|
16
|
+
require 'mongoid-rspec'
|
17
|
+
|
18
|
+
require_relative 'support/shared_examples'
|
19
|
+
require_relative 'support/mongoid_matchers'
|
20
|
+
|
21
|
+
Dir["#{MODELS}/*.rb"].each { |f| require f }
|
22
|
+
|
23
|
+
Mongoid.configure do |config|
|
24
|
+
config.connect_to 'mongoid_multitenancy'
|
25
|
+
end
|
26
|
+
|
27
|
+
Mongoid.logger.level = Logger::INFO
|
28
|
+
Mongo::Logger.logger.level = Logger::INFO
|
29
|
+
|
30
|
+
RSpec.configure do |config|
|
31
|
+
config.include Mongoid::Matchers
|
32
|
+
|
33
|
+
config.expect_with :rspec do |c|
|
34
|
+
c.syntax = :expect
|
35
|
+
end
|
36
|
+
|
37
|
+
config.before(:each) do
|
38
|
+
DatabaseCleaner.clean
|
39
|
+
end
|
40
|
+
|
41
|
+
config.before(:each) do
|
42
|
+
Mongoid::Multitenancy.current_tenant = nil
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Matchers
|
3
|
+
module Validations
|
4
|
+
class ValidateTenantUniquenessOfMatcher < ValidateUniquenessOfMatcher
|
5
|
+
def initialize(field)
|
6
|
+
@field = field.to_s
|
7
|
+
@type = 'tenant_uniqueness'
|
8
|
+
@options = {}
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def validate_tenant_uniqueness_of(field)
|
13
|
+
ValidateTenantUniquenessOfMatcher.new(field)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
shared_examples_for 'a tenantable model' do
|
2
|
+
it { is_expected.to belong_to(:tenant) }
|
3
|
+
it { is_expected.to have_index_for(tenant_id: 1, title: 1) }
|
4
|
+
|
5
|
+
describe '#initialize' do
|
6
|
+
context 'within a client context' do
|
7
|
+
before do
|
8
|
+
Mongoid::Multitenancy.current_tenant = client
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'set the client' do
|
12
|
+
expect(item.tenant).to eq client
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context 'without a client context' do
|
17
|
+
before do
|
18
|
+
Mongoid::Multitenancy.current_tenant = nil
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'does not set any client' do
|
22
|
+
expect(item.tenant).to be_nil
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe '#valid?' do
|
28
|
+
context 'within a client context' do
|
29
|
+
before do
|
30
|
+
Mongoid::Multitenancy.current_tenant = client
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'with the client id' do
|
34
|
+
before do
|
35
|
+
item.tenant = client
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'is valid' do
|
39
|
+
expect(item).to be_valid
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
context 'with another client id' do
|
44
|
+
before do
|
45
|
+
item.tenant = another_client
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'is not valid' do
|
49
|
+
expect(item).not_to be_valid
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
context 'without a client context' do
|
55
|
+
before do
|
56
|
+
Mongoid::Multitenancy.current_tenant = nil
|
57
|
+
end
|
58
|
+
|
59
|
+
context 'with the client id' do
|
60
|
+
before do
|
61
|
+
item.tenant = client
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'is valid' do
|
65
|
+
expect(item).to be_valid
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
context 'with another client id' do
|
70
|
+
before do
|
71
|
+
item.tenant = another_client
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'is valid' do
|
75
|
+
expect(item).to be_valid
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
metadata
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid-multitenancy-2
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.0.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Botond Orban
|
8
|
+
- Aymeric Brisse
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2020-06-23 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: mongoid
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '7'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '7'
|
28
|
+
description: MultiTenancy with Mongoid
|
29
|
+
email:
|
30
|
+
- botondorban@gmail.com
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- ".gitignore"
|
36
|
+
- ".rspec"
|
37
|
+
- ".travis.yml"
|
38
|
+
- CHANGELOG.md
|
39
|
+
- Gemfile
|
40
|
+
- LICENSE.TXT
|
41
|
+
- README.md
|
42
|
+
- Rakefile
|
43
|
+
- gemfiles/Gemfile.mongoid-6
|
44
|
+
- gemfiles/Gemfile.mongoid-7
|
45
|
+
- lib/mongoid-multitenancy.rb
|
46
|
+
- lib/mongoid/multitenancy.rb
|
47
|
+
- lib/mongoid/multitenancy/document.rb
|
48
|
+
- lib/mongoid/multitenancy/validators/tenancy.rb
|
49
|
+
- lib/mongoid/multitenancy/validators/tenant_uniqueness.rb
|
50
|
+
- lib/mongoid/multitenancy/version.rb
|
51
|
+
- mongoid-multitenancy.gemspec
|
52
|
+
- spec/immutable_spec.rb
|
53
|
+
- spec/indexable_spec.rb
|
54
|
+
- spec/inheritance_spec.rb
|
55
|
+
- spec/mandatory_spec.rb
|
56
|
+
- spec/models/account.rb
|
57
|
+
- spec/models/immutable.rb
|
58
|
+
- spec/models/indexable.rb
|
59
|
+
- spec/models/mandatory.rb
|
60
|
+
- spec/models/mutable.rb
|
61
|
+
- spec/models/mutable_child.rb
|
62
|
+
- spec/models/no_scopable.rb
|
63
|
+
- spec/models/optional.rb
|
64
|
+
- spec/models/optional_exclude.rb
|
65
|
+
- spec/mongoid-multitenancy_spec.rb
|
66
|
+
- spec/mutable_child_spec.rb
|
67
|
+
- spec/mutable_spec.rb
|
68
|
+
- spec/optional_exclude_spec.rb
|
69
|
+
- spec/optional_spec.rb
|
70
|
+
- spec/scopable_spec.rb
|
71
|
+
- spec/spec_helper.rb
|
72
|
+
- spec/support/mongoid_matchers.rb
|
73
|
+
- spec/support/shared_examples.rb
|
74
|
+
homepage: https://github.com/orbanbotond/mongoid-multitenancy
|
75
|
+
licenses:
|
76
|
+
- MIT
|
77
|
+
metadata: {}
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubygems_version: 3.1.2
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: Support of a multi-tenant database with Mongoid
|
97
|
+
test_files:
|
98
|
+
- spec/immutable_spec.rb
|
99
|
+
- spec/indexable_spec.rb
|
100
|
+
- spec/inheritance_spec.rb
|
101
|
+
- spec/mandatory_spec.rb
|
102
|
+
- spec/models/account.rb
|
103
|
+
- spec/models/immutable.rb
|
104
|
+
- spec/models/indexable.rb
|
105
|
+
- spec/models/mandatory.rb
|
106
|
+
- spec/models/mutable.rb
|
107
|
+
- spec/models/mutable_child.rb
|
108
|
+
- spec/models/no_scopable.rb
|
109
|
+
- spec/models/optional.rb
|
110
|
+
- spec/models/optional_exclude.rb
|
111
|
+
- spec/mongoid-multitenancy_spec.rb
|
112
|
+
- spec/mutable_child_spec.rb
|
113
|
+
- spec/mutable_spec.rb
|
114
|
+
- spec/optional_exclude_spec.rb
|
115
|
+
- spec/optional_spec.rb
|
116
|
+
- spec/scopable_spec.rb
|
117
|
+
- spec/spec_helper.rb
|
118
|
+
- spec/support/mongoid_matchers.rb
|
119
|
+
- spec/support/shared_examples.rb
|