mongoid-multitenancy 0.4.2 → 0.4.3
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 +4 -4
- data/Gemfile +1 -1
- data/README.md +21 -3
- data/lib/mongoid/multitenancy/document.rb +24 -23
- data/lib/mongoid/multitenancy/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: facc15f874a27e6541f1d7963370a4c372c863e6
|
4
|
+
data.tar.gz: 686e5f4eca2e9c6e9b6245406aa0904bf33c7ee2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e7ff42bb09808638b9b10ca05d302d3f3c7b5974ef6aedd0a7debecdccbbb557889ac916b5b50727bd556930bb5299a317d52ba7742f4d7ebc24fe558c7329d3
|
7
|
+
data.tar.gz: d6df90fd199a0420952c244148e7ab7f10c60ab157d675adb76c577d5170395a29225d3fe9ee8dab6833d0b51a2d3b0c37253b59d2bec7705375f107d1ba19f8
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -99,7 +99,8 @@ Mongoid::Multitenancy.current_tenant = Client.find_by(:name => 'Perfect Memory')
|
|
99
99
|
Article.all # => all articles where client_id => 50ca04b86c82bfc125000025
|
100
100
|
|
101
101
|
# New objects are scoped to the current tenant
|
102
|
-
Article.new(:title => 'New blog')
|
102
|
+
article = Article.new(:title => 'New blog')
|
103
|
+
article.save # => <#Article _id: 50ca04b86c82bfc125000044, title: 'New blog', client_id: 50ca04b86c82bfc125000025>
|
103
104
|
|
104
105
|
# It can make the tenant field immutable once it is persisted to avoid inconsistency
|
105
106
|
article.persisted? # => true
|
@@ -109,10 +110,27 @@ article.valid? # => false
|
|
109
110
|
|
110
111
|
**Optional tenant**
|
111
112
|
|
112
|
-
When setting an optional tenant, for example to allow shared instances between all the tenants, the default scope will return both the tenant and the free-tenant items. That means that using `Article.delete_all` or `Article.destroy_all` will remove the shared items too
|
113
|
+
When setting an optional tenant, for example to allow shared instances between all the tenants, the default scope will return both the tenant and the free-tenant items. That means that using `Article.delete_all` or `Article.destroy_all` will **remove the shared items too**. And that means too that **the tenant must be set manually**.
|
113
114
|
|
114
115
|
```ruby
|
115
|
-
Article
|
116
|
+
class Article
|
117
|
+
include Mongoid::Document
|
118
|
+
include Mongoid::Multitenancy::Document
|
119
|
+
|
120
|
+
tenant(:client, optional: true)
|
121
|
+
|
122
|
+
field :title, :type => String
|
123
|
+
end
|
124
|
+
|
125
|
+
Mongoid::Multitenancy.with_tenant(client_instance) do
|
126
|
+
Article.all # => all articles where client_id.in [50ca04b86c82bfc125000025, nil]
|
127
|
+
article = Article.new(:title => 'New article')
|
128
|
+
article.save # => <#Article _id: 50ca04b86c82bfc125000044, title: 'New blog', client_id: nil>
|
129
|
+
|
130
|
+
# tenant needs to be set manually
|
131
|
+
article.tenant = client_instance
|
132
|
+
article.save => <#Article _id: 50ca04b86c82bfc125000044, title: 'New blog', client_id: 50ca04b86c82bfc125000025>
|
133
|
+
end
|
116
134
|
```
|
117
135
|
|
118
136
|
Rails
|
@@ -6,39 +6,28 @@ module Mongoid
|
|
6
6
|
module ClassMethods
|
7
7
|
attr_accessor :tenant_field, :full_indexes
|
8
8
|
|
9
|
+
MULTITENANCY_OPTIONS = [:optional, :immutable, :full_indexes, :index]
|
10
|
+
|
9
11
|
def tenant(association = :account, options = {})
|
10
|
-
options = { full_indexes: true }.merge(options)
|
11
|
-
active_model_options = options.clone
|
12
|
-
to_index = active_model_options.delete(:index)
|
13
|
-
tenant_options = { optional: active_model_options.delete(:optional), immutable: active_model_options.delete(:immutable) { true } }
|
14
|
-
self.full_indexes = active_model_options.delete(:full_indexes)
|
12
|
+
options = { full_indexes: true, immutable: true }.merge(options)
|
15
13
|
|
16
14
|
# Setup the association between the class and the tenant class
|
17
|
-
belongs_to association,
|
15
|
+
belongs_to association, extract_association_options(options)
|
18
16
|
|
19
17
|
# Get the tenant model and its foreign key
|
20
|
-
tenant_field = reflect_on_association(association).foreign_key
|
21
|
-
self.
|
18
|
+
self.tenant_field = reflect_on_association(association).foreign_key
|
19
|
+
self.full_indexes = options[:full_indexes]
|
22
20
|
|
23
21
|
# Validates the tenant field
|
24
|
-
validates tenant_field, tenant:
|
25
|
-
|
26
|
-
# Set the current_tenant on newly created objects
|
27
|
-
before_validation lambda { |m|
|
28
|
-
if Multitenancy.current_tenant and !tenant_options[:optional] and m.send(association.to_sym).nil?
|
29
|
-
m.send "#{association}=".to_sym, Multitenancy.current_tenant
|
30
|
-
end
|
31
|
-
true
|
32
|
-
}
|
22
|
+
validates tenant_field, tenant: options
|
33
23
|
|
34
24
|
# Set the default_scope to scope to current tenant
|
35
25
|
default_scope lambda {
|
36
|
-
|
37
|
-
if
|
38
|
-
|
39
|
-
where({ tenant_field.to_sym.in => [Multitenancy.current_tenant.id, nil] })
|
26
|
+
if Multitenancy.current_tenant
|
27
|
+
if options[:optional]
|
28
|
+
where(self.tenant_field.to_sym.in => [Multitenancy.current_tenant.id, nil])
|
40
29
|
else
|
41
|
-
where(
|
30
|
+
where(self.tenant_field => Multitenancy.current_tenant.id)
|
42
31
|
end
|
43
32
|
else
|
44
33
|
where(nil)
|
@@ -50,7 +39,7 @@ module Mongoid
|
|
50
39
|
super(child)
|
51
40
|
end
|
52
41
|
|
53
|
-
if
|
42
|
+
if options[:index]
|
54
43
|
index({self.tenant_field => 1}, { background: true })
|
55
44
|
end
|
56
45
|
end
|
@@ -79,6 +68,18 @@ module Mongoid
|
|
79
68
|
def delete_all(conditions = nil)
|
80
69
|
scoped.where(conditions).delete
|
81
70
|
end
|
71
|
+
|
72
|
+
private
|
73
|
+
|
74
|
+
def extract_association_options(options)
|
75
|
+
new_options = {}
|
76
|
+
|
77
|
+
options.each do |k, v|
|
78
|
+
new_options[k] = v unless MULTITENANCY_OPTIONS.include?(k)
|
79
|
+
end
|
80
|
+
|
81
|
+
new_options
|
82
|
+
end
|
82
83
|
end
|
83
84
|
end
|
84
85
|
end
|
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: 0.4.
|
4
|
+
version: 0.4.3
|
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: 2015-04-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mongoid
|
@@ -83,7 +83,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
83
83
|
version: '0'
|
84
84
|
requirements: []
|
85
85
|
rubyforge_project:
|
86
|
-
rubygems_version: 2.
|
86
|
+
rubygems_version: 2.4.4
|
87
87
|
signing_key:
|
88
88
|
specification_version: 4
|
89
89
|
summary: Support of a multi-tenant database with Mongoid
|