mongoid-multitenancy 0.1 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +9 -9
- data/lib/mongoid/multitenancy/document.rb +6 -2
- data/lib/mongoid/multitenancy/version.rb +1 -1
- metadata +1 -1
data/README.md
CHANGED
@@ -16,7 +16,7 @@ Installation
|
|
16
16
|
|
17
17
|
Add this line to your application's Gemfile:
|
18
18
|
|
19
|
-
gem '
|
19
|
+
gem 'mongoid-multitenancy'
|
20
20
|
|
21
21
|
And then execute:
|
22
22
|
|
@@ -24,7 +24,7 @@ And then execute:
|
|
24
24
|
|
25
25
|
Or install it yourself as:
|
26
26
|
|
27
|
-
$ gem install
|
27
|
+
$ gem install mongoid-multitenancy
|
28
28
|
|
29
29
|
Usage
|
30
30
|
===============
|
@@ -40,13 +40,13 @@ There are two ways to set the current tenant: (1) by setting the current tenant
|
|
40
40
|
|
41
41
|
**Setting the current tenant in a controller, manually**
|
42
42
|
|
43
|
-
Mongoid::
|
43
|
+
Mongoid::Multitenancy.current_tenant = client_instance
|
44
44
|
|
45
|
-
Setting the current_tenant yourself requires you to use a before_filter to set the Mongoid::
|
45
|
+
Setting the current_tenant yourself requires you to use a before_filter to set the Mongoid::Multitenancy.current_tenant variable.
|
46
46
|
|
47
47
|
**Setting the current tenant for a block**
|
48
48
|
|
49
|
-
Mongoid::
|
49
|
+
Mongoid::Multitenancy.with_tenant(client_instance) do
|
50
50
|
# Current tenant is set for all code in this block
|
51
51
|
end
|
52
52
|
|
@@ -79,7 +79,7 @@ The association passed to the `tenant` function must be valid.
|
|
79
79
|
Some examples to illustrate this behavior:
|
80
80
|
|
81
81
|
# This manually sets the current tenant for testing purposes. In your app this is handled by the gem.
|
82
|
-
Mongoid::
|
82
|
+
Mongoid::Multitenancy.current_tenant = Client.find_by(:name => 'Perfect Memory') # => <#Client _id:50ca04b86c82bfc125000025, :name: "Perfect Memory">
|
83
83
|
|
84
84
|
# All searches are scoped by the tenant, the following searches will only return objects belonging to the current client.
|
85
85
|
Article.all # => all articles where client_id => 50ca04b86c82bfc125000025
|
@@ -88,7 +88,7 @@ Some examples to illustrate this behavior:
|
|
88
88
|
Article.new(:title => 'New blog') # => <#Article _id: nil, title: 'New blog', :client_id: 50ca04b86c82bfc125000025>
|
89
89
|
|
90
90
|
# It makes the tenant field immutable once it is persisted to avoid inconsistency
|
91
|
-
article.
|
91
|
+
article.persisted? # => true
|
92
92
|
article.client = another_client
|
93
93
|
article.valid? # => false
|
94
94
|
|
@@ -104,11 +104,11 @@ If you are using Rails, you may want to set the current tenant at each request.
|
|
104
104
|
|
105
105
|
def set_current_client
|
106
106
|
current_client = Client.find_by_host(request.host)
|
107
|
-
Mongoid::
|
107
|
+
Mongoid::Multitenancy.current_tenant = current_client
|
108
108
|
end
|
109
109
|
end
|
110
110
|
|
111
|
-
Setting the current_tenant yourself requires you to use a before_filter to set the Mongoid::
|
111
|
+
Setting the current_tenant yourself requires you to use a before_filter to set the Mongoid::Multitenancy.current_tenant variable.
|
112
112
|
|
113
113
|
Mongoid Uniqueness validators
|
114
114
|
-------------------
|
@@ -8,7 +8,11 @@ module Mongoid
|
|
8
8
|
|
9
9
|
# Check that the tenant foreign key field has not been changed once the object has been persisted
|
10
10
|
def check_tenant_immutability
|
11
|
-
|
11
|
+
# We check that the tenant has changed and that the old was not nil to avoid after_create callbacks issues.
|
12
|
+
# Indeed in this case, even if the flag is set to persisted, changes have not yet been reset.
|
13
|
+
if attribute_changed?(self.class.tenant_field) and attribute_was(self.class.tenant_field)
|
14
|
+
self.errors.add(self.class.tenant_field, 'is immutable and cannot be updated' )
|
15
|
+
end
|
12
16
|
end
|
13
17
|
end
|
14
18
|
|
@@ -32,7 +36,7 @@ module Mongoid
|
|
32
36
|
after_initialize lambda { |m| m.send "#{association}=".to_sym, Multitenancy.current_tenant if Multitenancy.current_tenant ; true }
|
33
37
|
|
34
38
|
# Rewrite accessors to make tenant foreign_key/association immutable
|
35
|
-
validate :check_tenant_immutability, :
|
39
|
+
validate :check_tenant_immutability, :on => :update
|
36
40
|
|
37
41
|
# Set the default_scope to scope to current tenant
|
38
42
|
default_scope lambda {
|