mongoid-multitenancy 1.0.0 → 1.1.0

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: 95438e3e7adc7dc21d06a0deec16979183b37738
4
- data.tar.gz: 61ce79abafb106d53af8dbf35c74523a04b43a31
3
+ metadata.gz: e24dad42cdd8d6692c7c37ae3d370c11615423ed
4
+ data.tar.gz: 0a6b110bab9bd521fe3be96a76cc7f10e9ef4957
5
5
  SHA512:
6
- metadata.gz: 58cf2892dd86cd822f1fc436e745e53ffd128680164226323e5e32ac328bd14db877cd990ea29e01db130e4155b205a1de1a9b6cd040c7bc0381d1dfd89dc72a
7
- data.tar.gz: 3ac676cd7c0df46eb10a703967f9375d51674f6a61f3a8ae637ea8cd7562b6361905def86aa52269af8dfcdfc1cbd29a9e4ea2eee8665ff35a1341207247d7fa
6
+ metadata.gz: 5ffaa31b8280e405b4b0e7acd7b5ad84e8c15ae3815be55a962526ef6cf9d918ea76d144041856d0f281153e31dbea025bdf5cee6ab0d1546436751022fa777c
7
+ data.tar.gz: 68fa75bceb0e1103a0307edafb1e0b2d4110bb1f76473b13a823d560dd35e5ca00a8bbc2c94413fcb358d77eb24e608cc9bdf8efe36139b8347051188cca5ecf
data/CHANGELOG.md CHANGED
@@ -1,3 +1,13 @@
1
+ ## 1.1
2
+
3
+ ### New Features
4
+
5
+ * Add scopes *shared* and *unshared* (1b5c420)
6
+
7
+ ### Fixes
8
+
9
+ * When a tenant is optional, do not override the tenant during persisted document initialization (81a9b45)
10
+
1
11
  ## 1.0.0
2
12
 
3
13
  ### New Features
@@ -34,13 +34,13 @@ module Mongoid
34
34
  belongs_to association, assoc_options
35
35
 
36
36
  # Get the tenant model and its foreign key
37
- self.tenant_field = reflect_on_association(association).foreign_key
37
+ self.tenant_field = reflect_on_association(association).foreign_key.to_sym
38
38
  self.tenant_options = multitenant_options
39
39
 
40
40
  # Validates the tenant field
41
41
  validates_tenancy_of tenant_field, multitenant_options
42
42
 
43
- define_default_scope
43
+ define_scopes
44
44
  define_initializer association
45
45
  define_inherited association, options
46
46
  define_index if multitenant_options[:index]
@@ -120,7 +120,7 @@ module Mongoid
120
120
  def define_initializer(association)
121
121
  # Apply the default value when the default scope is complex (optional tenant)
122
122
  after_initialize lambda {
123
- if Multitenancy.current_tenant && send(association.to_sym).nil?
123
+ if Multitenancy.current_tenant && send(association.to_sym).nil? && new_record?
124
124
  send "#{association}=".to_sym, Multitenancy.current_tenant
125
125
  end
126
126
  }
@@ -138,14 +138,14 @@ module Mongoid
138
138
 
139
139
  # @private
140
140
  #
141
- # Set the default scope
142
- def define_default_scope
141
+ # Define the scopes
142
+ def define_scopes
143
143
  # Set the default_scope to scope to current tenant
144
144
  default_scope lambda {
145
145
  if Multitenancy.current_tenant
146
146
  tenant_id = Multitenancy.current_tenant.id
147
147
  if tenant_options[:optional]
148
- where(tenant_field.to_sym.in => [tenant_id, nil])
148
+ where(tenant_field.in => [tenant_id, nil])
149
149
  else
150
150
  where(tenant_field => tenant_id)
151
151
  end
@@ -153,6 +153,9 @@ module Mongoid
153
153
  where(nil)
154
154
  end
155
155
  }
156
+
157
+ scope :shared, -> { where(tenant_field => nil) }
158
+ scope :unshared, -> { where(tenant_field => Multitenancy.current_tenant.id) }
156
159
  end
157
160
 
158
161
  # @private
@@ -1,6 +1,6 @@
1
1
  module Mongoid
2
2
  module Multitenancy
3
3
  # Version
4
- VERSION = '1.0.0'
4
+ VERSION = '1.1.0'
5
5
  end
6
6
  end
@@ -20,16 +20,13 @@ module Mongoid
20
20
 
21
21
  # Affects a tenant temporary for a block execution
22
22
  def with_tenant(tenant, &block)
23
- if block.nil?
24
- raise ArgumentError, 'block required'
25
- end
23
+ raise ArgumentError, 'block required' if block.nil?
26
24
 
27
25
  old_tenant = self.current_tenant
28
26
  self.current_tenant = tenant
29
-
30
- block.call
31
-
27
+ result = block.call
32
28
  self.current_tenant = old_tenant
29
+ result
33
30
  end
34
31
  end
35
32
  end
@@ -17,45 +17,130 @@ describe Optional do
17
17
  it_behaves_like 'a tenantable model'
18
18
  it { is_expected.to validate_tenant_uniqueness_of(:slug) }
19
19
 
20
+ describe '#initialize' do
21
+ context 'within a client context' do
22
+ before do
23
+ Mongoid::Multitenancy.current_tenant = client
24
+ end
25
+
26
+ context 'when persisted' do
27
+ before do
28
+ item.client = nil
29
+ item.save!
30
+ end
31
+
32
+ it 'does not override the client' do
33
+ item.reload
34
+ expect(Optional.last.client).to be_nil
35
+ end
36
+ end
37
+ end
38
+ end
39
+
20
40
  describe '.default_scope' do
21
- before do
22
- @itemC = Optional.create!(title: 'title C', slug: 'article-c')
23
- Mongoid::Multitenancy.with_tenant(client) { @itemX = Optional.create!(title: 'title X', slug: 'article-x', :client => client) }
24
- Mongoid::Multitenancy.with_tenant(another_client) { @itemY = Optional.create!(title: 'title Y', slug: 'article-y', :client => another_client) }
41
+ let!(:item_a) do
42
+ Mongoid::Multitenancy.with_tenant(client) do
43
+ Optional.create!(title: 'title A', slug: 'article-a')
44
+ end
25
45
  end
26
46
 
27
- context 'with a current tenant' do
28
- before do
29
- Mongoid::Multitenancy.current_tenant = another_client
47
+ let!(:item_b) do
48
+ Mongoid::Multitenancy.with_tenant(another_client) do
49
+ Optional.create!(title: 'title B', slug: 'article-b')
30
50
  end
51
+ end
52
+
53
+ let!(:shared_item) do
54
+ Optional.create!(title: 'title C', slug: 'article-c')
55
+ end
31
56
 
57
+ context 'with a current tenant' do
32
58
  it 'filters on the current tenant / free-tenant items' do
33
- expect(Optional.all.to_a).to match_array [@itemY, @itemC]
59
+ Mongoid::Multitenancy.with_tenant(another_client) do
60
+ expect(Optional.all.to_a).to match_array [shared_item, item_b]
61
+ end
34
62
  end
35
63
  end
36
64
 
37
65
  context 'without a current tenant' do
38
- before do
39
- Mongoid::Multitenancy.current_tenant = nil
66
+ it 'does not filter on any tenant' do
67
+ expect(Optional.all.to_a).to match_array [item_a, item_b, shared_item]
40
68
  end
69
+ end
70
+ end
41
71
 
42
- it 'does not filter on any tenant' do
43
- expect(Optional.all.to_a).to match_array [@itemC, @itemX, @itemY]
72
+ describe '.shared' do
73
+ let!(:item_a) do
74
+ Mongoid::Multitenancy.with_tenant(client) do
75
+ Optional.create!(title: 'title A', slug: 'article-a')
76
+ end
77
+ end
78
+
79
+ let!(:item_b) do
80
+ Mongoid::Multitenancy.with_tenant(another_client) do
81
+ Optional.create!(title: 'title B', slug: 'article-b')
82
+ end
83
+ end
84
+
85
+ let!(:shared_item) do
86
+ Optional.create!(title: 'title C', slug: 'article-c')
87
+ end
88
+
89
+ it 'returns only the shared items' do
90
+ Mongoid::Multitenancy.with_tenant(another_client) do
91
+ expect(Optional.shared.to_a).to match_array [shared_item]
92
+ end
93
+ end
94
+ end
95
+
96
+ describe '.unshared' do
97
+ let!(:item_a) do
98
+ Mongoid::Multitenancy.with_tenant(client) do
99
+ Optional.create!(title: 'title A', slug: 'article-a')
100
+ end
101
+ end
102
+
103
+ let!(:item_b) do
104
+ Mongoid::Multitenancy.with_tenant(another_client) do
105
+ Optional.create!(title: 'title B', slug: 'article-b')
106
+ end
107
+ end
108
+
109
+ let!(:shared_item) do
110
+ Optional.create!(title: 'title C', slug: 'article-c')
111
+ end
112
+
113
+ it 'returns only the shared items' do
114
+ Mongoid::Multitenancy.with_tenant(another_client) do
115
+ expect(Optional.unshared.to_a).to match_array [item_b]
44
116
  end
45
117
  end
46
118
  end
47
119
 
48
120
  describe '#delete_all' do
49
- before do
50
- @itemC = Optional.create!(title: 'title C', slug: 'article-c')
51
- Mongoid::Multitenancy.with_tenant(client) { @itemX = Optional.create!(title: 'title X', slug: 'article-x', :client => client) }
52
- Mongoid::Multitenancy.with_tenant(another_client) { @itemY = Optional.create!(title: 'title Y', slug: 'article-y', :client => another_client) }
121
+ let!(:item_a) do
122
+ Mongoid::Multitenancy.with_tenant(client) do
123
+ Optional.create!(title: 'title A', slug: 'article-a')
124
+ end
125
+ end
126
+
127
+ let!(:item_b) do
128
+ Mongoid::Multitenancy.with_tenant(another_client) do
129
+ Optional.create!(title: 'title B', slug: 'article-b')
130
+ end
131
+ end
132
+
133
+ let!(:shared_item) do
134
+ Optional.create!(title: 'title C', slug: 'article-c')
53
135
  end
54
136
 
55
137
  context 'with a current tenant' do
56
138
  it 'only deletes the current tenant / free-tenant items' do
57
- Mongoid::Multitenancy.with_tenant(another_client) { Optional.delete_all }
58
- expect(Optional.all.to_a).to match_array [@itemX]
139
+ Mongoid::Multitenancy.with_tenant(another_client) do
140
+ Optional.delete_all
141
+ end
142
+
143
+ expect(Optional.all.to_a).to match_array [item_a]
59
144
  end
60
145
  end
61
146
 
data/spec/spec_helper.rb CHANGED
@@ -43,7 +43,7 @@ RSpec.configure do |config|
43
43
  DatabaseCleaner.clean
44
44
  end
45
45
 
46
- config.after(:each) do
46
+ config.before(:each) do
47
47
  Mongoid::Multitenancy.current_tenant = nil
48
48
  end
49
49
  end
@@ -1,5 +1,3 @@
1
- require 'pp'
2
-
3
1
  class DatabaseCleaner
4
2
  class << self
5
3
  def clean
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: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aymeric Brisse
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-13 00:00:00.000000000 Z
11
+ date: 2015-10-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongoid