Skip to content

Commit

Permalink
Merge branch 'mongoid-6'
Browse files Browse the repository at this point in the history
  • Loading branch information
abrisse committed Jul 21, 2017
2 parents 7ee14e8 + fc0d4d6 commit 5440eb0
Show file tree
Hide file tree
Showing 23 changed files with 59 additions and 96 deletions.
8 changes: 2 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
language: ruby
rvm:
- 2.0.0
- 2.1.0
- 2.2.0
- 2.2.2
- 2.3.0
- jruby
- 2.4.1
gemfile:
- gemfiles/Gemfile.mongoid-4.0
- gemfiles/Gemfile.mongoid-5.0
- Gemfile
services:
- mongodb
Expand Down
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source 'https://rubygems.org'

gem 'mongoid', '~> 5.1'
gem 'mongoid', '~> 6.0'

gem 'rake', '~> 11.0'

Expand All @@ -9,7 +9,7 @@ group :test do
gem 'coveralls', require: false
gem 'rspec', '~> 3.1'
gem 'yard', '~> 0.8'
gem 'mongoid-rspec', '~> 3.0'
gem 'mongoid-rspec', git: 'https://github.com/mongoid-rspec/mongoid-rspec.git'
gem 'rubocop', require: false
end

Expand Down
43 changes: 24 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@ In addition, mongoid-multitenancy:
* is thread safe
* redefines some mongoid functions like `index`, `validates_with` and `delete_all` to take in account the multitenancy.

Compatibility
===============

mongoid-multitenancy 2.0 is only compatible with mongoid 6. For mongoid 4 & 5 compatiblity, use mongoid-multitenancy 1.2.

Installation
===============

Add this line to your application's Gemfile:

gem 'mongoid-multitenancy'
gem 'mongoid-multitenancy', '~> 2.0'

And then execute:

Expand Down Expand Up @@ -74,7 +79,7 @@ class Article
include Mongoid::Document
include Mongoid::Multitenancy::Document

tenant(:client)
tenant(:tenant)

field :title, :type => String
end
Expand All @@ -101,15 +106,15 @@ Some examples to illustrate this behavior:
Mongoid::Multitenancy.current_tenant = Client.find_by(:name => 'Perfect Memory') # => <#Client _id:50ca04b86c82bfc125000025, :name: "Perfect Memory">

# All searches are scoped by the tenant, the following searches will only return objects belonging to the current client.
Article.all # => all articles where client_id => 50ca04b86c82bfc125000025
Article.all # => all articles where tenant_id => 50ca04b86c82bfc125000025

# New objects are scoped to the current tenant
article = Article.new(:title => 'New blog')
article.save # => <#Article _id: 50ca04b86c82bfc125000044, title: 'New blog', client_id: 50ca04b86c82bfc125000025>
article.save # => <#Article _id: 50ca04b86c82bfc125000044, title: 'New blog', tenant_id: 50ca04b86c82bfc125000025>

# It can make the tenant field immutable once it is persisted to avoid inconsistency
article.persisted? # => true
article.client = another_client
article.tenant = another_client
article.valid? # => false
```

Expand All @@ -124,21 +129,21 @@ class Article
include Mongoid::Document
include Mongoid::Multitenancy::Document

tenant(:client, optional: true)
tenant(:tenant, optional: true)

field :title, :type => String
end

Mongoid::Multitenancy.with_tenant(client_instance) do
Article.all # => all articles where client_id.in [50ca04b86c82bfc125000025, nil]
Article.all # => all articles where tenant_id.in [50ca04b86c82bfc125000025, nil]
article = Article.new(:title => 'New article')
article.save # => <#Article _id: 50ca04b86c82bfc125000044, title: 'New blog', client_id: 50ca04b86c82bfc125000025>
article.save # => <#Article _id: 50ca04b86c82bfc125000044, title: 'New blog', tenant_id: 50ca04b86c82bfc125000025>

# tenant needs to be set manually to nil
article = Article.new(:title => 'New article', :client => nil)
article.save # => <#Article _id: 50ca04b86c82bfc125000044, title: 'New blog', client_id: 50ca04b86c82bfc125000025>
article = Article.new(:title => 'New article', :tenant => nil)
article.save # => <#Article _id: 50ca04b86c82bfc125000044, title: 'New blog', tenant_id: 50ca04b86c82bfc125000025>
article.tenant = nil
article.save => <#Article _id: 50ca04b86c82bfc125000044, title: 'New blog', client_id: nil>
article.save => <#Article _id: 50ca04b86c82bfc125000044, title: 'New blog', tenant_id: nil>
end
```

Expand Down Expand Up @@ -177,7 +182,7 @@ class Article
include Mongoid::Document
include Mongoid::Multitenancy::Document

tenant :client
tenant :tenant

field :slug

Expand All @@ -197,7 +202,7 @@ class Article
include Mongoid::Document
include Mongoid::Multitenancy::Document

tenant :client, optional: true
tenant :tenant, optional: true

field :slug

Expand All @@ -206,14 +211,14 @@ end
```

In the following case, 2 private articles can have the same slug if they belongs to 2 different clients even if a shared
article already uses that same slug, like a `validates_uniqueness_of scope: :client` does.
article already uses that same slug, like a `validates_uniqueness_of scope: :tenant` does.

```ruby
class Article
include Mongoid::Document
include Mongoid::Multitenancy::Document

tenant :client, optional: true
tenant :tenant, optional: true

field :slug

Expand All @@ -230,14 +235,14 @@ To create a single index on the tenant field, you can use the option `index: tru

On the example below, only one indexe will be created:

* { 'title_id' => 1, 'client_id' => 1 }
* { 'title_id' => 1, 'tenant_id' => 1 }

```ruby
class Article
include Mongoid::Document
include Mongoid::Multitenancy::Document

tenant :client, full_indexes: true
tenant :tenant, full_indexes: true

field :title

Expand All @@ -247,15 +252,15 @@ end

On the example below, 2 indexes will be created:

* { 'client_id' => 1 }
* { 'tenant_id' => 1 }
* { 'title_id' => 1 }

```ruby
class Article
include Mongoid::Document
include Mongoid::Multitenancy::Document

tenant :client, index: true
tenant :tenant, index: true

field :title

Expand Down
17 changes: 0 additions & 17 deletions gemfiles/Gemfile.mongoid-4.0

This file was deleted.

17 changes: 0 additions & 17 deletions gemfiles/Gemfile.mongoid-5.0

This file was deleted.

1 change: 1 addition & 0 deletions lib/mongoid/multitenancy/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def build_options(options)
options.each do |k, v|
if MULTITENANCY_OPTIONS.include?(k)
multitenant_options[k] = v
assoc_options[k] = v if k == :optional
else
assoc_options[k] = v
end
Expand Down
10 changes: 2 additions & 8 deletions lib/mongoid/multitenancy/validators/tenant_uniqueness.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,8 @@ def validate_root(document, attribute, value)
criteria = with_tenant_criterion(criteria, klass, document)
criteria = criteria.merge(options[:conditions].call) if options[:conditions]

if Mongoid::VERSION.start_with?('4')
if criteria.with(persistence_options(criteria)).exists?
add_error(document, attribute, value)
end
else
if criteria.with(criteria.persistence_options).read(mode: :primary).exists?
add_error(document, attribute, value)
end
if criteria.read(mode: :primary).exists?
add_error(document, attribute, value)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/multitenancy/version.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Mongoid
module Multitenancy
# Version
VERSION = '1.2.0'.freeze
VERSION = '2.0'.freeze
end
end
2 changes: 1 addition & 1 deletion mongoid-multitenancy.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ Gem::Specification.new do |gem|
gem.require_paths = ['lib']
gem.version = Mongoid::Multitenancy::VERSION

gem.add_dependency('mongoid', '>= 4.0')
gem.add_dependency('mongoid', '~> 6.0')
end
2 changes: 1 addition & 1 deletion spec/immutable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
end

it 'is not valid' do
item.client = another_client
item.tenant = another_client
expect(item).not_to be_valid
end
end
Expand Down
8 changes: 4 additions & 4 deletions spec/indexable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@

context 'without index: true' do
it 'does not create an index' do
expect(Immutable).not_to have_index_for(client_id: 1)
expect(Immutable).not_to have_index_for(tenant_id: 1)
end
end

context 'with index: true' do
it 'creates an index' do
expect(Indexable).to have_index_for(client_id: 1)
expect(Indexable).to have_index_for(tenant_id: 1)
end
end

context 'with full_indexes: true' do
it 'add the tenant field on each index' do
expect(Immutable).to have_index_for(client_id: 1, title: 1)
expect(Immutable).to have_index_for(tenant_id: 1, title: 1)
end
end

context 'with full_indexes: false' do
it 'does not add the tenant field on each index' do
expect(Indexable).not_to have_index_for(client_id: 1, title: 1)
expect(Indexable).not_to have_index_for(tenant_id: 1, title: 1)
end
end
end
2 changes: 1 addition & 1 deletion spec/models/immutable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Immutable
include Mongoid::Document
include Mongoid::Multitenancy::Document

tenant(:client, class_name: 'Account', immutable: true)
tenant(:tenant, class_name: 'Account', immutable: true)

field :slug, type: String
field :title, type: String
Expand Down
2 changes: 1 addition & 1 deletion spec/models/indexable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Indexable

field :title, type: String

tenant :client, class_name: 'Account', index: true, full_indexes: false
tenant :tenant, class_name: 'Account', index: true, full_indexes: false

index(title: 1)
end
2 changes: 1 addition & 1 deletion spec/models/mandatory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Mandatory
include Mongoid::Document
include Mongoid::Multitenancy::Document

tenant(:client, class_name: 'Account')
tenant(:tenant, class_name: 'Account')

field :slug, type: String
field :title, type: String
Expand Down
2 changes: 1 addition & 1 deletion spec/models/mutable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Mutable
include Mongoid::Document
include Mongoid::Multitenancy::Document

tenant :client, class_name: 'Account', immutable: false, optional: false
tenant :tenant, class_name: 'Account', immutable: false, optional: false

field :slug, type: String
field :title, type: String
Expand Down
2 changes: 1 addition & 1 deletion spec/models/no_scopable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class NoScopable
include Mongoid::Document
include Mongoid::Multitenancy::Document

tenant :client, class_name: 'Account', scopes: false
tenant :tenant, class_name: 'Account', scopes: false

field :slug, type: String
field :title, type: String
Expand Down
2 changes: 1 addition & 1 deletion spec/models/optional.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Optional
include Mongoid::Document
include Mongoid::Multitenancy::Document

tenant(:client, class_name: 'Account', optional: true)
tenant(:tenant, class_name: 'Account', optional: true)

field :slug, type: String
field :title, type: String
Expand Down
2 changes: 1 addition & 1 deletion spec/models/optional_exclude.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class OptionalExclude
include Mongoid::Document
include Mongoid::Multitenancy::Document

tenant(:client, class_name: 'Account', optional: true)
tenant(:tenant, class_name: 'Account', optional: true)

field :slug, type: String
field :title, type: String
Expand Down
2 changes: 1 addition & 1 deletion spec/mutable_child_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
end

it 'is valid' do
item.client = another_client
item.tenant = another_client
expect(item).to be_valid
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/mutable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
end

it 'is valid' do
item.client = another_client
item.tenant = another_client
expect(item).to be_valid
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/optional_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@

context 'when persisted' do
before do
item.client = nil
item.tenant = nil
item.save!
end

it 'does not override the client' do
item.reload
expect(Optional.last.client).to be_nil
expect(Optional.last.tenant).to be_nil
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
config.connect_to 'mongoid_multitenancy'
end

Mongoid.logger = nil
Mongoid.logger.level = Logger::INFO
Mongo::Logger.logger.level = Logger::INFO

RSpec.configure do |config|
config.include Mongoid::Matchers
Expand Down

0 comments on commit 5440eb0

Please sign in to comment.