mongoid-tenant 0.0.7 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 13200e42649305b163500529ef5cf49d9f72b6af
4
- data.tar.gz: 3d7df78088d6668b12b1e1f371ff2dd283c2f1fe
3
+ metadata.gz: 05977824630fa2458706fd4f9df02c28b770884d
4
+ data.tar.gz: 6f8976a2cf9c0debc07841b29c6cf71d7be1c529
5
5
  SHA512:
6
- metadata.gz: 890d1639ca4a5b59fe6a49c72ba53942c223c25f766ad84d2ffc1bd15f40a42bb8127810c5af0f6ce69b1bc48efa0f29f5ade830e302ae3c25af2e5ee4a31b6e
7
- data.tar.gz: b3784c0a6748cc5380bec1bafab4cd1f317f3d92193d732bc3c5fd1852223a579781a6d2471f8d23a344414e9d4a617d660e9b406d7454ccea4b89fc857a8f2f
6
+ metadata.gz: 949f8b62dbb9c8da303e0f99f59bd54475dd769d855a4f3eccb418bcc0c97cbbdbf10fb2a7873728e42f40a76e4d8cce7a9ccff9f92b4f9c1c5573eb0f31ba23
7
+ data.tar.gz: bb5bb3faf2c946d3ed02578d21210eaf8a1665f1950eb4abb417260ac3c21a34907b5cf616aeb43c2dba0f0b938c171481d49d99ddd0684ae470e289e20b5485
data/README.md CHANGED
@@ -4,6 +4,8 @@ Mongoid::Tenant
4
4
  [![Gem Version](https://badge.fury.io/rb/mongoid-tenant.svg)](http://badge.fury.io/rb/mongoid-tenant)
5
5
  [![Dependency Status](https://gemnasium.com/nofxx/mongoid-tenant.svg)](https://gemnasium.com/nofxx/mongoid-tenant)
6
6
  [![Build Status](https://secure.travis-ci.org/nofxx/mongoid-tenant.svg)](http://travis-ci.org/nofxx/mongoid-tenant)
7
+ [![Code Climate](https://codeclimate.com/github/nofxx/mongoid-tenant/badges/gpa.svg)](https://codeclimate.com/github/nofxx/mongoid-tenant)
8
+ [![Coverage Status](https://coveralls.io/repos/nofxx/mongoid-tenant/badge.svg?branch=master&service=github)](https://coveralls.io/github/nofxx/mongoid-tenant?branch=master)
7
9
 
8
10
  ## Mongoid::Tenant
9
11
 
@@ -15,31 +17,31 @@ Simple multi tenant for mongoid models!
15
17
  This library is a simple way to split your client data between databases.
16
18
  To mongo it's pretty trivial to use multiple databases. Mongoid helps too.
17
19
 
18
- ## Tenant
20
+ Supposing you have a Bike SaaS where each Shop stores:
19
21
 
20
- Models to be splitted, supposing you have a Bike SaaS where each Shop stores
21
- their bikes:
22
+ ## Tenancy
22
23
 
23
24
  ```
24
- class Bike
25
+ class Shop
25
26
  include Mongoid::Document
26
- include Mongoid::Tenant
27
+ include Mongoid::Tenancy
28
+
29
+ tenant_key :url
27
30
  end
28
31
  ```
29
32
 
30
- And that's all. But we need a tenancy:
33
+ ## Tenant
31
34
 
32
- ## Tenancy
33
35
 
34
36
  ```
35
- class Shop
37
+ class Bike
36
38
  include Mongoid::Document
37
- include Mongoid::Tenancy
38
-
39
- tenant_key :url
39
+ include Mongoid::Tenant
40
40
  end
41
41
  ```
42
42
 
43
+ And that's all.
44
+
43
45
 
44
46
  ## Helpers
45
47
 
@@ -93,8 +95,18 @@ def app_domain
93
95
  end
94
96
  ```
95
97
 
98
+ ## Populate & Spec
96
99
 
97
- ## Issues
100
+ `Mongoid.purge!` won't work. It doesn't know all dbs.
101
+ A good alternative:
98
102
 
103
+ ```
104
+ Mongoid.default_client.with(database: db).collections.each(&:drop)
105
+ ```
106
+
107
+ If you don't know every db try `Mongoid.default_client.list_databases`.
108
+
109
+
110
+ ## Issues
99
111
 
100
112
  http://github.com/nofxx/mongoid-tenant
@@ -10,13 +10,14 @@ module Mongoid
10
10
  # Model instance
11
11
  module ClassMethods
12
12
  def tenant_key(key, options = {})
13
- field key, type: Symbol
13
+ field key, type: Symbol
14
+
14
15
  options[:validates] ||= { presence: true }
15
16
  options[:index] ||= {}
16
17
 
17
18
  validates key, { uniqueness: true }.merge(options[:validates])
18
19
 
19
- index({ key => 1 }, { unique: true }.merge(options[:index]))
20
+ index({ key => 1 }, { unique: true }.merge(options[:index]))
20
21
 
21
22
  define_method(:tenant_key) do
22
23
  send(key).to_s
@@ -4,26 +4,35 @@ Rake::Task['db:mongoid:remove_undefined_indexes'].clear
4
4
  namespace :db do
5
5
  namespace :mongoid do
6
6
  def tenancy_env
7
- ENV['TENANCY'] ||
8
- fail("Provide a tenancy model: `TENANCY=Foo #{ARGV.join}`")
7
+ ENV['TENANCY'] && !ENV['TENANCY'].empty? ? ENV['TENANCY'] : nil
9
8
  end
10
9
 
11
10
  desc 'Create Mongoid indexes, tenant aware'
12
11
  task create_indexes: [:environment, :load_models] do
13
- Rake::Task['db:mongoid:create_indexes'].clear
14
- Object.const_get(tenancy_env).all.each do |t|
15
- puts "Tenant #{t}"
16
- t.tenancy!
17
- ::Mongoid::Tasks::Database.create_indexes
12
+ # Run once, for tables outside tenancy
13
+ ::Mongoid::Tasks::Database.create_indexes
14
+ if tenancy_env
15
+ Object.const_get(tenancy_env).all.each do |t|
16
+ puts "Tenant #{t}"
17
+ t.tenancy!
18
+ ::Mongoid::Tasks::Database.create_indexes
19
+ end
20
+ else
21
+ puts 'Not running tenants: Provide a tenancy model:'
22
+ puts "`TENANCY=Firm #{ARGV.join(' ')}`"
18
23
  end
19
24
  end
20
25
 
21
26
  desc 'Removes undefined Mongoid indexes, tenant aware'
22
27
  task remove_undefined_indexes: [:environment, :load_models] do
23
- Object.const_get(tenancy_env).all.each do |t|
24
- puts "Tenant #{t}"
25
- t.tenancy!
26
- ::Mongoid::Tasks::Database.remove_undefined_indexes
28
+ # Run once, for tables outside tenancy
29
+ ::Mongoid::Tasks::Database.remove_undefined_indexes
30
+ if tenancy_env
31
+ Object.const_get(tenancy_env).all.each do |t|
32
+ puts "Tenant #{t}"
33
+ t.tenancy!
34
+ ::Mongoid::Tasks::Database.remove_undefined_indexes
35
+ end
27
36
  end
28
37
  end
29
38
  end
@@ -1,6 +1,6 @@
1
1
  module Mongoid
2
2
  # Mongoid::Tenant::VERSION
3
3
  module Tenant
4
- VERSION = '0.0.7'
4
+ VERSION = '0.0.8'.freeze
5
5
  end
6
6
  end
@@ -11,7 +11,13 @@ module Mongoid
11
11
  extend ActiveSupport::Concern
12
12
 
13
13
  included do
14
- store_in database: -> { Thread.current[:tenancy] }
14
+ store_in database: lambda do
15
+ Thread.current[:tenancy] || raise('No tenancy set!')
16
+ end
17
+
18
+ def tenancy
19
+ Thread.current[:tenancy]
20
+ end
15
21
  end
16
22
  end # Tenant
17
23
  end # Mongoid
data/spec/spec_helper.rb CHANGED
@@ -69,8 +69,8 @@ RSpec.configure do |config|
69
69
  end
70
70
 
71
71
  config.after(:suite) do
72
- extra_dbs = fetch_dbs - START_DBS
73
- fail "Extra DBs: #{extra_dbs.inspect}" unless extra_dbs.empty?
74
72
  puts "\n# With Mongoid v#{Mongoid::VERSION}"
73
+ extra_dbs = (fetch_dbs - START_DBS) - ['mongoid_tenant_test']
74
+ fail "Extra DBs: #{extra_dbs.inspect}" unless extra_dbs.empty?
75
75
  end
76
76
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-tenant
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcos Piccinini
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-23 00:00:00.000000000 Z
11
+ date: 2016-06-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongoid
@@ -69,7 +69,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
69
69
  version: '0'
70
70
  requirements: []
71
71
  rubyforge_project: mongoid-tenant
72
- rubygems_version: 2.4.8
72
+ rubygems_version: 2.5.1
73
73
  signing_key:
74
74
  specification_version: 4
75
75
  summary: Multiple databases Models for Mongoid documents.