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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: facc15f874a27e6541f1d7963370a4c372c863e6
4
- data.tar.gz: 686e5f4eca2e9c6e9b6245406aa0904bf33c7ee2
3
+ metadata.gz: 7a20d8d26c776318f1eb6407d0dc19017a721cdb
4
+ data.tar.gz: 1404d16b8b568ebf9a7744ce7f6fa5c5338fa91b
5
5
  SHA512:
6
- metadata.gz: e7ff42bb09808638b9b10ca05d302d3f3c7b5974ef6aedd0a7debecdccbbb557889ac916b5b50727bd556930bb5299a317d52ba7742f4d7ebc24fe558c7329d3
7
- data.tar.gz: d6df90fd199a0420952c244148e7ab7f10c60ab157d675adb76c577d5170395a29225d3fe9ee8dab6833d0b51a2d3b0c37253b59d2bec7705375f107d1ba19f8
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, :full_indexes
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.full_indexes = options[:full_indexes]
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
- validator = if Mongoid::Multitenancy.mongoid4?
50
- Validatable::UniquenessValidator
51
- else
52
- Validations::UniquenessValidator
53
- end
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
- if args.first.ancestors.include?(validator)
56
- args.last[:scope] = Array(args.last[:scope]) << self.tenant_field
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
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module Multitenancy
3
- VERSION = "0.4.3"
3
+ VERSION = "0.4.4"
4
4
  end
5
5
  end
@@ -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
@@ -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 => true
5
+ tenant :client, :class_name => 'Account', :immutable => false, :optional => false
6
6
 
7
7
  field :slug, :type => String
8
8
  field :title, :type => String
@@ -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.3
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-04-29 00:00:00.000000000 Z
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.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