mongoid-multitenancy 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- NTdmMTA4ODVhMTI1Njk1YzMyMGI3OTA5ZDUxYmNkMDkzMmY3MTE4Nw==
5
- data.tar.gz: !binary |-
6
- NGY5ZGFlNmJjMjQ5MzhlOWI4M2NhY2FkMGRkZTk0MjQzNmI3ZDY1MQ==
2
+ SHA1:
3
+ metadata.gz: e0e3e8744c3c18e29194af6955cd8a2fba4562fd
4
+ data.tar.gz: 6fdadff774736f91927770271e2a28f7e64dd38b
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- ZDQwNDZmZDc3N2NlYmRiMDJhMzQ0YmVlZDc2NWVmZmY4ZWRkYTY0ZjgzMzVk
10
- Yzg1M2I5ZDZkOWM4ZDk1YTJhODk4YWNhNjVkY2I1NTM0MTNjNTRlNDZmNzBi
11
- ZTM0NDAzYTM3YmI5NDZhOTI5ODQ4MTEzMGRhNWViOTc0NWZmY2Y=
12
- data.tar.gz: !binary |-
13
- OTkwYmNkMzIwY2MwNjRhNTIwZTVkNjliNDM0NWZiNzUzNTI3NWQ0YTY3NGZk
14
- YjgxYzQ0M2RmZWMzNjYxZGNiNDUzZjNhOTkyNjJlYmU4ZGVlZDA3NmNiZjJi
15
- OWFjMzEzYjdjNWY3MTcxZWRjN2JlNzBhMDE5YzVlMzMyOTBhZmU=
6
+ metadata.gz: 3a8995b28ca405dddb5b9c45f2c505ffa89d6c40ac722137cd9e408310dc43a6ac8d659af3952fcae698b858a5a05c4708430d949b50ec30355f5040be05b7d1
7
+ data.tar.gz: a9b3e25dd1dcbfb7969f2d0913e04e980db5d2dafc9c3f3dee50a7306505b0d9bab46a730de5f9b6ab5ef4a3461a3d8e556baa328a402eeef3d73a9b9726546d
data/README.md CHANGED
@@ -157,18 +157,42 @@ end
157
157
  Mongoid indexes
158
158
  -------------------
159
159
 
160
- mongoid-multitenancy will automatically add the tenant foreign key in all your mongoid indexes to avoid to redefine all your validators.
160
+ mongoid-multitenancy automatically adds the tenant foreign key in all your mongoid indexes to avoid to redefine all your validators. If you prefer to define manually the indexes, you can use the option `full_indexes: false`.
161
+
162
+ To create a single index on the tenant field, you can use the option `index: true` like any `belongs_to` declaration (false by default)
163
+
164
+ On the example below, only one indexe will be created:
165
+
166
+ * { 'title_id' => 1, 'client_id' => 1 }
161
167
 
162
168
  ```ruby
163
169
  class Article
164
170
  include Mongoid::Document
165
171
  include Mongoid::Multitenancy::Document
166
172
 
167
- tenant(:client)
173
+ tenant :client, full_indexes: true
174
+
175
+ field :title
176
+
177
+ index({ :title => 1 })
178
+ end
179
+ ```
180
+
181
+ On the example below, 2 indexes will be created:
182
+
183
+ * { 'client_id' => 1 }
184
+ * { 'title_id' => 1 }
185
+
186
+ ```ruby
187
+ class Article
188
+ include Mongoid::Document
189
+ include Mongoid::Multitenancy::Document
190
+
191
+ tenant :client, index: true
168
192
 
169
193
  field :title
170
194
 
171
- index({ :title => 1 }) # => create an index with { :client_id => 1, :title => 1 }
195
+ index({ :title => 1 })
172
196
  end
173
197
  ```
174
198
 
@@ -4,13 +4,16 @@ module Mongoid
4
4
  extend ActiveSupport::Concern
5
5
 
6
6
  module ClassMethods
7
- attr_accessor :tenant_field
7
+ attr_accessor :tenant_field, :full_indexes
8
8
 
9
- def tenant(association = :account, options={})
9
+ def tenant(association = :account, options = {})
10
+ options = { full_indexes: true }.merge(options)
10
11
  active_model_options = options.clone
12
+ to_index = active_model_options.delete(:index)
11
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)
15
+
12
16
  # Setup the association between the class and the tenant class
13
- # TODO: should index this association if no other indexes are defined => , index: true
14
17
  belongs_to association, active_model_options
15
18
 
16
19
  # Get the tenant model and its foreign key
@@ -46,6 +49,10 @@ module Mongoid
46
49
  child.tenant association, options
47
50
  super(child)
48
51
  end
52
+
53
+ if to_index
54
+ index({self.tenant_field => 1}, { background: true })
55
+ end
49
56
  end
50
57
 
51
58
  # Redefine 'validates_with' to add the tenant scope when using a UniquenessValidator
@@ -64,7 +71,7 @@ module Mongoid
64
71
 
65
72
  # Redefine 'index' to include the tenant field in first position
66
73
  def index(spec, options = nil)
67
- spec = { self.tenant_field => 1 }.merge(spec)
74
+ spec = { self.tenant_field => 1 }.merge(spec) if self.full_indexes
68
75
  super(spec, options)
69
76
  end
70
77
 
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module Multitenancy
3
- VERSION = "0.4.0"
3
+ VERSION = "0.4.1"
4
4
  end
5
5
  end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'tenant' do
4
+
5
+ let(:client) do
6
+ Account.create!(:name => "client")
7
+ end
8
+
9
+ before do
10
+ Mongoid::Multitenancy.current_tenant = client
11
+ end
12
+
13
+ context 'without index: true' do
14
+ it 'does not create an index' do
15
+ Immutable.should_not have_index_for(:client_id => 1)
16
+ end
17
+ end
18
+
19
+ context 'with index: true' do
20
+ it 'creates an index' do
21
+ Indexable.should have_index_for(:client_id => 1)
22
+ end
23
+ end
24
+
25
+ context 'with full_indexes: true' do
26
+ it 'add the tenant field on each index' do
27
+ Immutable.should have_index_for(:client_id => 1, :title => 1)
28
+ end
29
+ end
30
+
31
+ context 'with full_indexes: false' do
32
+ it 'does not add the tenant field on each index' do
33
+ Indexable.should_not have_index_for(:client_id => 1, :title => 1)
34
+ end
35
+ end
36
+
37
+ end
@@ -0,0 +1,10 @@
1
+ class Indexable
2
+ include Mongoid::Document
3
+ include Mongoid::Multitenancy::Document
4
+
5
+ field :title, :type => String
6
+
7
+ tenant :client, class_name: 'Account', index: true, full_indexes: false
8
+
9
+ index({ :title => 1 })
10
+ end
metadata CHANGED
@@ -1,27 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-multitenancy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
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-01-28 00:00:00.000000000 Z
11
+ date: 2014-07-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongoid
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ! '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '3.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ! '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '3.0'
27
27
  description: MultiTenancy with Mongoid
@@ -31,9 +31,9 @@ executables: []
31
31
  extensions: []
32
32
  extra_rdoc_files: []
33
33
  files:
34
- - .gitignore
35
- - .rspec
36
- - .travis.yml
34
+ - ".gitignore"
35
+ - ".rspec"
36
+ - ".travis.yml"
37
37
  - Gemfile
38
38
  - LICENSE.TXT
39
39
  - README.md
@@ -47,10 +47,12 @@ files:
47
47
  - lib/mongoid/validators/tenant_validator.rb
48
48
  - mongoid-multitenancy.gemspec
49
49
  - spec/immutable_spec.rb
50
+ - spec/indexable_spec.rb
50
51
  - spec/inheritance_spec.rb
51
52
  - spec/mandatory_spec.rb
52
53
  - spec/models/account.rb
53
54
  - spec/models/immutable.rb
55
+ - spec/models/indexable.rb
54
56
  - spec/models/mandatory.rb
55
57
  - spec/models/mutable.rb
56
58
  - spec/models/mutable_child.rb
@@ -71,26 +73,28 @@ require_paths:
71
73
  - lib
72
74
  required_ruby_version: !ruby/object:Gem::Requirement
73
75
  requirements:
74
- - - ! '>='
76
+ - - ">="
75
77
  - !ruby/object:Gem::Version
76
78
  version: '0'
77
79
  required_rubygems_version: !ruby/object:Gem::Requirement
78
80
  requirements:
79
- - - ! '>='
81
+ - - ">="
80
82
  - !ruby/object:Gem::Version
81
83
  version: '0'
82
84
  requirements: []
83
85
  rubyforge_project:
84
- rubygems_version: 2.1.11
86
+ rubygems_version: 2.2.1
85
87
  signing_key:
86
88
  specification_version: 4
87
89
  summary: Support of a multi-tenant database with Mongoid
88
90
  test_files:
89
91
  - spec/immutable_spec.rb
92
+ - spec/indexable_spec.rb
90
93
  - spec/inheritance_spec.rb
91
94
  - spec/mandatory_spec.rb
92
95
  - spec/models/account.rb
93
96
  - spec/models/immutable.rb
97
+ - spec/models/indexable.rb
94
98
  - spec/models/mandatory.rb
95
99
  - spec/models/mutable.rb
96
100
  - spec/models/mutable_child.rb