active_tenant 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in active_tenant.gemspec
4
- gemspec
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in active_tenant.gemspec
4
+ gemspec
data/LICENSE CHANGED
@@ -1,22 +1,22 @@
1
- Copyright (c) 2012 gabriel
2
-
3
- MIT License
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining
6
- a copy of this software and associated documentation files (the
7
- "Software"), to deal in the Software without restriction, including
8
- without limitation the rights to use, copy, modify, merge, publish,
9
- distribute, sublicense, and/or sell copies of the Software, and to
10
- permit persons to whom the Software is furnished to do so, subject to
11
- the following conditions:
12
-
13
- The above copyright notice and this permission notice shall be
14
- included in all copies or substantial portions of the Software.
15
-
16
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
1
+ Copyright (c) 2012 gabriel
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
22
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile CHANGED
@@ -1,2 +1,2 @@
1
- #!/usr/bin/env rake
2
- require "bundler/gem_tasks"
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -15,7 +15,7 @@ Gem::Specification.new do |s|
15
15
  s.test_files = s.files.grep(%r{^(test|spec|features)/})
16
16
  s.require_paths = ["lib"]
17
17
 
18
- s.add_dependency 'activerecord', '>= 3.0.0'
18
+ s.add_dependency 'rails', '>= 3.2.0'
19
19
 
20
20
  s.add_development_dependency 'pg'
21
21
  s.add_development_dependency 'sqlite3'
@@ -1,6 +1,8 @@
1
+ require 'rails'
1
2
  require 'active_record'
2
3
 
3
4
  require 'active_tenant/version'
5
+ require 'active_tenant/engine'
4
6
  require 'active_tenant/adapters/sqlite_adapter'
5
7
  require 'active_tenant/adapters/postgres_adapter'
6
8
  require 'active_tenant/base'
@@ -18,8 +18,12 @@ module ActiveTenant
18
18
  ActiveTenant.current.with(name) { yield }
19
19
  end
20
20
 
21
+ def tenant?
22
+ !ActiveTenant.current.global?
23
+ end
24
+
21
25
  def tenant_name
22
- ActiveTenant.current.name
26
+ ActiveTenant.current.name if tenant?
23
27
  end
24
28
 
25
29
  end
@@ -47,6 +47,10 @@ module ActiveTenant
47
47
  @global || 'public'
48
48
  end
49
49
 
50
+ def global?
51
+ global == name
52
+ end
53
+
50
54
  private
51
55
 
52
56
  def global=(name)
@@ -50,6 +50,10 @@ module ActiveTenant
50
50
  @global
51
51
  end
52
52
 
53
+ def global?
54
+ global == name
55
+ end
56
+
53
57
  private
54
58
 
55
59
  def global=(name)
@@ -5,9 +5,10 @@ module ActiveTenant
5
5
  postgresql: PostgresAdapter
6
6
  }
7
7
 
8
- delegate :all, :create, :remove, :with, :name, :global, to: :adapter
8
+ delegate :all, :create, :remove, :with, :name, :global, :global?, to: :adapter
9
9
 
10
10
  def migrate(name, version=nil)
11
+ ::ActiveRecord::Base.logger.info "[ActiveTenant] Migrating tenant: #{name}"
11
12
  with name do
12
13
  ::ActiveRecord::Migrator.migrate(::ActiveRecord::Migrator.migrations_path, version) do |migration_proxy|
13
14
  [:all, ::ActiveRecord::Base.tenant_name.to_sym].include? migration_proxy.send(:migration).class.tenant
@@ -22,6 +23,7 @@ module ActiveTenant
22
23
  end
23
24
 
24
25
  def migrate_global(version=nil)
26
+ ::ActiveRecord::Base.logger.info '[ActiveTenant] Migrating global db'
25
27
  with global do
26
28
  ::ActiveRecord::Migrator.migrate(::ActiveRecord::Migrator.migrations_path, version) do |migration_proxy|
27
29
  migration_proxy.send(:migration).class.tenant.nil?
@@ -0,0 +1,4 @@
1
+ module ActiveTenant
2
+ class Engine < ::Rails::Engine
3
+ end
4
+ end
@@ -1,3 +1,3 @@
1
1
  module ActiveTenant
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
@@ -0,0 +1,21 @@
1
+ namespace :db do
2
+ namespace :migrate do
3
+
4
+ desc 'Migrate global db and all tenants'
5
+ task :all => :environment do
6
+ ActiveTenant.current.migrate_global
7
+ ActiveTenant.current.migrate_all
8
+ end
9
+
10
+ desc 'Migrate only global db'
11
+ task :global => :environment do
12
+ ActiveTenant.current.migrate_global
13
+ end
14
+
15
+ desc 'Migrate all tenants excluding global db'
16
+ task :tenants => :environment do
17
+ ActiveTenant.current.migrate_all
18
+ end
19
+
20
+ end
21
+ end
@@ -95,9 +95,9 @@ ActiveTenant::Base::ADAPTERS.keys.each do |adapter_name|
95
95
  ActiveTenant.current.migrate_global
96
96
 
97
97
  ActiveRecord::Base.connection.table_exists?('globals').should be_true
98
- ActiveRecord::Base.connection.table_exists?('tenants').should_not be_true
99
- ActiveRecord::Base.connection.table_exists?('other_tenants').should_not be_true
100
- ActiveRecord::Base.connection.table_exists?('customs').should_not be_true
98
+ ActiveRecord::Base.connection.table_exists?('tenants').should be_false
99
+ ActiveRecord::Base.connection.table_exists?('other_tenants').should be_false
100
+ ActiveRecord::Base.connection.table_exists?('customs').should be_false
101
101
 
102
102
  ActiveTenant.current.remove 'dummy'
103
103
  end
@@ -109,17 +109,17 @@ ActiveTenant::Base::ADAPTERS.keys.each do |adapter_name|
109
109
  ActiveTenant.current.migrate 'dummy_1'
110
110
 
111
111
  ActiveTenant.current.with 'dummy_1' do
112
- ActiveRecord::Base.connection.table_exists?('globals').should_not be_true
112
+ ActiveRecord::Base.connection.table_exists?('globals').should be_false
113
113
  ActiveRecord::Base.connection.table_exists?('tenants').should be_true
114
114
  ActiveRecord::Base.connection.table_exists?('other_tenants').should be_true
115
- ActiveRecord::Base.connection.table_exists?('customs').should_not be_true
115
+ ActiveRecord::Base.connection.table_exists?('customs').should be_false
116
116
  end
117
117
 
118
118
  ActiveTenant.current.with 'dummy_2' do
119
- ActiveRecord::Base.connection.table_exists?('globals').should_not be_true
120
- ActiveRecord::Base.connection.table_exists?('tenants').should_not be_true
121
- ActiveRecord::Base.connection.table_exists?('other_tenants').should_not be_true
122
- ActiveRecord::Base.connection.table_exists?('customs').should_not be_true
119
+ ActiveRecord::Base.connection.table_exists?('globals').should be_false
120
+ ActiveRecord::Base.connection.table_exists?('tenants').should be_false
121
+ ActiveRecord::Base.connection.table_exists?('other_tenants').should be_false
122
+ ActiveRecord::Base.connection.table_exists?('customs').should be_false
123
123
  end
124
124
 
125
125
  ActiveTenant.current.remove 'dummy_1'
@@ -133,17 +133,17 @@ ActiveTenant::Base::ADAPTERS.keys.each do |adapter_name|
133
133
  ActiveTenant.current.migrate_all
134
134
 
135
135
  ActiveTenant.current.with 'dummy_1' do
136
- ActiveRecord::Base.connection.table_exists?('globals').should_not be_true
136
+ ActiveRecord::Base.connection.table_exists?('globals').should be_false
137
137
  ActiveRecord::Base.connection.table_exists?('tenants').should be_true
138
138
  ActiveRecord::Base.connection.table_exists?('other_tenants').should be_true
139
- ActiveRecord::Base.connection.table_exists?('customs').should_not be_true
139
+ ActiveRecord::Base.connection.table_exists?('customs').should be_false
140
140
  end
141
141
 
142
142
  ActiveTenant.current.with 'dummy_2' do
143
- ActiveRecord::Base.connection.table_exists?('globals').should_not be_true
143
+ ActiveRecord::Base.connection.table_exists?('globals').should be_false
144
144
  ActiveRecord::Base.connection.table_exists?('tenants').should be_true
145
145
  ActiveRecord::Base.connection.table_exists?('other_tenants').should be_true
146
- ActiveRecord::Base.connection.table_exists?('customs').should_not be_true
146
+ ActiveRecord::Base.connection.table_exists?('customs').should be_false
147
147
  end
148
148
 
149
149
  ActiveTenant.current.remove 'dummy_1'
@@ -156,7 +156,7 @@ ActiveTenant::Base::ADAPTERS.keys.each do |adapter_name|
156
156
  ActiveTenant.current.migrate 'custom'
157
157
 
158
158
  ActiveTenant.current.with 'custom' do
159
- ActiveRecord::Base.connection.table_exists?('globals').should_not be_true
159
+ ActiveRecord::Base.connection.table_exists?('globals').should be_false
160
160
  ActiveRecord::Base.connection.table_exists?('tenants').should be_true
161
161
  ActiveRecord::Base.connection.table_exists?('other_tenants').should be_true
162
162
  ActiveRecord::Base.connection.table_exists?('customs').should be_true
@@ -172,7 +172,7 @@ ActiveTenant::Base::ADAPTERS.keys.each do |adapter_name|
172
172
 
173
173
  ActiveTenant.current.with 'dummy' do
174
174
  ActiveRecord::Base.connection.table_exists?('tenants').should be_true
175
- ActiveRecord::Base.connection.table_exists?('other_tenants').should_not be_true
175
+ ActiveRecord::Base.connection.table_exists?('other_tenants').should be_false
176
176
  end
177
177
 
178
178
  ActiveTenant.current.remove 'dummy'
@@ -183,23 +183,32 @@ ActiveTenant::Base::ADAPTERS.keys.each do |adapter_name|
183
183
  context 'ActiveRecord extensions' do
184
184
 
185
185
  it 'Create, migrate and remove' do
186
+ ActiveRecord::Base.tenant?.should be_false
187
+ ActiveRecord::Base.tenant_name.should be_nil
188
+
186
189
  ActiveRecord::Base.create_tenant 'dummy'
187
190
 
188
191
  ActiveRecord::Migration.migrate_all
189
192
 
190
193
  ActiveRecord::Base.connection.table_exists?('globals').should be_true
191
- ActiveRecord::Base.connection.table_exists?('tenants').should_not be_true
192
- ActiveRecord::Base.connection.table_exists?('other_tenants').should_not be_true
193
- ActiveRecord::Base.connection.table_exists?('customs').should_not be_true
194
+ ActiveRecord::Base.connection.table_exists?('tenants').should be_false
195
+ ActiveRecord::Base.connection.table_exists?('other_tenants').should be_false
196
+ ActiveRecord::Base.connection.table_exists?('customs').should be_false
194
197
 
195
198
  ActiveRecord::Base.with_tenant 'dummy' do
196
- ActiveRecord::Base.connection.table_exists?('globals').should_not be_true
199
+ ActiveRecord::Base.tenant?.should be_true
200
+ ActiveRecord::Base.tenant_name.should eq 'dummy'
201
+
202
+ ActiveRecord::Base.connection.table_exists?('globals').should be_false
197
203
  ActiveRecord::Base.connection.table_exists?('tenants').should be_true
198
204
  ActiveRecord::Base.connection.table_exists?('other_tenants').should be_true
199
- ActiveRecord::Base.connection.table_exists?('customs').should_not be_true
205
+ ActiveRecord::Base.connection.table_exists?('customs').should be_false
200
206
  end
201
207
 
202
208
  ActiveRecord::Base.remove_tenant 'dummy'
209
+
210
+ ActiveRecord::Base.tenant?.should be_false
211
+ ActiveRecord::Base.tenant_name.should be_nil
203
212
  end
204
213
 
205
214
  end
@@ -4,6 +4,7 @@ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
4
4
 
5
5
  TEMP_PATH = ENV['TMP'].gsub("\\", '/')
6
6
 
7
+ ActiveRecord::Base.logger = Logger.new($stdout)
7
8
  ActiveRecord::Migrator.migrations_path = "#{File.dirname(__FILE__)}/migrations"
8
9
 
9
10
  RSpec.configure do |config|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_tenant
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,22 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-04 00:00:00.000000000 Z
12
+ date: 2012-10-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: activerecord
16
- requirement: &25487856 !ruby/object:Gem::Requirement
15
+ name: rails
16
+ requirement: &27972744 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
- version: 3.0.0
21
+ version: 3.2.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *25487856
24
+ version_requirements: *27972744
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: pg
27
- requirement: &25463208 !ruby/object:Gem::Requirement
27
+ requirement: &27972492 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *25463208
35
+ version_requirements: *27972492
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: sqlite3
38
- requirement: &25414248 !ruby/object:Gem::Requirement
38
+ requirement: &27972216 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *25414248
46
+ version_requirements: *27972216
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rspec
49
- requirement: &25336344 !ruby/object:Gem::Requirement
49
+ requirement: &27971964 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *25336344
57
+ version_requirements: *27971964
58
58
  description: ActiveRecord extensions for multi tenant applications
59
59
  email:
60
60
  - gabynaiman@gmail.com
@@ -74,7 +74,9 @@ files:
74
74
  - lib/active_tenant/adapters/sqlite_adapter.rb
75
75
  - lib/active_tenant/base.rb
76
76
  - lib/active_tenant/configuration.rb
77
+ - lib/active_tenant/engine.rb
77
78
  - lib/active_tenant/version.rb
79
+ - lib/tasks/migration.rake
78
80
  - spec/adapters_spec.rb
79
81
  - spec/migrations/20120823132512_create_globals.rb
80
82
  - spec/migrations/20120823132854_create_tenants.rb