storey 2.1.2 → 2.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +67 -0
- data/lib/storey.rb +2 -0
- data/lib/storey/rack_switch.rb +19 -0
- data/lib/storey/version.rb +1 -1
- data/spec/lib/storey/rack_switch_spec.rb +51 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bbbb6a1e4e73a064abdc73ec6a2730af44a48f19827288fa480a5bfca422b246
|
4
|
+
data.tar.gz: 9a2801e339b05307db2169c3d39a9bd05707b8b5b6fdaf10d5712d3ca8ba79cf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d85cf6045e666a7c8810af9090363be9dc1cf998ba6764281d44495fa0dc845526fa25a1f7cc17e20ec0bb47f6046fa8018efae533f76f72afe10f736fcade48
|
7
|
+
data.tar.gz: b61f568f85ef3d2c75c63f2af61b16b83b4534aba7ec58950e0b963dcce676d00f7aed5256193be754ebeba0f6e324e23d6419d9bde5c439678eba1647ce0466
|
data/CHANGELOG.md
CHANGED
@@ -2,6 +2,10 @@
|
|
2
2
|
All notable changes to this project will be documented in this file.
|
3
3
|
This project adheres to [Semantic Versioning](http://semver.org/).
|
4
4
|
|
5
|
+
## [2.2.0] - 2019-02-08
|
6
|
+
### Added
|
7
|
+
- Ability to switch via a Rack app
|
8
|
+
|
5
9
|
## [2.1.2] - 2018-08-14
|
6
10
|
### Fixed
|
7
11
|
- Instances when queries are cached between switches
|
data/README.md
CHANGED
@@ -29,6 +29,65 @@ Storey.configure do |c|
|
|
29
29
|
end
|
30
30
|
```
|
31
31
|
|
32
|
+
## Switching in your Rack app
|
33
|
+
|
34
|
+
To switch in your application, you can just opt to call `Storey.switch`
|
35
|
+
somewhere in your app. For example, in a Rails ApplicationController it would
|
36
|
+
look like:
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
class ApplicationController
|
40
|
+
before_action :switch_to_tenant
|
41
|
+
|
42
|
+
def switch_to_tenant
|
43
|
+
subdomain = request.subdomain
|
44
|
+
Storey.switch(subdomain) if Website.exists?(subdomain: subdomain)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
```
|
48
|
+
|
49
|
+
There are some instances where you need to switch schemas before you get to your
|
50
|
+
application. A good example is Devise. Devise uses Warden to authenticate users.
|
51
|
+
Warden is inserted as a Rack application and checks to see if the user
|
52
|
+
attempting to access the page signed in.
|
53
|
+
|
54
|
+
To do this, it must check the database. If your users live on separate schemas,
|
55
|
+
there's a big chance that Warden will think the user does not exist. Especially
|
56
|
+
in this scenario, use the Rack app found in this gem. In Rails, insert this
|
57
|
+
somewhere in your `application.rb` or in an initializer:
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
Rails.application.config.middleware.
|
61
|
+
insert_before Warden::Manager, Storey::RackSwitch
|
62
|
+
```
|
63
|
+
|
64
|
+
You must also define how to determine the schema to switch to. To do that, set
|
65
|
+
`switch_processor`:
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
Storey.configure do |c|
|
69
|
+
c.switch_processor = ->(env) do
|
70
|
+
# find the schema name based on something in the env
|
71
|
+
subdomain = find_in_env(env)
|
72
|
+
return subdomain if Website.exists?(subdomain: schema)
|
73
|
+
end
|
74
|
+
|
75
|
+
# You can pass any object that responds to call and accepts one arg: the env.
|
76
|
+
c.switch_processor = MyStoreySwitchProcessor
|
77
|
+
end
|
78
|
+
|
79
|
+
class MyStoreySwitchProcessor
|
80
|
+
def self.call(env)
|
81
|
+
subdomain = find_in_env(env)
|
82
|
+
# ...
|
83
|
+
end
|
84
|
+
end
|
85
|
+
```
|
86
|
+
|
87
|
+
When the result of `switch_processor` is a string,
|
88
|
+
`Storey.switch('the-string-it-returns')` is called. If `nil`, no switching
|
89
|
+
happens.
|
90
|
+
|
32
91
|
# Methods
|
33
92
|
|
34
93
|
## schema
|
@@ -122,3 +181,11 @@ Usage:
|
|
122
181
|
## storey:hstore:install
|
123
182
|
|
124
183
|
Run `rake storey:hstore:install` to install hstore extension into the hstore schema. Ensure that 'hstore' is one of the persistent schemas.
|
184
|
+
|
185
|
+
# Development
|
186
|
+
|
187
|
+
In the storey directory, after installing the gems:
|
188
|
+
|
189
|
+
- `docker-compose up db`
|
190
|
+
- `cp spec/dummy/config/database.yml{.sample,}
|
191
|
+
- `rspec spec`
|
data/lib/storey.rb
CHANGED
@@ -6,6 +6,7 @@ require "open3"
|
|
6
6
|
require "storey/version"
|
7
7
|
require "rails/all"
|
8
8
|
require 'storey/railtie' if defined?(Rails)
|
9
|
+
require 'storey/rack_switch'
|
9
10
|
require 'storey/exceptions'
|
10
11
|
require 'storey/migrator'
|
11
12
|
require 'storey/duplicator'
|
@@ -37,6 +38,7 @@ module Storey
|
|
37
38
|
has :suffix, classes: [String, NilClass]
|
38
39
|
has :persistent_schemas, classes: Array, default: []
|
39
40
|
has :excluded_models, classes: Array, default: []
|
41
|
+
has :switch_processor
|
40
42
|
end
|
41
43
|
|
42
44
|
def self.persistent_schemas=(schemas)
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Storey
|
2
|
+
class RackSwitch
|
3
|
+
|
4
|
+
def initialize(app, processor=Storey.configuration.switch_processor)
|
5
|
+
@app, @processor = app, processor
|
6
|
+
end
|
7
|
+
|
8
|
+
def call(env)
|
9
|
+
schema = @processor.call(env)
|
10
|
+
|
11
|
+
if schema
|
12
|
+
Storey.switch(schema) { @app.call(env) }
|
13
|
+
else
|
14
|
+
@app.call(env)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
data/lib/storey/version.rb
CHANGED
@@ -0,0 +1,51 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
module Storey
|
4
|
+
RSpec.describe RackSwitch do
|
5
|
+
|
6
|
+
let(:env) do
|
7
|
+
{ "env" => "here" }
|
8
|
+
end
|
9
|
+
|
10
|
+
context "processor returns nil" do
|
11
|
+
let(:processor) { ->(env) { nil } }
|
12
|
+
before do
|
13
|
+
Storey.create("original_schema")
|
14
|
+
Storey.switch("original_schema")
|
15
|
+
end
|
16
|
+
|
17
|
+
it "does not switch and continues with the next rack app" do
|
18
|
+
schema_in_app = nil
|
19
|
+
app = ->(env) { schema_in_app = Storey.schema }
|
20
|
+
|
21
|
+
expect(processor).to receive(:call).with(env).
|
22
|
+
and_call_original
|
23
|
+
|
24
|
+
switch = described_class.new(app, processor)
|
25
|
+
switch.(env)
|
26
|
+
|
27
|
+
expect(schema_in_app).to eq "original_schema"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context "processor returns a string" do
|
32
|
+
let(:processor) { ->(env) { "tenant" } }
|
33
|
+
before do
|
34
|
+
Storey.create("tenant")
|
35
|
+
end
|
36
|
+
|
37
|
+
it "switches to the string returned by the processor" do
|
38
|
+
schema_in_app = nil
|
39
|
+
app = ->(env) { schema_in_app = Storey.schema }
|
40
|
+
|
41
|
+
expect(processor).to receive(:call).with(env).
|
42
|
+
and_call_original
|
43
|
+
|
44
|
+
switch = described_class.new(app, processor)
|
45
|
+
switch.(env)
|
46
|
+
|
47
|
+
expect(schema_in_app).to eq "tenant"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: storey
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ramon Tayag
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-02-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec-rails
|
@@ -129,6 +129,7 @@ files:
|
|
129
129
|
- lib/storey/hstore.rb
|
130
130
|
- lib/storey/migrator.rb
|
131
131
|
- lib/storey/native_schema_matcher.rb
|
132
|
+
- lib/storey/rack_switch.rb
|
132
133
|
- lib/storey/railtie.rb
|
133
134
|
- lib/storey/resets_column_info.rb
|
134
135
|
- lib/storey/ruby_dumper.rb
|
@@ -179,6 +180,7 @@ files:
|
|
179
180
|
- spec/dummy/public/favicon.ico
|
180
181
|
- spec/dummy/script/rails
|
181
182
|
- spec/fixtures/.gitkeep
|
183
|
+
- spec/lib/storey/rack_switch_spec.rb
|
182
184
|
- spec/lib/storey_spec.rb
|
183
185
|
- spec/migrator_spec.rb
|
184
186
|
- spec/spec_helper.rb
|
@@ -276,6 +278,7 @@ test_files:
|
|
276
278
|
- spec/dummy/public/favicon.ico
|
277
279
|
- spec/dummy/script/rails
|
278
280
|
- spec/fixtures/.gitkeep
|
281
|
+
- spec/lib/storey/rack_switch_spec.rb
|
279
282
|
- spec/lib/storey_spec.rb
|
280
283
|
- spec/migrator_spec.rb
|
281
284
|
- spec/spec_helper.rb
|