mongoid-multitenancy 0.4.3 → 0.4.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/mongoid/multitenancy/document.rb +23 -10
- data/lib/mongoid/multitenancy/version.rb +1 -1
- data/spec/mandatory_spec.rb +2 -1
- data/spec/models/mutable.rb +1 -1
- data/spec/optional_spec.rb +2 -1
- data/spec/spec_helper.rb +1 -2
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7a20d8d26c776318f1eb6407d0dc19017a721cdb
|
4
|
+
data.tar.gz: 1404d16b8b568ebf9a7744ce7f6fa5c5338fa91b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bed65acf48172bfaac61012ebdb5bc67bb73e96500835273e7ef45743cc7e77bddd5a32ed4b2ed52f11441e735f484311d1a192047a2b594c11b07e4ba4e1dce
|
7
|
+
data.tar.gz: 1271fa93a141b513d1d5587b6d4bd7ce4c0b294635d243292f35cc62cfe60349abe78b499ba689bc7913868746cb1ee6a91400d4a5ecf7bb0e1a691bf196db5c
|
@@ -4,7 +4,7 @@ module Mongoid
|
|
4
4
|
extend ActiveSupport::Concern
|
5
5
|
|
6
6
|
module ClassMethods
|
7
|
-
attr_accessor :tenant_field, :
|
7
|
+
attr_accessor :tenant_field, :tenant_options
|
8
8
|
|
9
9
|
MULTITENANCY_OPTIONS = [:optional, :immutable, :full_indexes, :index]
|
10
10
|
|
@@ -16,7 +16,7 @@ module Mongoid
|
|
16
16
|
|
17
17
|
# Get the tenant model and its foreign key
|
18
18
|
self.tenant_field = reflect_on_association(association).foreign_key
|
19
|
-
self.
|
19
|
+
self.tenant_options = extract_tenant_options(options)
|
20
20
|
|
21
21
|
# Validates the tenant field
|
22
22
|
validates tenant_field, tenant: options
|
@@ -46,21 +46,24 @@ module Mongoid
|
|
46
46
|
|
47
47
|
# Redefine 'validates_with' to add the tenant scope when using a UniquenessValidator
|
48
48
|
def validates_with(*args, &block)
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
49
|
+
if !self.tenant_options[:optional]
|
50
|
+
validator = if Mongoid::Multitenancy.mongoid4?
|
51
|
+
Validatable::UniquenessValidator
|
52
|
+
else
|
53
|
+
Validations::UniquenessValidator
|
54
|
+
end
|
54
55
|
|
55
|
-
|
56
|
-
|
56
|
+
if args.first.ancestors.include?(validator)
|
57
|
+
args.last[:scope] = Array(args.last[:scope]) << self.tenant_field
|
58
|
+
end
|
57
59
|
end
|
60
|
+
|
58
61
|
super(*args, &block)
|
59
62
|
end
|
60
63
|
|
61
64
|
# Redefine 'index' to include the tenant field in first position
|
62
65
|
def index(spec, options = nil)
|
63
|
-
spec = { self.tenant_field => 1 }.merge(spec) if self.full_indexes
|
66
|
+
spec = { self.tenant_field => 1 }.merge(spec) if self.tenant_options[:full_indexes]
|
64
67
|
super(spec, options)
|
65
68
|
end
|
66
69
|
|
@@ -80,6 +83,16 @@ module Mongoid
|
|
80
83
|
|
81
84
|
new_options
|
82
85
|
end
|
86
|
+
|
87
|
+
def extract_tenant_options(options)
|
88
|
+
new_options = {}
|
89
|
+
|
90
|
+
options.each do |k, v|
|
91
|
+
new_options[k] = v if MULTITENANCY_OPTIONS.include?(k)
|
92
|
+
end
|
93
|
+
|
94
|
+
new_options
|
95
|
+
end
|
83
96
|
end
|
84
97
|
end
|
85
98
|
end
|
data/spec/mandatory_spec.rb
CHANGED
@@ -3,6 +3,7 @@ require 'spec_helper'
|
|
3
3
|
describe Mandatory do
|
4
4
|
|
5
5
|
it_behaves_like "a tenantable model"
|
6
|
+
it { should validate_uniqueness_of(:slug).scoped_to(:client_id) }
|
6
7
|
|
7
8
|
let(:client) { Account.create!(:name => "client") }
|
8
9
|
let(:another_client) { Account.create!(:name => "another client") }
|
@@ -79,4 +80,4 @@ describe Mandatory do
|
|
79
80
|
end
|
80
81
|
end
|
81
82
|
end
|
82
|
-
end
|
83
|
+
end
|
data/spec/models/mutable.rb
CHANGED
@@ -2,7 +2,7 @@ class Mutable
|
|
2
2
|
include Mongoid::Document
|
3
3
|
include Mongoid::Multitenancy::Document
|
4
4
|
|
5
|
-
tenant :client, :class_name => 'Account', :immutable => false, :optional =>
|
5
|
+
tenant :client, :class_name => 'Account', :immutable => false, :optional => false
|
6
6
|
|
7
7
|
field :slug, :type => String
|
8
8
|
field :title, :type => String
|
data/spec/optional_spec.rb
CHANGED
@@ -3,6 +3,7 @@ require 'spec_helper'
|
|
3
3
|
describe Optional do
|
4
4
|
|
5
5
|
it_behaves_like "a tenantable model"
|
6
|
+
it { should validate_uniqueness_of(:slug) }
|
6
7
|
|
7
8
|
let(:client) { Account.create!(:name => "client") }
|
8
9
|
let(:another_client) { Account.create!(:name => "another client") }
|
@@ -81,4 +82,4 @@ describe Optional do
|
|
81
82
|
end
|
82
83
|
end
|
83
84
|
end
|
84
|
-
end
|
85
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -46,7 +46,6 @@ end
|
|
46
46
|
shared_examples_for "a tenantable model" do
|
47
47
|
|
48
48
|
it { should belong_to(:client) }
|
49
|
-
it { should validate_uniqueness_of(:slug).scoped_to(:client_id) }
|
50
49
|
it { should have_index_for(:client_id => 1, :title => 1) }
|
51
50
|
|
52
51
|
end
|
@@ -91,4 +90,4 @@ shared_examples_for "a tenant validator" do
|
|
91
90
|
end
|
92
91
|
end
|
93
92
|
end
|
94
|
-
end
|
93
|
+
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.
|
4
|
+
version: 0.4.4
|
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-
|
11
|
+
date: 2015-07-17 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.4.
|
86
|
+
rubygems_version: 2.4.6
|
87
87
|
signing_key:
|
88
88
|
specification_version: 4
|
89
89
|
summary: Support of a multi-tenant database with Mongoid
|