rails_multitenant 0.2.0 → 0.3.0
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 +4 -4
- data/CHANGELOG.md +5 -1
- data/README.md +70 -5
- data/lib/rails_multitenant/global_context_registry.rb +4 -5
- data/lib/rails_multitenant/version.rb +1 -1
- data/spec/global_context_registry_spec.rb +15 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 316a055e8cc0d31080584671093b6dc56f022699
|
4
|
+
data.tar.gz: 50c5641c12bd0d5b8e2c80b74a8c4a2b9aa10dc7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7008b9aeae0c543d437198f5e14037183e79a871579d966e0b8ca7b60c037697cdfa698f92666e9d4ba216ee5abb492156b5a9cf86675d6d975cb9f2e1cee5a9
|
7
|
+
data.tar.gz: d98d65403a5f4bb5eaffbf20d265fd9755734567eff9df23d9cbcfde833fc981199011da8ef9dbbacaa6564f0b640c59452537b21874508998064dfdd0e44919
|
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
-
### 0.
|
3
|
+
### 0.3.0
|
4
|
+
* Modify `RailsMultitenant::GlobalContextRegistry#new_registry` to accept an arg
|
5
|
+
specifying the new registry to set. The previous registry is still returned.
|
6
|
+
|
7
|
+
### 0.2.0
|
4
8
|
* Merged [PR 2](https://github.com/salsify/rails-multitenant/pull/2) which adds support for
|
5
9
|
multi-tenancy based on a foreign key to an external model. As part of this the `multitenant_model_on`
|
6
10
|
method was renamed to `multitenant_on_model`.
|
data/README.md
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
# RailsMultitenant
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
rails_multitenant is a gem for isolating ActiveRecord models from different tenants. The gem assumes tables storing
|
4
|
+
multi-tenant models include an appropriate tenant id column.
|
6
5
|
|
7
6
|
## Installation
|
8
7
|
|
@@ -22,7 +21,73 @@ Or install it yourself as:
|
|
22
21
|
|
23
22
|
## Usage
|
24
23
|
|
25
|
-
|
24
|
+
The gem supports two multi-tenancy strategies:
|
25
|
+
|
26
|
+
1. Based on a model attribute, typically a foreign key to an entity owned by another service
|
27
|
+
2. Based on a model association
|
28
|
+
|
29
|
+
The gem uses ActiveRecord default scopes to make isolating tenants fairly transparent.
|
30
|
+
|
31
|
+
### Multi-tenancy Based on Model Attributes
|
32
|
+
|
33
|
+
The following model is multi-tenant based on an `organization_id` attribute:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
class Product < ActiveRecord::Base
|
37
|
+
include RailsMultitenant::MultitenantModel
|
38
|
+
|
39
|
+
multitenant_on :organization_id
|
40
|
+
end
|
41
|
+
```
|
42
|
+
|
43
|
+
The model can then be used as follows:
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
RailsMultitenant::GlobalContextRegistry[:organization_id] = 'my-org'
|
47
|
+
|
48
|
+
# Only returns products from 'my-org'
|
49
|
+
Product.all
|
50
|
+
|
51
|
+
# Returns products across all orgs
|
52
|
+
Product.strip_organization_scope.all
|
53
|
+
|
54
|
+
# Or set the current organization in block form
|
55
|
+
RailsMultitenant::GlobalContextRegistry.with_isolated_registry(organization_id: 'my-org') do
|
56
|
+
# Only returns products from 'my-org'
|
57
|
+
Product.all
|
58
|
+
end
|
59
|
+
```
|
60
|
+
|
61
|
+
### Multi-tenancy Based on Associated Models
|
62
|
+
|
63
|
+
The following model is multi-tenant based on an `Organization` model:
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
class Product < ActiveRecord::Base
|
67
|
+
include RailsMultitenant::MultitenantModel
|
68
|
+
|
69
|
+
multitenant_on_model :organization
|
70
|
+
end
|
71
|
+
```
|
72
|
+
|
73
|
+
The model can then be used as follows:
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
Organization.current_id = 1
|
77
|
+
|
78
|
+
# Only returns products from organization 1
|
79
|
+
Product.all
|
80
|
+
|
81
|
+
# Use the automatically generated belongs_to association to get
|
82
|
+
# a product's organization
|
83
|
+
Product.first.organization
|
84
|
+
|
85
|
+
# Or set the current organization in block form
|
86
|
+
Organization.as_current_id(1) do
|
87
|
+
# Only returns products from organization 1
|
88
|
+
Product.all
|
89
|
+
end
|
90
|
+
```
|
26
91
|
|
27
92
|
## Development
|
28
93
|
|
@@ -32,5 +97,5 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
32
97
|
|
33
98
|
## Contributing
|
34
99
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
100
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/salsify/rails_multitenant. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
36
101
|
|
@@ -164,8 +164,7 @@ module RailsMultitenant
|
|
164
164
|
|
165
165
|
# Run a block of code with an the given registry
|
166
166
|
def with_isolated_registry(registry = {})
|
167
|
-
prior_globals =
|
168
|
-
self.globals = registry
|
167
|
+
prior_globals = new_registry(registry)
|
169
168
|
yield
|
170
169
|
ensure
|
171
170
|
self.globals = prior_globals
|
@@ -175,10 +174,10 @@ module RailsMultitenant
|
|
175
174
|
# Note: these methods are intended for use in a manner like .with_isolated_registry,
|
176
175
|
# but in contexts where around semantics are not allowed.
|
177
176
|
|
178
|
-
# Set a new, empty registry, returning the previous one.
|
179
|
-
def new_registry
|
177
|
+
# Set a new, by default empty registry, returning the previous one.
|
178
|
+
def new_registry(registry = {})
|
180
179
|
priors = globals
|
181
|
-
self.globals =
|
180
|
+
self.globals = registry
|
182
181
|
priors
|
183
182
|
end
|
184
183
|
|
@@ -50,7 +50,7 @@ describe GlobalContextRegistry do
|
|
50
50
|
let!(:old_registry) { GlobalContextRegistry.new_registry }
|
51
51
|
|
52
52
|
specify do
|
53
|
-
expect(old_registry).to eq
|
53
|
+
expect(old_registry).to eq({ foo: 'bar' })
|
54
54
|
end
|
55
55
|
specify do
|
56
56
|
expect(GlobalContextRegistry.get(:foo)).to be_nil
|
@@ -59,6 +59,20 @@ describe GlobalContextRegistry do
|
|
59
59
|
GlobalContextRegistry.replace_registry(old_registry)
|
60
60
|
expect(GlobalContextRegistry.get(:foo)).to eq 'bar'
|
61
61
|
end
|
62
|
+
|
63
|
+
context 'when a new registry is specified' do
|
64
|
+
let!(:old_registry) { GlobalContextRegistry.new_registry(bar: 'foo') }
|
65
|
+
|
66
|
+
specify do
|
67
|
+
expect(old_registry).to eq({ foo: 'bar' })
|
68
|
+
end
|
69
|
+
specify do
|
70
|
+
expect(GlobalContextRegistry.get(:foo)).to be_nil
|
71
|
+
end
|
72
|
+
specify do
|
73
|
+
expect(GlobalContextRegistry.get(:bar)).to eq 'foo'
|
74
|
+
end
|
75
|
+
end
|
62
76
|
end
|
63
77
|
|
64
78
|
describe '.duplicate_registry' do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_multitenant
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pat Breault
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-01-
|
11
|
+
date: 2016-01-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -198,7 +198,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
198
198
|
version: '0'
|
199
199
|
requirements: []
|
200
200
|
rubyforge_project:
|
201
|
-
rubygems_version: 2.4.
|
201
|
+
rubygems_version: 2.4.8
|
202
202
|
signing_key:
|
203
203
|
specification_version: 4
|
204
204
|
summary: Automatically configures multiple tenants in a Rails environment
|