mongoid-multitenancy 0.4.2 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e151c5f6685219fa3632b566d9f6b5da8af7d5eb
4
- data.tar.gz: dff2b8a2e7751b2a46441d0f9fb1a3664e3056d9
3
+ metadata.gz: facc15f874a27e6541f1d7963370a4c372c863e6
4
+ data.tar.gz: 686e5f4eca2e9c6e9b6245406aa0904bf33c7ee2
5
5
  SHA512:
6
- metadata.gz: 9d82f6b364671ed9e87c3c5b301d10d34c0fdb2bf169e4c80a532fb001b6a0e75df91be8bb585452370eef9f8c3093902764799ca1a3dc02086b49ab14cc1a36
7
- data.tar.gz: b8e2737fb53ad53d0a77b145b04e1f4bfec1310e47e7b20ca85ee23c93aab50cffd0b8892b6de5e1e0d1a8554fc44da62f93ca985cb5a4b50adaf713a3011db6
6
+ metadata.gz: e7ff42bb09808638b9b10ca05d302d3f3c7b5974ef6aedd0a7debecdccbbb557889ac916b5b50727bd556930bb5299a317d52ba7742f4d7ebc24fe558c7329d3
7
+ data.tar.gz: d6df90fd199a0420952c244148e7ab7f10c60ab157d675adb76c577d5170395a29225d3fe9ee8dab6833d0b51a2d3b0c37253b59d2bec7705375f107d1ba19f8
data/Gemfile CHANGED
@@ -1,6 +1,6 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'mongoid', '~> 4.0.alpha'
3
+ gem 'mongoid', '~> 4.0'
4
4
 
5
5
  gem 'rake', '~> 10.0'
6
6
 
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') # => <#Article _id: nil, title: 'New blog', :client_id: 50ca04b86c82bfc125000025>
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.all # => all articles where client_id.in [50ca04b86c82bfc125000025, nil]
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, active_model_options
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.tenant_field = tenant_field
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: tenant_options
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
- criteria = if Multitenancy.current_tenant
37
- if tenant_options[:optional]
38
- #any_of({ self.tenant_field => Multitenancy.current_tenant.id }, { self.tenant_field => nil })
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({ tenant_field => Multitenancy.current_tenant.id })
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 to_index
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
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module Multitenancy
3
- VERSION = "0.4.2"
3
+ VERSION = "0.4.3"
4
4
  end
5
5
  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.2
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: 2014-08-04 00:00:00.000000000 Z
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.2.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