mongoid-multitenancy 2.0.4 → 2.0.5

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
  SHA256:
3
- metadata.gz: 6b73b7aa6b228218cdc8f7984b1e1b6ba340f06f23ad4730e533c7eda4fe9023
4
- data.tar.gz: bf9b89578b7afef058724f6e3170b5158590894d6267d419df9675c1fa786e08
3
+ metadata.gz: 485b7b5927867bd3f2e3d33aa0705a0f78422a81cba9b5f207b7c71ba56fda65
4
+ data.tar.gz: 07c63615e55a7a66d7776bf61b8fd741dd15105bfb14404742242dfe36cc0387
5
5
  SHA512:
6
- metadata.gz: 59a416b22144c02a3617356317421b27b6bb73947556c96c34ce36e67525d2b6a84a15bc5cd9f66d0acd9efd93e88cccaa1273c1dd4d3e6a08651acd294f5777
7
- data.tar.gz: 3eefea63c163088fd87870cbe58d04aa8f17df612da993c552f666d01ebb4d9bf520715735f1afc04a234fba7a25573ba3e06b37bbc2c6c95951e8f4370da579
6
+ metadata.gz: 406b1104c97d9faa39822f114bb4c4b0687e2aedcf0f307e5dc2d90a96ef11a89509988a585f79030aec5ec2948ead959a351d558cc933152f55344b6ee3c96f
7
+ data.tar.gz: e46f79ec9b5dbe8adef7f3f5da17e6eb34397f600679e5c2f8c140b1c6911a88712b919b3c3e7a4fc9938f63731354c268e97345261441384ac821f83d2e3ebd
data/CHANGELOG.md CHANGED
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
4
4
  The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
5
5
  and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## [2.0.5] - 2024-09-20
8
+ ### Fixed
9
+
10
+ * Fix uniqueness validator when used with conditions
11
+
7
12
  ## [2.0.4] - 2024-06-17
8
13
  ### Fixed
9
14
 
data/README.md CHANGED
@@ -226,6 +226,22 @@ class Article
226
226
  end
227
227
  ```
228
228
 
229
+ TenantUniqueness validator also allow to specify additional `conditions` to limit the uniqueness of the constraint.
230
+
231
+ ```ruby
232
+ class Article
233
+ include Mongoid::Document
234
+ include Mongoid::Multitenancy::Document
235
+
236
+ tenant :tenant, optional: true
237
+
238
+ field :title
239
+ field :slug
240
+
241
+ validates_tenant_uniqueness_of :slug, exclude_shared: true, conditions: -> { ne(title: nil) }
242
+ end
243
+ ```
244
+
229
245
  Mongoid indexes
230
246
  -------------------
231
247
 
@@ -37,7 +37,11 @@ module Mongoid
37
37
 
38
38
  # <<Add the tenant Criteria>>
39
39
  criteria = with_tenant_criterion(criteria, klass, document)
40
- criteria = criteria.merge(options[:conditions].call) if options[:conditions]
40
+ # Add additional conditions
41
+ if options[:conditions]
42
+ conditions = klass.unscoped { options[:conditions].call }
43
+ criteria = criteria.merge(conditions)
44
+ end
41
45
 
42
46
  if criteria.read(mode: :primary).exists?
43
47
  add_error(document, attribute, value)
@@ -1,6 +1,6 @@
1
1
  module Mongoid
2
2
  module Multitenancy
3
3
  # Version
4
- VERSION = '2.0.4'.freeze
4
+ VERSION = '2.0.5'.freeze
5
5
  end
6
6
  end
@@ -0,0 +1,61 @@
1
+ require 'spec_helper'
2
+
3
+ describe ConditionalUniqueness do
4
+ let(:client) do
5
+ Account.create!(name: 'client')
6
+ end
7
+
8
+ let(:another_client) do
9
+ Account.create!(name: 'another client')
10
+ end
11
+
12
+ let(:item) do
13
+ ConditionalUniqueness.new(approved: true, slug: 'page-x')
14
+ end
15
+
16
+ it_behaves_like 'a tenantable model'
17
+
18
+ describe '#valid?' do
19
+ context 'with a tenant' do
20
+ before do
21
+ Mongoid::Multitenancy.current_tenant = client
22
+ end
23
+
24
+ it 'is valid' do
25
+ expect(item).to be_valid
26
+ end
27
+
28
+ context 'with a duplicate on the constraint' do
29
+ let(:duplicate) do
30
+ ConditionalUniqueness.new(approved: true, slug: 'page-x')
31
+ end
32
+
33
+ before do
34
+ item.save!
35
+ end
36
+
37
+ it 'is not valid' do
38
+ expect(duplicate).not_to be_valid
39
+ end
40
+
41
+ context 'with a duplicate outside the conditions' do
42
+ before do
43
+ item.update(approved: false)
44
+ end
45
+
46
+ it 'is valid' do
47
+ expect(duplicate).to be_valid
48
+ end
49
+ end
50
+
51
+ context 'with a different tenant' do
52
+ it 'is valid' do
53
+ Mongoid::Multitenancy.with_tenant(another_client) do
54
+ expect(duplicate).to be_valid
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,14 @@
1
+ class ConditionalUniqueness
2
+ include Mongoid::Document
3
+ include Mongoid::Multitenancy::Document
4
+
5
+ tenant(:tenant, class_name: 'Account', optional: true)
6
+
7
+ field :slug, type: String
8
+ field :approved, type: Boolean, default: false
9
+
10
+ validates_tenant_uniqueness_of :slug, conditions: -> { where(approved: true) }
11
+ validates_presence_of :slug
12
+
13
+ index(title: 1)
14
+ end
@@ -7,7 +7,7 @@ class OptionalExclude
7
7
  field :slug, type: String
8
8
  field :title, type: String
9
9
 
10
- validates_tenant_uniqueness_of :slug, exclude_shared: true
10
+ validates_tenant_uniqueness_of :slug, exclude_shared: true, conditions: -> { ne(title: nil) }
11
11
  validates_presence_of :slug
12
12
  validates_presence_of :title
13
13
 
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: 2.0.4
4
+ version: 2.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aymeric Brisse
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-06-17 00:00:00.000000000 Z
11
+ date: 2024-09-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongoid
@@ -56,11 +56,13 @@ files:
56
56
  - lib/mongoid/multitenancy/validators/tenant_uniqueness.rb
57
57
  - lib/mongoid/multitenancy/version.rb
58
58
  - mongoid-multitenancy.gemspec
59
+ - spec/conditional_uniqueness_spec.rb
59
60
  - spec/immutable_spec.rb
60
61
  - spec/indexable_spec.rb
61
62
  - spec/inheritance_spec.rb
62
63
  - spec/mandatory_spec.rb
63
64
  - spec/models/account.rb
65
+ - spec/models/conditional_uniqueness.rb
64
66
  - spec/models/immutable.rb
65
67
  - spec/models/indexable.rb
66
68
  - spec/models/mandatory.rb
@@ -102,11 +104,13 @@ signing_key:
102
104
  specification_version: 4
103
105
  summary: Support of a multi-tenant database with Mongoid
104
106
  test_files:
107
+ - spec/conditional_uniqueness_spec.rb
105
108
  - spec/immutable_spec.rb
106
109
  - spec/indexable_spec.rb
107
110
  - spec/inheritance_spec.rb
108
111
  - spec/mandatory_spec.rb
109
112
  - spec/models/account.rb
113
+ - spec/models/conditional_uniqueness.rb
110
114
  - spec/models/immutable.rb
111
115
  - spec/models/indexable.rb
112
116
  - spec/models/mandatory.rb