mongoid-multitenancy 0.3 → 0.3.1
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
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
Y2Y1ODVlM2ZlMzZlMDRlZmEzM2MwOTRiNWI3NzRjZmRkNTNlNDI5MQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
Yjc4MjEyYTlkZWUxNDAwYjNlZTU5ZGQ3NmVkYWUzYWRlNDU1ZDE0NA==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MDYyZjllZGM4ZDFjNmU2ZWYxNGZlMTE1ZTUwZTQ0YmU2MjU4MTg4YmY2OWYx
|
10
|
+
ZWI2MGU0ZTdmNjY2YTEwMmYxZTBmOWRlMDQ5OWIwN2QxYjM4NjRiZTEyMjll
|
11
|
+
MGYzOTUwOGUyMjNmMmY4M2NkNWMyYzc3ZmU3NjBhNGM4Y2RlYTY=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
N2VjZDQ1N2MzZTE4MjVhOTI2NDhkN2YzMGEwNTQwYzQyYjIwMzk2N2IyMTNh
|
14
|
+
NjM3MDQ4MWVlMThjZjVjNDc1OTkyNGJmZWE0OGNhZGQxZGYzNTg3Mzc1MTAy
|
15
|
+
NzEzZTI3ZThjYzA0ODdlMDE2YTMzMmZhYTIwNmU1N2Y4MDk5MDI=
|
data/Gemfile.lock
CHANGED
@@ -3,35 +3,22 @@ module Mongoid
|
|
3
3
|
module Document
|
4
4
|
extend ActiveSupport::Concern
|
5
5
|
|
6
|
-
included do
|
7
|
-
private
|
8
|
-
|
9
|
-
# Check that the tenant foreign key field has not been changed once the object has been persisted
|
10
|
-
def check_tenant_immutability
|
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
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
6
|
module ClassMethods
|
20
|
-
|
21
|
-
attr_reader :tenant_field, :tenant_options
|
7
|
+
attr_accessor :tenant_field
|
22
8
|
|
23
9
|
def tenant(association = :account, options={})
|
24
|
-
|
10
|
+
tenant_options = { optional: options.delete(:optional), immutable: options.delete(:immutable) { true } }
|
25
11
|
# Setup the association between the class and the tenant class
|
26
12
|
# TODO: should index this association if no other indexes are defined => , index: true
|
27
13
|
belongs_to association, options
|
28
14
|
|
29
15
|
# Get the tenant model and its foreign key
|
30
|
-
|
31
|
-
|
16
|
+
tenant_field = reflect_on_association(association).foreign_key
|
17
|
+
self.tenant_field = tenant_field
|
32
18
|
|
33
19
|
# Validates the presence of the association key
|
34
|
-
validates_presence_of
|
20
|
+
validates_presence_of tenant_field unless tenant_options[:optional]
|
21
|
+
validates tenant_field, immutable: { field: tenant_field } if tenant_options[:immutable]
|
35
22
|
|
36
23
|
# Set the current_tenant on newly created objects
|
37
24
|
after_initialize lambda { |m|
|
@@ -41,17 +28,14 @@ module Mongoid
|
|
41
28
|
true
|
42
29
|
}
|
43
30
|
|
44
|
-
# Rewrite accessors to make tenant foreign_key/association immutable
|
45
|
-
validate :check_tenant_immutability, :on => :update if @tenant_options[:immutable]
|
46
|
-
|
47
31
|
# Set the default_scope to scope to current tenant
|
48
32
|
default_scope lambda {
|
49
33
|
criteria = if Multitenancy.current_tenant
|
50
|
-
if
|
34
|
+
if tenant_options[:optional]
|
51
35
|
#any_of({ self.tenant_field => Multitenancy.current_tenant.id }, { self.tenant_field => nil })
|
52
|
-
where({
|
36
|
+
where({ tenant_field.to_sym.in => [Multitenancy.current_tenant.id, nil] })
|
53
37
|
else
|
54
|
-
where({
|
38
|
+
where({ tenant_field => Multitenancy.current_tenant.id })
|
55
39
|
end
|
56
40
|
else
|
57
41
|
where(nil)
|
data/lib/mongoid/multitenancy.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
+
require "mongoid"
|
2
|
+
require "mongoid/multitenancy/document"
|
3
|
+
require "mongoid/multitenancy/version"
|
4
|
+
require "mongoid/validators/immutable_validator"
|
5
|
+
|
1
6
|
module Mongoid
|
2
7
|
module Multitenancy
|
3
|
-
require "mongoid"
|
4
|
-
|
5
|
-
require "mongoid/multitenancy/document"
|
6
|
-
require "mongoid/multitenancy/version"
|
7
|
-
|
8
8
|
class << self
|
9
9
|
|
10
10
|
# Set the current tenant. Make it Thread aware
|
@@ -0,0 +1,7 @@
|
|
1
|
+
class ImmutableValidator < ActiveModel::EachValidator
|
2
|
+
def validate_each(object, attribute, value)
|
3
|
+
if object.send(:attribute_changed?, options[:field]) and object.send(:attribute_was, options[:field])
|
4
|
+
object.errors.add(options[:field], 'is immutable and cannot be updated') and false
|
5
|
+
end
|
6
|
+
end
|
7
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid-multitenancy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aymeric Brisse
|
@@ -126,6 +126,7 @@ files:
|
|
126
126
|
- lib/mongoid/multitenancy.rb
|
127
127
|
- lib/mongoid/multitenancy/document.rb
|
128
128
|
- lib/mongoid/multitenancy/version.rb
|
129
|
+
- lib/mongoid/validators/immutable_validator.rb
|
129
130
|
- mongoid-multitenancy.gemspec
|
130
131
|
- spec/models/account.rb
|
131
132
|
- spec/models/article.rb
|