rails_multitenant 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.ruby-version +1 -0
- data/.travis.yml +10 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/README.md +36 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/rails_multitenant.rb +5 -0
- data/lib/rails_multitenant/global_context_registry.rb +215 -0
- data/lib/rails_multitenant/multitenant_model.rb +43 -0
- data/lib/rails_multitenant/version.rb +3 -0
- data/log/test.log +875 -0
- data/rails_multitenant.gemspec +31 -0
- data/spec/db/database.yml +3 -0
- data/spec/db/schema.rb +23 -0
- data/spec/global_context_registry_spec.rb +84 -0
- data/spec/item_spec.rb +35 -0
- data/spec/spec_helper.rb +53 -0
- metadata +207 -0
@@ -0,0 +1,31 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rails_multitenant/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "rails_multitenant"
|
8
|
+
spec.version = RailsMultitenant::VERSION
|
9
|
+
spec.authors = ["Pat Breault"]
|
10
|
+
spec.email = ["pbreault@salsify.com"]
|
11
|
+
spec.summary = %q{Automatically configures multiple tenants in a Rails environment}
|
12
|
+
spec.description = %q{Handles multiple tenants in a Rails environment}
|
13
|
+
spec.homepage = "https://github.com/salsify/rails_multitenant"
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.test_files = Dir.glob('spec/**/*')
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
21
|
+
|
22
|
+
spec.add_dependency 'activerecord', ENV.fetch('RAILS_VERSION', ['>= 3.2', '< 4.3'])
|
23
|
+
spec.add_dependency 'activesupport', ENV.fetch('RAILS_VERSION', ['>= 3.2', '< 4.3'])
|
24
|
+
|
25
|
+
spec.add_development_dependency 'coveralls'
|
26
|
+
spec.add_development_dependency 'database_cleaner', '>= 1.2'
|
27
|
+
spec.add_development_dependency 'rake'
|
28
|
+
spec.add_development_dependency 'rspec', '~> 2'
|
29
|
+
spec.add_development_dependency 'simplecov', '~> 0.7.1'
|
30
|
+
spec.add_development_dependency 'sqlite3'
|
31
|
+
end
|
data/spec/db/schema.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
ActiveRecord::Schema.define(:version => 0) do
|
4
|
+
|
5
|
+
create_table(:organizations, force: true) do |t|
|
6
|
+
t.string :name
|
7
|
+
end
|
8
|
+
|
9
|
+
create_table(:items, force: true) do |t|
|
10
|
+
t.string :name
|
11
|
+
t.integer :organization_id
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
class Organization < ActiveRecord::Base
|
17
|
+
include RailsMultitenant::GlobalContextRegistry::CurrentInstance
|
18
|
+
end
|
19
|
+
|
20
|
+
class Item < ActiveRecord::Base
|
21
|
+
include RailsMultitenant::MultitenantModel
|
22
|
+
multitenant_model_on :organization
|
23
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
include RailsMultitenant
|
3
|
+
|
4
|
+
describe GlobalContextRegistry do
|
5
|
+
|
6
|
+
before do
|
7
|
+
# The framework will setup the organization; clear that out
|
8
|
+
GlobalContextRegistry.new_registry
|
9
|
+
GlobalContextRegistry.set(:foo, 'bar')
|
10
|
+
end
|
11
|
+
|
12
|
+
describe '.get and .set' do
|
13
|
+
specify do
|
14
|
+
expect(GlobalContextRegistry.get(:foo)).to eq 'bar'
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '.delete' do
|
19
|
+
specify do
|
20
|
+
expect(GlobalContextRegistry.delete(:foo)).to eq 'bar'
|
21
|
+
expect(GlobalContextRegistry.get(:foo)).to be_nil
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe '.with_isolated_registry' do
|
26
|
+
specify do
|
27
|
+
GlobalContextRegistry.with_isolated_registry do
|
28
|
+
expect(GlobalContextRegistry.get(:foo)).to be_nil
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe '.replace_registry and .new_registry' do
|
34
|
+
let!(:old_registry) { GlobalContextRegistry.new_registry }
|
35
|
+
|
36
|
+
specify do
|
37
|
+
expect(old_registry).to eq ({ foo: 'bar' })
|
38
|
+
end
|
39
|
+
specify do
|
40
|
+
expect(GlobalContextRegistry.get(:foo)).to be_nil
|
41
|
+
end
|
42
|
+
specify do
|
43
|
+
GlobalContextRegistry.replace_registry(old_registry)
|
44
|
+
expect(GlobalContextRegistry.get(:foo)).to eq 'bar'
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe '.duplicate_registry' do
|
49
|
+
def setup_registry; end
|
50
|
+
|
51
|
+
before { setup_registry }
|
52
|
+
let!(:dupe) { GlobalContextRegistry.duplicate_registry }
|
53
|
+
|
54
|
+
specify do
|
55
|
+
expect(GlobalContextRegistry.new_registry).to eq dupe
|
56
|
+
end
|
57
|
+
specify do
|
58
|
+
expect(GlobalContextRegistry.new_registry.object_id).not_to eq dupe.object_id
|
59
|
+
end
|
60
|
+
specify do
|
61
|
+
expect(GlobalContextRegistry.get(:foo).object_id).not_to eq dupe[:foo].object_id
|
62
|
+
end
|
63
|
+
|
64
|
+
context 'with nils' do
|
65
|
+
def setup_registry
|
66
|
+
GlobalContextRegistry.set(:bar, nil)
|
67
|
+
end
|
68
|
+
|
69
|
+
specify do
|
70
|
+
expect(dupe).to eq GlobalContextRegistry.new_registry
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
context 'with fixnums' do
|
75
|
+
def setup_registry
|
76
|
+
GlobalContextRegistry.set(:bar, 5)
|
77
|
+
end
|
78
|
+
|
79
|
+
specify do
|
80
|
+
expect(dupe).to eq GlobalContextRegistry.new_registry
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
data/spec/item_spec.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# Create multiple orgs
|
2
|
+
# Create an item in each
|
3
|
+
# Make sure you can only see one org's item in one org
|
4
|
+
|
5
|
+
require 'spec_helper'
|
6
|
+
|
7
|
+
describe Item do
|
8
|
+
|
9
|
+
let!(:item1) { Item.create(name: 'item1') }
|
10
|
+
|
11
|
+
let!(:org2) { Organization.create(name: 'org2') }
|
12
|
+
let!(:item2) { org2.as_current { Item.create(name: 'item2') } }
|
13
|
+
let!(:item3) { org2.as_current { Item.create(name: 'item3') } }
|
14
|
+
|
15
|
+
specify 'default org should have one item' do
|
16
|
+
expect(Item.all).to eq [ item1 ]
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'does not return item2' do
|
20
|
+
expect(Item.where(id: item2.id)).to eq []
|
21
|
+
end
|
22
|
+
|
23
|
+
specify 'org2 should have two items' do
|
24
|
+
org2.as_current do
|
25
|
+
expect(Item.all).to eq [ item2, item3]
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'does not return item1' do
|
30
|
+
org2.as_current do
|
31
|
+
expect(Item.where(id: item1.id)).to eq []
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require 'simplecov'
|
4
|
+
require 'coveralls'
|
5
|
+
|
6
|
+
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
|
7
|
+
SimpleCov::Formatter::HTMLFormatter,
|
8
|
+
Coveralls::SimpleCov::Formatter
|
9
|
+
]
|
10
|
+
SimpleCov.start do
|
11
|
+
add_filter 'spec'
|
12
|
+
end
|
13
|
+
|
14
|
+
require 'logger'
|
15
|
+
require 'database_cleaner'
|
16
|
+
require 'rails_multitenant'
|
17
|
+
require 'yaml'
|
18
|
+
|
19
|
+
FileUtils.makedirs('log')
|
20
|
+
|
21
|
+
ActiveRecord::Base.logger = Logger.new('log/test.log')
|
22
|
+
ActiveRecord::Base.logger.level = Logger::DEBUG
|
23
|
+
ActiveRecord::Migration.verbose = false
|
24
|
+
|
25
|
+
db_adapter = ENV.fetch('ADAPTER', 'sqlite3')
|
26
|
+
config = YAML.load(File.read('spec/db/database.yml'))
|
27
|
+
ActiveRecord::Base.establish_connection(config[db_adapter])
|
28
|
+
require 'db/schema'
|
29
|
+
|
30
|
+
RSpec.configure do |config|
|
31
|
+
config.order = 'random'
|
32
|
+
|
33
|
+
config.add_setting :test_org_id
|
34
|
+
|
35
|
+
config.before(:suite) do
|
36
|
+
DatabaseCleaner.clean_with(:truncation)
|
37
|
+
end
|
38
|
+
|
39
|
+
config.before(:each) do
|
40
|
+
DatabaseCleaner.strategy = :transaction
|
41
|
+
DatabaseCleaner.start
|
42
|
+
RailsMultitenant::GlobalContextRegistry.new_registry
|
43
|
+
org = Organization.create(name: 'test org')
|
44
|
+
RSpec.configuration.test_org_id = org.id
|
45
|
+
Organization.current_id = RSpec.configuration.test_org_id
|
46
|
+
end
|
47
|
+
|
48
|
+
config.after(:each) do
|
49
|
+
DatabaseCleaner.clean
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
puts "Testing with ActiveRecord #{ActiveRecord::VERSION::STRING}"
|
metadata
ADDED
@@ -0,0 +1,207 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails_multitenant
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Pat Breault
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-12-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activerecord
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.2'
|
34
|
+
- - "<"
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '4.3'
|
37
|
+
type: :runtime
|
38
|
+
prerelease: false
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '3.2'
|
44
|
+
- - "<"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '4.3'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: activesupport
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '3.2'
|
54
|
+
- - "<"
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '4.3'
|
57
|
+
type: :runtime
|
58
|
+
prerelease: false
|
59
|
+
version_requirements: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '3.2'
|
64
|
+
- - "<"
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '4.3'
|
67
|
+
- !ruby/object:Gem::Dependency
|
68
|
+
name: coveralls
|
69
|
+
requirement: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
type: :development
|
75
|
+
prerelease: false
|
76
|
+
version_requirements: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
- !ruby/object:Gem::Dependency
|
82
|
+
name: database_cleaner
|
83
|
+
requirement: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '1.2'
|
88
|
+
type: :development
|
89
|
+
prerelease: false
|
90
|
+
version_requirements: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '1.2'
|
95
|
+
- !ruby/object:Gem::Dependency
|
96
|
+
name: rake
|
97
|
+
requirement: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
- !ruby/object:Gem::Dependency
|
110
|
+
name: rspec
|
111
|
+
requirement: !ruby/object:Gem::Requirement
|
112
|
+
requirements:
|
113
|
+
- - "~>"
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '2'
|
116
|
+
type: :development
|
117
|
+
prerelease: false
|
118
|
+
version_requirements: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - "~>"
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '2'
|
123
|
+
- !ruby/object:Gem::Dependency
|
124
|
+
name: simplecov
|
125
|
+
requirement: !ruby/object:Gem::Requirement
|
126
|
+
requirements:
|
127
|
+
- - "~>"
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: 0.7.1
|
130
|
+
type: :development
|
131
|
+
prerelease: false
|
132
|
+
version_requirements: !ruby/object:Gem::Requirement
|
133
|
+
requirements:
|
134
|
+
- - "~>"
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
version: 0.7.1
|
137
|
+
- !ruby/object:Gem::Dependency
|
138
|
+
name: sqlite3
|
139
|
+
requirement: !ruby/object:Gem::Requirement
|
140
|
+
requirements:
|
141
|
+
- - ">="
|
142
|
+
- !ruby/object:Gem::Version
|
143
|
+
version: '0'
|
144
|
+
type: :development
|
145
|
+
prerelease: false
|
146
|
+
version_requirements: !ruby/object:Gem::Requirement
|
147
|
+
requirements:
|
148
|
+
- - ">="
|
149
|
+
- !ruby/object:Gem::Version
|
150
|
+
version: '0'
|
151
|
+
description: Handles multiple tenants in a Rails environment
|
152
|
+
email:
|
153
|
+
- pbreault@salsify.com
|
154
|
+
executables: []
|
155
|
+
extensions: []
|
156
|
+
extra_rdoc_files: []
|
157
|
+
files:
|
158
|
+
- ".gitignore"
|
159
|
+
- ".ruby-version"
|
160
|
+
- ".travis.yml"
|
161
|
+
- CODE_OF_CONDUCT.md
|
162
|
+
- Gemfile
|
163
|
+
- README.md
|
164
|
+
- Rakefile
|
165
|
+
- bin/console
|
166
|
+
- bin/setup
|
167
|
+
- lib/rails_multitenant.rb
|
168
|
+
- lib/rails_multitenant/global_context_registry.rb
|
169
|
+
- lib/rails_multitenant/multitenant_model.rb
|
170
|
+
- lib/rails_multitenant/version.rb
|
171
|
+
- log/test.log
|
172
|
+
- rails_multitenant.gemspec
|
173
|
+
- spec/db/database.yml
|
174
|
+
- spec/db/schema.rb
|
175
|
+
- spec/global_context_registry_spec.rb
|
176
|
+
- spec/item_spec.rb
|
177
|
+
- spec/spec_helper.rb
|
178
|
+
homepage: https://github.com/salsify/rails_multitenant
|
179
|
+
licenses:
|
180
|
+
- MIT
|
181
|
+
metadata: {}
|
182
|
+
post_install_message:
|
183
|
+
rdoc_options: []
|
184
|
+
require_paths:
|
185
|
+
- lib
|
186
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
187
|
+
requirements:
|
188
|
+
- - ">="
|
189
|
+
- !ruby/object:Gem::Version
|
190
|
+
version: '0'
|
191
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
192
|
+
requirements:
|
193
|
+
- - ">="
|
194
|
+
- !ruby/object:Gem::Version
|
195
|
+
version: '0'
|
196
|
+
requirements: []
|
197
|
+
rubyforge_project:
|
198
|
+
rubygems_version: 2.4.5.1
|
199
|
+
signing_key:
|
200
|
+
specification_version: 4
|
201
|
+
summary: Automatically configures multiple tenants in a Rails environment
|
202
|
+
test_files:
|
203
|
+
- spec/db/database.yml
|
204
|
+
- spec/db/schema.rb
|
205
|
+
- spec/global_context_registry_spec.rb
|
206
|
+
- spec/item_spec.rb
|
207
|
+
- spec/spec_helper.rb
|