active_tenant 0.0.2 → 0.0.3

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.
data/.gitignore CHANGED
@@ -1,18 +1,18 @@
1
- *.gem
2
- *.rbc
3
- .bundle
4
- .config
5
- .yardoc
6
- Gemfile.lock
7
- InstalledFiles
8
- _yardoc
9
- coverage
10
- doc/
11
- lib/bundler/man
12
- pkg
13
- rdoc
14
- spec/reports
15
- test/tmp
16
- test/version_tmp
17
- tmp
18
- .idea
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .idea
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
5
+ script: bundle exec rspec spec
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/README.md CHANGED
@@ -1,39 +1,41 @@
1
- # ActiveTenant
2
-
3
- ActiveRecord extensions for multi tenant applications
4
-
5
- ## Installation
6
-
7
- Add this line to your application's Gemfile:
8
-
9
- gem 'active_tenant'
10
-
11
- And then execute:
12
-
13
- $ bundle
14
-
15
- Or install it yourself as:
16
-
17
- $ gem install active_tenant
18
-
19
- ## Usage
20
-
21
- ActiveRecord::Base.create_tenant 'customer_name'
22
-
23
- ActiveRecord::Migration.migrate_tenant 'customer_name'
24
- # or
25
- ActiveRecord::Migration.migrate_all_tenants
26
-
27
- ActiveRecord::Base.with_tenant 'customer_name' do
28
- Porduct.find(1234)
29
- end
30
-
31
- ActiveRecord::Base.remove_tenant 'customer_name'
32
-
33
- ## Contributing
34
-
35
- 1. Fork it
36
- 2. Create your feature branch (`git checkout -b my-new-feature`)
37
- 3. Commit your changes (`git commit -am 'Added some feature'`)
38
- 4. Push to the branch (`git push origin my-new-feature`)
39
- 5. Create new Pull Request
1
+ # ActiveTenant
2
+
3
+ [![Build Status](https://travis-ci.org/gabynaiman/active_tenant.png?branch=master)](https://travis-ci.org/gabynaiman/active_tenant)
4
+
5
+ ActiveRecord extensions for multi tenant applications
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'active_tenant'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install active_tenant
20
+
21
+ ## Usage
22
+
23
+ ActiveRecord::Base.create_tenant 'customer_name'
24
+
25
+ ActiveRecord::Migration.migrate_tenant 'customer_name'
26
+ # or
27
+ ActiveRecord::Migration.migrate_all_tenants
28
+
29
+ ActiveRecord::Base.with_tenant 'customer_name' do
30
+ Porduct.find(1234)
31
+ end
32
+
33
+ ActiveRecord::Base.remove_tenant 'customer_name'
34
+
35
+ ## Contributing
36
+
37
+ 1. Fork it
38
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
39
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
40
+ 4. Push to the branch (`git push origin my-new-feature`)
41
+ 5. Create new Pull Request
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"
@@ -1,23 +1,23 @@
1
- # -*- encoding: utf-8 -*-
2
- require File.expand_path('../lib/active_tenant/version', __FILE__)
3
-
4
- Gem::Specification.new do |s|
5
- s.name = 'active_tenant'
6
- s.version = ActiveTenant::VERSION
7
- s.authors = ['Gabriel Naiman']
8
- s.email = ['gabynaiman@gmail.com']
9
- s.description = 'ActiveRecord extensions for multi tenant applications'
10
- s.summary = 'ActiveRecord extensions for multi tenant applications'
11
- s.homepage = 'https://github.com/gabynaiman/active_tenant'
12
-
13
- s.files = `git ls-files`.split($\)
14
- s.executables = s.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
15
- s.test_files = s.files.grep(%r{^(test|spec|features)/})
16
- s.require_paths = ["lib"]
17
-
18
- s.add_dependency 'rails', '>= 3.2.0'
19
-
20
- s.add_development_dependency 'pg'
21
- s.add_development_dependency 'sqlite3'
22
- s.add_development_dependency 'rspec'
23
- end
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/active_tenant/version', __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'active_tenant'
6
+ s.version = ActiveTenant::VERSION
7
+ s.authors = ['Gabriel Naiman']
8
+ s.email = ['gabynaiman@gmail.com']
9
+ s.description = 'ActiveRecord extensions for multi tenant applications'
10
+ s.summary = 'ActiveRecord extensions for multi tenant applications'
11
+ s.homepage = 'https://github.com/gabynaiman/active_tenant'
12
+
13
+ s.files = `git ls-files`.split($\)
14
+ s.executables = s.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
15
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
16
+ s.require_paths = ["lib"]
17
+
18
+ s.add_dependency 'rails', '>= 3.2.0'
19
+
20
+ s.add_development_dependency 'pg'
21
+ s.add_development_dependency 'sqlite3'
22
+ s.add_development_dependency 'rspec'
23
+ end
@@ -1,56 +1,60 @@
1
- module ActiveTenant
2
- module ActiveRecord
3
- module Base
4
-
5
- def all_tenants
6
- ActiveTenant.current.all
7
- end
8
-
9
- def create_tenant(name)
10
- ActiveTenant.current.create name
11
- end
12
-
13
- def remove_tenant(name)
14
- ActiveTenant.current.remove name
15
- end
16
-
17
- def with_tenant(name)
18
- ActiveTenant.current.with(name) { yield }
19
- end
20
-
21
- def tenant?
22
- !ActiveTenant.current.global?
23
- end
24
-
25
- def tenant_name
26
- ActiveTenant.current.name if tenant?
27
- end
28
-
29
- end
30
-
31
- module Migration
32
-
33
- def tenant(name=nil)
34
- name ? @tenant_name = name : @tenant_name
35
- end
36
-
37
- def migrate_global(version=nil)
38
- ActiveTenant.current.migrate_global version
39
- end
40
-
41
- def migrate_tenant(name, version=nil)
42
- ActiveTenant.current.migrate name, version
43
- end
44
-
45
- def migrate_all_tenants(version=nil)
46
- ActiveTenant.current.migrate_all version
47
- end
48
-
49
- def migrate_all(version=nil)
50
- migrate_global version
51
- migrate_all_tenants version
52
- end
53
-
54
- end
55
- end
1
+ module ActiveTenant
2
+ module ActiveRecord
3
+ module Base
4
+
5
+ def all_tenants
6
+ ActiveTenant.current.all
7
+ end
8
+
9
+ def create_tenant(name)
10
+ ActiveTenant.current.create name
11
+ end
12
+
13
+ def remove_tenant(name)
14
+ ActiveTenant.current.remove name
15
+ end
16
+
17
+ def with_tenant(name)
18
+ ActiveTenant.current.with(name) { yield }
19
+ end
20
+
21
+ def tenant?
22
+ !ActiveTenant.current.global?
23
+ end
24
+
25
+ def tenant_name
26
+ ActiveTenant.current.name if tenant?
27
+ end
28
+
29
+ def belongs_to_tenant_global
30
+ establish_connection ActiveTenant.current.connection_settings(ActiveTenant.current.global)
31
+ end
32
+
33
+ end
34
+
35
+ module Migration
36
+
37
+ def tenant(name=nil)
38
+ name ? @tenant_name = name : @tenant_name
39
+ end
40
+
41
+ def migrate_global(version=nil)
42
+ ActiveTenant.current.migrate_global version
43
+ end
44
+
45
+ def migrate_tenant(name, version=nil)
46
+ ActiveTenant.current.migrate name, version
47
+ end
48
+
49
+ def migrate_all_tenants(version=nil)
50
+ ActiveTenant.current.migrate_all version
51
+ end
52
+
53
+ def migrate_all(version=nil)
54
+ migrate_global version
55
+ migrate_all_tenants version
56
+ end
57
+
58
+ end
59
+ end
56
60
  end
@@ -1,70 +1,73 @@
1
- module ActiveTenant
2
- class PostgresAdapter
3
- delegate :connection_config, :establish_connection, :connection, to: ::ActiveRecord::Base
4
-
5
- def initialize
6
- self.global = ActiveTenant.configuration.global if ActiveTenant.configuration.global
7
- end
8
-
9
- def all
10
- connection.select_values("SELECT nspname FROM pg_namespace WHERE nspname <> 'information_schema' AND nspname NOT LIKE 'pg%'") - [global]
11
- end
12
-
13
- def create(name)
14
- unless all.include? name
15
- connection.execute "CREATE SCHEMA \"#{name}\""
16
- end
17
- end
18
-
19
- def remove(name)
20
- if all.include? name
21
- connection.execute "DROP SCHEMA \"#{name}\" CASCADE"
22
- end
23
- end
24
-
25
- def with(name)
26
- return yield if name == search_path
27
-
28
- ex = nil
29
- current_schema = search_path
30
- search_path name
31
- begin
32
- result = yield
33
- rescue => e
34
- ex = e
35
- ensure
36
- search_path current_schema
37
- raise ex unless ex.nil?
38
- result
39
- end
40
- end
41
-
42
- def name
43
- search_path
44
- end
45
-
46
- def global
47
- @global || 'public'
48
- end
49
-
50
- def global?
51
- global == name
52
- end
53
-
54
- private
55
-
56
- def global=(name)
57
- @global = name
58
- end
59
-
60
- def search_path(name=nil)
61
- if name
62
- connection.execute("SET SEARCH_PATH TO \"#{name}\"")
63
- establish_connection connection_config.merge(schema_search_path: name)
64
- else
65
- connection_config[:schema_search_path]
66
- end
67
- end
68
-
69
- end
1
+ module ActiveTenant
2
+ class PostgresAdapter
3
+ delegate :connection_config, :establish_connection, :connection, to: ::ActiveRecord::Base
4
+
5
+ def initialize
6
+ self.global = ActiveTenant.configuration.global if ActiveTenant.configuration.global
7
+ end
8
+
9
+ def all
10
+ connection.select_values("SELECT nspname FROM pg_namespace WHERE nspname <> 'information_schema' AND nspname NOT LIKE 'pg%'") - [global]
11
+ end
12
+
13
+ def create(name)
14
+ unless all.include? name
15
+ connection.execute "CREATE SCHEMA \"#{name}\""
16
+ end
17
+ end
18
+
19
+ def remove(name)
20
+ if all.include? name
21
+ connection.execute "DROP SCHEMA \"#{name}\" CASCADE"
22
+ end
23
+ end
24
+
25
+ def with(name)
26
+ return yield if name == search_path
27
+
28
+ ex = nil
29
+ current_schema = search_path
30
+ search_path name
31
+ begin
32
+ result = yield
33
+ rescue => e
34
+ ex = e
35
+ ensure
36
+ search_path current_schema
37
+ raise ex unless ex.nil?
38
+ result
39
+ end
40
+ end
41
+
42
+ def connection_settings(name)
43
+ connection_config.merge(schema_search_path: name)
44
+ end
45
+
46
+ def name
47
+ search_path
48
+ end
49
+
50
+ def global
51
+ @global || 'public'
52
+ end
53
+
54
+ def global?
55
+ global == name
56
+ end
57
+
58
+ private
59
+
60
+ def global=(name)
61
+ @global = name
62
+ end
63
+
64
+ def search_path(name=nil)
65
+ if name
66
+ establish_connection connection_settings(name)
67
+ else
68
+ connection_config[:schema_search_path]
69
+ end
70
+ end
71
+
72
+ end
70
73
  end
@@ -1,72 +1,76 @@
1
- module ActiveTenant
2
- class SQLiteAdapter
3
- delegate :connection_config, :establish_connection, :connection, to: ::ActiveRecord::Base
4
-
5
- def initialize
6
- self.global = ActiveTenant.configuration.global if ActiveTenant.configuration.global
7
- end
8
-
9
- def all
10
- path = database_path
11
- Dir.glob("#{path}/*.sqlite3").map { |f| File.basename(f, '.sqlite3') } - [global]
12
- end
13
-
14
- def create(name)
15
- unless all.include? name
16
- current_config = connection_config
17
- establish_connection current_config.merge(database: file_name(name))
18
- connection
19
- establish_connection current_config
20
- end
21
- end
22
-
23
- def remove(name)
24
- file = file_name name
25
- FileUtils.rm(file) if File.exist?(file)
26
- end
27
-
28
- def with(name)
29
- return yield if name == self.name
30
-
31
- current_config = connection_config
32
- ex = nil
33
- begin
34
- establish_connection current_config.merge(database: file_name(name))
35
- result = yield
36
- rescue => e
37
- ex = e
38
- ensure
39
- establish_connection current_config
40
- raise ex unless ex.nil?
41
- result
42
- end
43
- end
44
-
45
- def name
46
- File.basename(connection_config[:database], '.sqlite3')
47
- end
48
-
49
- def global
50
- @global
51
- end
52
-
53
- def global?
54
- global == name
55
- end
56
-
57
- private
58
-
59
- def global=(name)
60
- @global = name
61
- end
62
-
63
- def database_path
64
- File.dirname(connection_config[:database])
65
- end
66
-
67
- def file_name(name)
68
- "#{database_path}/#{name}.sqlite3"
69
- end
70
-
71
- end
1
+ module ActiveTenant
2
+ class SQLiteAdapter
3
+ delegate :connection_config, :establish_connection, :connection, to: ::ActiveRecord::Base
4
+
5
+ def initialize
6
+ self.global = ActiveTenant.configuration.global if ActiveTenant.configuration.global
7
+ end
8
+
9
+ def all
10
+ path = database_path
11
+ Dir.glob("#{path}/*.sqlite3").map { |f| File.basename(f, '.sqlite3') } - [global]
12
+ end
13
+
14
+ def create(name)
15
+ unless all.include? name
16
+ current_config = connection_config
17
+ establish_connection current_config.merge(database: file_name(name))
18
+ connection
19
+ establish_connection current_config
20
+ end
21
+ end
22
+
23
+ def remove(name)
24
+ file = file_name name
25
+ FileUtils.rm(file) if File.exist?(file)
26
+ end
27
+
28
+ def with(name)
29
+ return yield if name == self.name
30
+
31
+ current_config = connection_config
32
+ ex = nil
33
+ begin
34
+ establish_connection connection_settings(name)
35
+ result = yield
36
+ rescue => e
37
+ ex = e
38
+ ensure
39
+ establish_connection current_config
40
+ raise ex unless ex.nil?
41
+ result
42
+ end
43
+ end
44
+
45
+ def connection_settings(name)
46
+ connection_config.merge(database: file_name(name))
47
+ end
48
+
49
+ def name
50
+ File.basename(connection_config[:database], '.sqlite3')
51
+ end
52
+
53
+ def global
54
+ @global
55
+ end
56
+
57
+ def global?
58
+ global == name
59
+ end
60
+
61
+ private
62
+
63
+ def global=(name)
64
+ @global = name
65
+ end
66
+
67
+ def database_path
68
+ File.dirname(connection_config[:database])
69
+ end
70
+
71
+ def file_name(name)
72
+ "#{database_path}/#{name}.sqlite3"
73
+ end
74
+
75
+ end
72
76
  end