sandboxy 3.0.0 → 3.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +198 -228
- data/app/models/sandbox.rb +7 -7
- data/lib/generators/sandboxy_generator.rb +23 -26
- data/lib/generators/templates/initializer.rb +13 -13
- data/lib/generators/templates/migration.rb.erb +6 -6
- data/lib/sandboxy.rb +7 -7
- data/lib/sandboxy/configuration.rb +47 -48
- data/lib/sandboxy/engine.rb +8 -6
- data/lib/sandboxy/middleware.rb +20 -18
- data/lib/sandboxy/railtie.rb +16 -15
- data/lib/sandboxy/sandboxed.rb +103 -91
- data/lib/sandboxy/version.rb +3 -3
- metadata +29 -35
- data/CHANGELOG.md +0 -32
- data/lib/generators/templates/README.md +0 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dc3184bd2874e25d0f5cb34fd99a14426335abaed4cc600081025f64d50f9f79
|
4
|
+
data.tar.gz: f1886b9a873a7ff1371e3854eeb440f4c09a36c85e9661931a425ca38cdd6472
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 66edca9dfd3663e9694a6c3f952bf3a5a7dd4aeae1bc5916705524511467466fbbb9736355c7f5e5de0bbe33a78ef383610bb8b32cbb5e5192d60739b9b31d76
|
7
|
+
data.tar.gz: 8e5294a1d6f591f8c57db56370b87af869410e7f00b91ee37b23383c52e3aecd4f736ca5423bde18a720984c6a544e6cbf99ed7577df7af9eb4edbe9d6815a73
|
data/README.md
CHANGED
@@ -1,228 +1,198 @@
|
|
1
|
-
# Sandboxy
|
2
|
-
|
3
|
-
[![Gem Version](https://badge.fury.io/rb/sandboxy.svg)](https://badge.fury.io/rb/sandboxy)
|
4
|
-
|
5
|
-
Sandboxy allows you to use virtual data-oriented environments inside a Rails application while being able to switch between them at runtime. It achieves that by using a combination of Rack Middleware and ActiveRecord.
|
6
|
-
|
7
|
-
---
|
8
|
-
|
9
|
-
## Table of Contents
|
10
|
-
|
11
|
-
* [Installation](#installation)
|
12
|
-
* [Usage](#usage)
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
* [Configuration](#configuration)
|
18
|
-
* [Testing](#testing)
|
19
|
-
|
20
|
-
* [To
|
21
|
-
* [Contributing](#contributing)
|
22
|
-
|
23
|
-
|
24
|
-
* [License](#license)
|
25
|
-
|
26
|
-
---
|
27
|
-
|
28
|
-
## Installation
|
29
|
-
|
30
|
-
Sandboxy works with Rails 5 onwards. You can add it to your `Gemfile` with:
|
31
|
-
|
32
|
-
```ruby
|
33
|
-
gem 'sandboxy'
|
34
|
-
```
|
35
|
-
|
36
|
-
And then execute:
|
37
|
-
|
38
|
-
$ bundle
|
39
|
-
|
40
|
-
Or install it yourself as:
|
41
|
-
|
42
|
-
$ gem install sandboxy
|
43
|
-
|
44
|
-
If you always want to be up to date fetch the latest from GitHub in your `Gemfile`:
|
45
|
-
|
46
|
-
```ruby
|
47
|
-
gem 'sandboxy', github: 'jonhue/sandboxy'
|
48
|
-
```
|
49
|
-
|
50
|
-
Now run the generator:
|
51
|
-
|
52
|
-
$ rails g sandboxy
|
53
|
-
|
54
|
-
To wrap things up, migrate the changes into your database:
|
55
|
-
|
56
|
-
$ rails db:migrate
|
57
|
-
|
58
|
-
## Usage
|
59
|
-
|
60
|
-
### Setup
|
61
|
-
|
62
|
-
Add Sandboxy to the models where you want to separate records depending on their environments:
|
63
|
-
|
64
|
-
```ruby
|
65
|
-
class Foo < ApplicationRecord
|
66
|
-
|
67
|
-
end
|
68
|
-
```
|
69
|
-
|
70
|
-
In most use cases you would want to add `sandboxy` to a lot of ActiveRecord models if not all. To simplify that you could create a new class and let all your models inherit from it:
|
71
|
-
|
72
|
-
```ruby
|
73
|
-
class SharedSandbox < ApplicationRecord
|
74
|
-
|
75
|
-
|
76
|
-
end
|
77
|
-
|
78
|
-
class Foo < SharedSandbox
|
79
|
-
end
|
80
|
-
```
|
81
|
-
|
82
|
-
### `sandboxy` methods
|
83
|
-
|
84
|
-
By default you can only access records belonging to the current environment (defined by `Sandboxy.environment`):
|
85
|
-
|
86
|
-
```ruby
|
87
|
-
Sandboxy.environment = 'test'
|
88
|
-
Foo.all # => returns all test foo's
|
89
|
-
Sandboxy.environment = 'sandbox'
|
90
|
-
Foo.all # => returns all sandbox foo's
|
91
|
-
```
|
92
|
-
|
93
|
-
Now to access the records belonging to a certain environment regardless of your current environment, you can use:
|
94
|
-
|
95
|
-
```ruby
|
96
|
-
Foo.live_environment # => returns all live foo's
|
97
|
-
Foo.sandbox_environment # => returns all sandbox foo's
|
98
|
-
Foo.desandbox # => returns all foo's
|
99
|
-
```
|
100
|
-
|
101
|
-
Let's check to which environment this `Foo` belongs:
|
102
|
-
|
103
|
-
```ruby
|
104
|
-
foo = Foo.create!
|
105
|
-
foo.live_environment? # => false
|
106
|
-
foo.sandbox_environment? # => true
|
107
|
-
```
|
108
|
-
|
109
|
-
You should keep in mind that when you create a new record, it will automatically belong to your app's current environment.
|
110
|
-
|
111
|
-
Don't worry, you can move records between environments:
|
112
|
-
|
113
|
-
```ruby
|
114
|
-
foo.move_environment_live
|
115
|
-
foo.live_environment? # => true
|
116
|
-
foo.move_environment_sandbox
|
117
|
-
foo.sandbox_environment? # => true
|
118
|
-
```
|
119
|
-
|
120
|
-
### Switching environments
|
121
|
-
|
122
|
-
At runtime you can always switch environments anywhere in your application by setting `Sandboxy.environment`. You can set it to any string you like. That makes Sandboxy super flexible.
|
123
|
-
|
124
|
-
```ruby
|
125
|
-
Sandboxy.environment = 'live'
|
126
|
-
Sandboxy.live_environment? # => true
|
127
|
-
Sandboxy.sandbox_environment? # =>
|
128
|
-
```
|
129
|
-
|
130
|
-
#### Sandbox & APIs
|
131
|
-
|
132
|
-
It's flexibility allows Sandboxy to work really well with APIs.
|
133
|
-
|
134
|
-
Typically an API provides two sets of authentication credentials for a consumer - one for live access and one for sandbox/testing.
|
135
|
-
|
136
|
-
Whenever you authenticate your API's consumer, just make sure to set `Sandboxy.environment` accordingly to the credential the consumer used. From thereon, Sandboxy will make sure that your consumer only reads & updates data from the environment he is in.
|
137
|
-
|
138
|
-
---
|
139
|
-
|
140
|
-
## Configuration
|
141
|
-
|
142
|
-
You can configure Sandboxy by passing a block to `configure`. This can be done in `config/initializers/sandboxy.rb`:
|
143
|
-
|
144
|
-
```ruby
|
145
|
-
Sandboxy.configure do |config|
|
146
|
-
|
147
|
-
end
|
148
|
-
```
|
149
|
-
|
150
|
-
**`default`** Set your environment default. This is the environment that your app boots with. By default it gets refreshed with every new request to your server. Takes a string. Defaults to `'live'`.
|
151
|
-
|
152
|
-
**`retain`** Retain your current app environment on new requests. If set to `false`, your app will return to your default environment on every new request. Takes a boolean. Defaults to `false`.
|
153
|
-
|
154
|
-
---
|
155
|
-
|
156
|
-
## Testing
|
157
|
-
|
158
|
-
Tests are written with Shoulda on top of `Test::Unit` with Factory Girl being used instead of fixtures. Tests are run using rake.
|
159
|
-
|
160
|
-
1. Fork this repository
|
161
|
-
2. Clone your forked git locally
|
162
|
-
3. Install dependencies
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
4. Run tests
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
### Test Coverage
|
171
|
-
|
172
|
-
Test coverage can be calculated using SimpleCov. Make sure you have the [simplecov gem](https://github.com/colszowka/simplecov) installed.
|
173
|
-
|
174
|
-
1. Add SimpleCov to the Gemfile
|
175
|
-
2. Uncomment the relevant section in `test/test_helper.rb`
|
176
|
-
3. Run tests
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
---
|
181
|
-
|
182
|
-
## To
|
183
|
-
|
184
|
-
[
|
185
|
-
|
186
|
-
To propose your ideas, initiate the discussion by adding a [new issue](https://github.com/jonhue/sandboxy/issues/new).
|
187
|
-
|
188
|
-
---
|
189
|
-
|
190
|
-
## Contributing
|
191
|
-
|
192
|
-
We hope that you will consider contributing to Sandboxy. Please read this short overview for some information about how to get started:
|
193
|
-
|
194
|
-
[Learn more about contributing to this repository](
|
195
|
-
|
196
|
-
###
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
https://github.com/jonhue/sandboxy/graphs/contributors
|
201
|
-
|
202
|
-
### Semantic Versioning
|
203
|
-
|
204
|
-
Sandboxy follows Semantic Versioning 2.0 as defined at http://semver.org.
|
205
|
-
|
206
|
-
## License
|
207
|
-
|
208
|
-
MIT License
|
209
|
-
|
210
|
-
Copyright (c) 2017 Jonas Hübotter
|
211
|
-
|
212
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
213
|
-
of this software and associated documentation files (the "Software"), to deal
|
214
|
-
in the Software without restriction, including without limitation the rights
|
215
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
216
|
-
copies of the Software, and to permit persons to whom the Software is
|
217
|
-
furnished to do so, subject to the following conditions:
|
218
|
-
|
219
|
-
The above copyright notice and this permission notice shall be included in all
|
220
|
-
copies or substantial portions of the Software.
|
221
|
-
|
222
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
223
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
224
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
225
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
226
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
227
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
228
|
-
SOFTWARE.
|
1
|
+
# Sandboxy
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/sandboxy.svg)](https://badge.fury.io/rb/sandboxy) ![Travis](https://travis-ci.org/jonhue/sandboxy.svg?branch=master)
|
4
|
+
|
5
|
+
Sandboxy allows you to use virtual data-oriented environments inside a Rails application while being able to switch between them at runtime. It achieves that by using a combination of Rack Middleware and ActiveRecord.
|
6
|
+
|
7
|
+
---
|
8
|
+
|
9
|
+
## Table of Contents
|
10
|
+
|
11
|
+
* [Installation](#installation)
|
12
|
+
* [Usage](#usage)
|
13
|
+
* [Setup](#setup)
|
14
|
+
* [`sandboxy` methods](#sandboxy-methods)
|
15
|
+
* [Switching environments](#switching-environments)
|
16
|
+
* [Sandbox & APIs](#sandbox--apis)
|
17
|
+
* [Configuration](#configuration)
|
18
|
+
* [Testing](#testing)
|
19
|
+
* [Test Coverage](#test-coverage)
|
20
|
+
* [To do](#to-do)
|
21
|
+
* [Contributing](#contributing)
|
22
|
+
* [Contributors](#contributors)
|
23
|
+
* [Semantic versioning](#semantic-versioning)
|
24
|
+
* [License](#license)
|
25
|
+
|
26
|
+
---
|
27
|
+
|
28
|
+
## Installation
|
29
|
+
|
30
|
+
Sandboxy works with Rails 5 onwards. You can add it to your `Gemfile` with:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
gem 'sandboxy'
|
34
|
+
```
|
35
|
+
|
36
|
+
And then execute:
|
37
|
+
|
38
|
+
$ bundle
|
39
|
+
|
40
|
+
Or install it yourself as:
|
41
|
+
|
42
|
+
$ gem install sandboxy
|
43
|
+
|
44
|
+
If you always want to be up to date fetch the latest from GitHub in your `Gemfile`:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
gem 'sandboxy', github: 'jonhue/sandboxy'
|
48
|
+
```
|
49
|
+
|
50
|
+
Now run the generator:
|
51
|
+
|
52
|
+
$ rails g sandboxy
|
53
|
+
|
54
|
+
To wrap things up, migrate the changes into your database:
|
55
|
+
|
56
|
+
$ rails db:migrate
|
57
|
+
|
58
|
+
## Usage
|
59
|
+
|
60
|
+
### Setup
|
61
|
+
|
62
|
+
Add Sandboxy to the models where you want to separate records depending on their environments:
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
class Foo < ApplicationRecord
|
66
|
+
sandboxy
|
67
|
+
end
|
68
|
+
```
|
69
|
+
|
70
|
+
In most use cases you would want to add `sandboxy` to a lot of ActiveRecord models if not all. To simplify that you could create a new class and let all your models inherit from it:
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
class SharedSandbox < ApplicationRecord
|
74
|
+
self.abstract_class = true
|
75
|
+
sandboxy
|
76
|
+
end
|
77
|
+
|
78
|
+
class Foo < SharedSandbox
|
79
|
+
end
|
80
|
+
```
|
81
|
+
|
82
|
+
### `sandboxy` methods
|
83
|
+
|
84
|
+
By default you can only access records belonging to the current environment (defined by `Sandboxy.environment`):
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
Sandboxy.environment = 'test'
|
88
|
+
Foo.all # => returns all test foo's
|
89
|
+
Sandboxy.environment = 'sandbox'
|
90
|
+
Foo.all # => returns all sandbox foo's
|
91
|
+
```
|
92
|
+
|
93
|
+
Now to access the records belonging to a certain environment regardless of your current environment, you can use:
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
Foo.live_environment # => returns all live foo's
|
97
|
+
Foo.sandbox_environment # => returns all sandbox foo's
|
98
|
+
Foo.desandbox # => returns all foo's
|
99
|
+
```
|
100
|
+
|
101
|
+
Let's check to which environment this `Foo` belongs:
|
102
|
+
|
103
|
+
```ruby
|
104
|
+
foo = Foo.create!
|
105
|
+
foo.live_environment? # => false
|
106
|
+
foo.sandbox_environment? # => true
|
107
|
+
```
|
108
|
+
|
109
|
+
You should keep in mind that when you create a new record, it will automatically belong to your app's current environment.
|
110
|
+
|
111
|
+
Don't worry, you can move records between environments:
|
112
|
+
|
113
|
+
```ruby
|
114
|
+
foo.move_environment_live
|
115
|
+
foo.live_environment? # => true
|
116
|
+
foo.move_environment_sandbox
|
117
|
+
foo.sandbox_environment? # => true
|
118
|
+
```
|
119
|
+
|
120
|
+
### Switching environments
|
121
|
+
|
122
|
+
At runtime you can always switch environments anywhere in your application by setting `Sandboxy.environment`. You can set it to any string you like. That makes Sandboxy super flexible.
|
123
|
+
|
124
|
+
```ruby
|
125
|
+
Sandboxy.environment = 'live'
|
126
|
+
Sandboxy.live_environment? # => true
|
127
|
+
Sandboxy.sandbox_environment? # => false
|
128
|
+
```
|
129
|
+
|
130
|
+
#### Sandbox & APIs
|
131
|
+
|
132
|
+
It's flexibility allows Sandboxy to work really well with APIs.
|
133
|
+
|
134
|
+
Typically an API provides two sets of authentication credentials for a consumer - one for live access and one for sandbox/testing.
|
135
|
+
|
136
|
+
Whenever you authenticate your API's consumer, just make sure to set `Sandboxy.environment` accordingly to the credential the consumer used. From thereon, Sandboxy will make sure that your consumer only reads & updates data from the environment he is in.
|
137
|
+
|
138
|
+
---
|
139
|
+
|
140
|
+
## Configuration
|
141
|
+
|
142
|
+
You can configure Sandboxy by passing a block to `configure`. This can be done in `config/initializers/sandboxy.rb`:
|
143
|
+
|
144
|
+
```ruby
|
145
|
+
Sandboxy.configure do |config|
|
146
|
+
config.default = 'sandbox'
|
147
|
+
end
|
148
|
+
```
|
149
|
+
|
150
|
+
**`default`** Set your environment default. This is the environment that your app boots with. By default it gets refreshed with every new request to your server. Takes a string. Defaults to `'live'`.
|
151
|
+
|
152
|
+
**`retain`** Retain your current app environment on new requests. If set to `false`, your app will return to your default environment on every new request. Takes a boolean. Defaults to `false`.
|
153
|
+
|
154
|
+
---
|
155
|
+
|
156
|
+
## Testing
|
157
|
+
|
158
|
+
Tests are written with Shoulda on top of `Test::Unit` with Factory Girl being used instead of fixtures. Tests are run using rake.
|
159
|
+
|
160
|
+
1. Fork this repository
|
161
|
+
2. Clone your forked git locally
|
162
|
+
3. Install dependencies
|
163
|
+
|
164
|
+
$ bundle install
|
165
|
+
|
166
|
+
4. Run tests
|
167
|
+
|
168
|
+
$ rake test
|
169
|
+
|
170
|
+
### Test Coverage
|
171
|
+
|
172
|
+
Test coverage can be calculated using SimpleCov. Make sure you have the [simplecov gem](https://github.com/colszowka/simplecov) installed.
|
173
|
+
|
174
|
+
1. Add SimpleCov to the Gemfile
|
175
|
+
2. Uncomment the relevant section in `test/test_helper.rb`
|
176
|
+
3. Run tests
|
177
|
+
|
178
|
+
$ rake test
|
179
|
+
|
180
|
+
---
|
181
|
+
|
182
|
+
## To do
|
183
|
+
|
184
|
+
We use [GitHub projects](https://github.com/jonhue/sandboxy/projects/1) to coordinate the work on this project.
|
185
|
+
|
186
|
+
To propose your ideas, initiate the discussion by adding a [new issue](https://github.com/jonhue/sandboxy/issues/new).
|
187
|
+
|
188
|
+
---
|
189
|
+
|
190
|
+
## Contributing
|
191
|
+
|
192
|
+
We hope that you will consider contributing to Sandboxy. Please read this short overview for some information about how to get started:
|
193
|
+
|
194
|
+
[Learn more about contributing to this repository](CONTRIBUTING.md), [Code of Conduct](CODE_OF_CONDUCT.md)
|
195
|
+
|
196
|
+
### Semantic Versioning
|
197
|
+
|
198
|
+
Sandboxy follows Semantic Versioning 2.0 as defined at http://semver.org.
|
data/app/models/sandbox.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Sandbox < ApplicationRecord
|
4
|
+
self.table_name = 'sandboxy'
|
5
|
+
|
6
|
+
belongs_to :sandboxed, polymorphic: true
|
7
|
+
end
|
@@ -1,39 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'rails/generators'
|
2
4
|
require 'rails/generators/migration'
|
3
5
|
|
4
6
|
class SandboxyGenerator < Rails::Generators::Base
|
7
|
+
include Rails::Generators::Migration
|
5
8
|
|
6
|
-
|
7
|
-
|
8
|
-
source_root File.join File.dirname(__FILE__), 'templates'
|
9
|
-
desc 'Install sandboxy'
|
9
|
+
source_root File.join File.dirname(__FILE__), 'templates'
|
10
|
+
desc 'Install sandboxy'
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
end
|
12
|
+
def self.next_migration_number(dirname)
|
13
|
+
if ActiveRecord::Base.timestamped_migrations
|
14
|
+
Time.now.utc.strftime('%Y%m%d%H%M%S')
|
15
|
+
else
|
16
|
+
format('%.3d', current_migration_number(dirname) + 1)
|
17
17
|
end
|
18
|
+
end
|
18
19
|
|
19
|
-
|
20
|
-
|
21
|
-
|
20
|
+
def create_initializer
|
21
|
+
template 'initializer.rb', 'config/initializers/sandboxy.rb'
|
22
|
+
end
|
22
23
|
|
23
|
-
|
24
|
-
|
25
|
-
|
24
|
+
def create_migration_file
|
25
|
+
migration_template 'migration.rb.erb', 'db/migrate/sandboxy_migration.rb',
|
26
|
+
migration_version: migration_version
|
27
|
+
end
|
26
28
|
|
27
|
-
|
28
|
-
readme 'README.md'
|
29
|
-
end
|
30
|
-
|
31
|
-
private
|
29
|
+
private
|
32
30
|
|
33
|
-
|
34
|
-
|
35
|
-
"[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
|
36
|
-
end
|
37
|
-
end
|
31
|
+
def migration_version
|
32
|
+
return unless Rails.version >= '5.0.0'
|
38
33
|
|
34
|
+
"[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
|
35
|
+
end
|
39
36
|
end
|
@@ -1,13 +1,13 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
Sandboxy.configure do |config|
|
4
|
+
# Set your environment default. This is the environment that your app boots
|
5
|
+
# with. By default it gets refreshed with every new request to your server.
|
6
|
+
# Takes a string.
|
7
|
+
# config.default = 'live'
|
8
|
+
|
9
|
+
# Retain your current app environment on new requests. If set to `false`, your
|
10
|
+
# app will return to your default environment on every new request. Takes a
|
11
|
+
# boolean.
|
12
|
+
# config.retain = false
|
13
|
+
end
|
@@ -1,9 +1,9 @@
|
|
1
1
|
class SandboxyMigration < ActiveRecord::Migration<%= migration_version %>
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
end
|
2
|
+
def change
|
3
|
+
create_table :sandboxy do |t|
|
4
|
+
t.references :sandboxed, polymorphic: true, index: true
|
5
|
+
t.string :environment, index: true
|
6
|
+
t.timestamps
|
8
7
|
end
|
8
|
+
end
|
9
9
|
end
|
data/lib/sandboxy.rb
CHANGED
@@ -1,14 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'sandboxy/version'
|
2
4
|
|
3
5
|
module Sandboxy
|
6
|
+
require 'sandboxy/configuration'
|
4
7
|
|
5
|
-
|
6
|
-
|
7
|
-
require 'sandboxy/engine'
|
8
|
-
|
9
|
-
autoload :Sandboxed, 'sandboxy/sandboxed'
|
8
|
+
require 'sandboxy/engine'
|
10
9
|
|
11
|
-
|
12
|
-
require 'sandboxy/railtie'
|
10
|
+
autoload :Sandboxed, 'sandboxy/sandboxed'
|
13
11
|
|
12
|
+
require 'sandboxy/middleware'
|
13
|
+
require 'sandboxy/railtie'
|
14
14
|
end
|
@@ -1,48 +1,47 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
end
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Sandboxy
|
4
|
+
class << self
|
5
|
+
attr_accessor :configuration
|
6
|
+
|
7
|
+
def environment
|
8
|
+
$sandboxy ||= Sandboxy.configuration.default
|
9
|
+
$sandboxy
|
10
|
+
end
|
11
|
+
|
12
|
+
def environment=(value)
|
13
|
+
$sandboxy = value
|
14
|
+
end
|
15
|
+
|
16
|
+
def method_missing(method, *args)
|
17
|
+
if method.to_s[/(.+)_environment?/]
|
18
|
+
environment?($1)
|
19
|
+
else
|
20
|
+
super
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def respond_to_missing?(method, include_private = false)
|
25
|
+
super || method.to_s[/(.+)_environment?/]
|
26
|
+
end
|
27
|
+
|
28
|
+
def environment?(value)
|
29
|
+
environment == value
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.configure
|
34
|
+
self.configuration ||= Configuration.new
|
35
|
+
yield configuration
|
36
|
+
end
|
37
|
+
|
38
|
+
class Configuration
|
39
|
+
attr_accessor :default
|
40
|
+
attr_accessor :retain
|
41
|
+
|
42
|
+
def initialize
|
43
|
+
@default = 'live'
|
44
|
+
@retain = false
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/lib/sandboxy/engine.rb
CHANGED
data/lib/sandboxy/middleware.rb
CHANGED
@@ -1,18 +1,20 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Sandboxy
|
4
|
+
class Middleware
|
5
|
+
def initialize(app)
|
6
|
+
@app = app
|
7
|
+
end
|
8
|
+
|
9
|
+
def call(_env)
|
10
|
+
require 'sandboxy'
|
11
|
+
|
12
|
+
previous_environment = Sandboxy.environment
|
13
|
+
$sandboxy = nil
|
14
|
+
|
15
|
+
return unless Sandboxy.environment != previous_environment
|
16
|
+
|
17
|
+
puts "Sandboxy: Moved to #{Sandboxy.configuration.default} environment"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/sandboxy/railtie.rb
CHANGED
@@ -1,23 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'rails/railtie'
|
2
4
|
require 'active_record'
|
3
5
|
|
4
|
-
|
5
6
|
module Sandboxy
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
end
|
13
|
-
|
14
|
-
initializer 'sandboxy.middleware' do |app|
|
15
|
-
app.middleware.use(Sandboxy::Middleware) unless Sandboxy.configuration&.retain
|
16
|
-
end
|
7
|
+
class Railtie < Rails::Railtie
|
8
|
+
initializer 'sandboxy.active_record' do
|
9
|
+
ActiveSupport.on_load :active_record do
|
10
|
+
include Sandboxy::Sandboxed
|
11
|
+
end
|
12
|
+
end
|
17
13
|
|
18
|
-
|
19
|
-
|
20
|
-
|
14
|
+
initializer 'sandboxy.middleware' do |app|
|
15
|
+
unless Sandboxy.configuration&.retain
|
16
|
+
app.middleware.use(Sandboxy::Middleware)
|
17
|
+
end
|
18
|
+
end
|
21
19
|
|
20
|
+
config.after_initialize do
|
21
|
+
puts "Sandboxy: Using #{Sandboxy.configuration.default} environment"
|
22
22
|
end
|
23
|
+
end
|
23
24
|
end
|
data/lib/sandboxy/sandboxed.rb
CHANGED
@@ -1,102 +1,114 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Sandboxy
|
2
|
-
|
4
|
+
module Sandboxed
|
5
|
+
def self.included(base)
|
6
|
+
base.extend(ClassMethods)
|
7
|
+
end
|
3
8
|
|
4
|
-
|
5
|
-
|
6
|
-
|
9
|
+
module ClassMethods
|
10
|
+
def sandboxy
|
11
|
+
has_one :sandbox, as: :sandboxed, dependent: :destroy
|
12
|
+
before_create :set_environment
|
13
|
+
include Sandboxy::Sandboxed::SandboxyInstanceMethods
|
7
14
|
|
8
|
-
|
9
|
-
|
10
|
-
def sandboxy
|
11
|
-
has_one :sandbox, as: :sandboxed, dependent: :destroy
|
12
|
-
before_create :set_environment
|
13
|
-
include Sandboxy::Sandboxed::InstanceMethods
|
14
|
-
|
15
|
-
default_scope { self.environment_scoped(Sandboxy.environment) }
|
16
|
-
scope :desandbox, -> { unscope(:joins, :where).all }
|
17
|
-
|
18
|
-
def method_missing m, *args
|
19
|
-
if m.to_s[/(.+)_environment/]
|
20
|
-
self.environment $1
|
21
|
-
elsif m.to_s[/(.+)_environment_scoped/]
|
22
|
-
self.environment_scoped $1
|
23
|
-
else
|
24
|
-
super
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
def respond_to? m, include_private = false
|
29
|
-
super || m.to_s[/(.+)_environment/] || m.to_s[/(.+)_environment_scoped/]
|
30
|
-
end
|
31
|
-
|
32
|
-
def environment value
|
33
|
-
unscope(:joins, :where).environment_scoped value
|
34
|
-
end
|
35
|
-
|
36
|
-
def environment_scoped value
|
37
|
-
case value
|
38
|
-
when Sandboxy.configuration.default
|
39
|
-
left_outer_joins(:sandbox).where sandboxy: { environment: nil }
|
40
|
-
else
|
41
|
-
left_outer_joins(:sandbox).where sandboxy: { environment: value }
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
15
|
+
default_scope { environment_scoped(Sandboxy.environment) }
|
16
|
+
scope :desandbox, -> { unscope(:joins, :where).all }
|
45
17
|
|
18
|
+
class << self
|
19
|
+
include Sandboxy::Sandboxed::SandboxyClassMethods
|
46
20
|
end
|
21
|
+
end
|
22
|
+
end
|
47
23
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
def environment? value
|
82
|
-
self.environment == value
|
83
|
-
end
|
84
|
-
|
85
|
-
def environment
|
86
|
-
return Sandboxy.configuration.default if self.sandbox.nil?
|
87
|
-
self.sandbox.environment
|
88
|
-
end
|
89
|
-
|
90
|
-
private
|
91
|
-
|
92
|
-
def set_environment
|
93
|
-
unless Sandboxy.environment == Sandboxy.configuration.default
|
94
|
-
sandbox = self.build_sandbox
|
95
|
-
sandbox.environment = Sandboxy.environment
|
96
|
-
end
|
97
|
-
end
|
24
|
+
module SandboxyClassMethods
|
25
|
+
def method_missing(method, *args)
|
26
|
+
if method.to_s[/(.+)_environment/]
|
27
|
+
environment($1)
|
28
|
+
elsif method.to_s[/(.+)_environment_scoped/]
|
29
|
+
environment_scoped($1)
|
30
|
+
else
|
31
|
+
super
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def respond_to_missing?(method, include_private = false)
|
36
|
+
super ||
|
37
|
+
method.to_s[/(.+)_environment/] ||
|
38
|
+
method.to_s[/(.+)_environment_scoped/]
|
39
|
+
end
|
40
|
+
|
41
|
+
def environment(value)
|
42
|
+
unscope(:joins, :where).environment_scoped value
|
43
|
+
end
|
44
|
+
|
45
|
+
def environment_scoped(value)
|
46
|
+
case value
|
47
|
+
when Sandboxy.configuration.default
|
48
|
+
left_outer_joins(:sandbox).where(sandboxy: { environment: nil })
|
49
|
+
else
|
50
|
+
left_outer_joins(:sandbox).where(sandboxy: { environment: value })
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
98
54
|
|
55
|
+
module SandboxyInstanceMethods
|
56
|
+
def method_missing(method, *args)
|
57
|
+
if method.to_s[/move_environment_(.+)/]
|
58
|
+
move_environment($1)
|
59
|
+
elsif method.to_s[/(.+)_environment?/]
|
60
|
+
environment?($1)
|
61
|
+
else
|
62
|
+
super
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def respond_to_missing?(method, include_private = false)
|
67
|
+
super ||
|
68
|
+
method.to_s[/move_environment_(.+)/] ||
|
69
|
+
method.to_s[/(.+)_environment?/]
|
70
|
+
end
|
71
|
+
|
72
|
+
def move_environment(environment)
|
73
|
+
case environment
|
74
|
+
when Sandboxy.configuration.default
|
75
|
+
move_to_default_environment
|
76
|
+
else
|
77
|
+
move_to_custom_environment(environment)
|
99
78
|
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def environment?(value)
|
82
|
+
environment == value
|
83
|
+
end
|
84
|
+
|
85
|
+
def environment
|
86
|
+
return Sandboxy.configuration.default if sandbox.nil?
|
87
|
+
|
88
|
+
sandbox.environment
|
89
|
+
end
|
90
|
+
|
91
|
+
private
|
92
|
+
|
93
|
+
def set_environment
|
94
|
+
return if Sandboxy.environment == Sandboxy.configuration.default
|
95
|
+
|
96
|
+
sandbox = build_sandbox
|
97
|
+
sandbox.environment = Sandboxy.environment
|
98
|
+
end
|
99
|
+
|
100
|
+
def move_to_default_environment
|
101
|
+
Sandbox.where(sandboxed_id: id, sandboxed_type: self.class.name)
|
102
|
+
.destroy_all
|
103
|
+
self.sandbox = nil
|
104
|
+
save!
|
105
|
+
end
|
100
106
|
|
107
|
+
def move_to_custom_environment(environment)
|
108
|
+
sandbox ||= build_sandbox
|
109
|
+
sandbox.environment = environment
|
110
|
+
sandbox.save!
|
111
|
+
end
|
101
112
|
end
|
113
|
+
end
|
102
114
|
end
|
data/lib/sandboxy/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sandboxy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.0.
|
4
|
+
version: 3.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonas Hübotter
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-05-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -25,75 +25,73 @@ dependencies:
|
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '5.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: factory_bot
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: rubocop
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - "
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - "
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
54
|
+
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: shoulda
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- - "
|
59
|
+
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
61
|
+
version: '0'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- - "
|
66
|
+
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '
|
68
|
+
version: '0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
70
|
+
name: sqlite3
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- - "
|
73
|
+
- - ">="
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: '
|
75
|
+
version: '0'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- - "
|
80
|
+
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: '
|
83
|
-
description: Sandboxy allows you to use
|
84
|
-
|
85
|
-
|
86
|
-
email:
|
82
|
+
version: '0'
|
83
|
+
description: Sandboxy allows you to use virtualdata-oriented environments inside a
|
84
|
+
Railsapplication while being able to switch inbetween at runtime. It achieves that
|
85
|
+
by using acombination of Rack Middleware and ActiveRecord.
|
86
|
+
email: me@jonhue.me
|
87
87
|
executables: []
|
88
88
|
extensions: []
|
89
89
|
extra_rdoc_files: []
|
90
90
|
files:
|
91
|
-
- CHANGELOG.md
|
92
91
|
- LICENSE
|
93
92
|
- README.md
|
94
93
|
- app/models/sandbox.rb
|
95
94
|
- lib/generators/sandboxy_generator.rb
|
96
|
-
- lib/generators/templates/README.md
|
97
95
|
- lib/generators/templates/initializer.rb
|
98
96
|
- lib/generators/templates/migration.rb.erb
|
99
97
|
- lib/sandboxy.rb
|
@@ -107,10 +105,7 @@ homepage: https://github.com/jonhue/sandboxy
|
|
107
105
|
licenses:
|
108
106
|
- MIT
|
109
107
|
metadata: {}
|
110
|
-
post_install_message:
|
111
|
-
**Thank you for installing Sandboxy!**
|
112
|
-
Get started by running `rails g sandboxy`.
|
113
|
-
Learn more at https://github.com/jonhue/sandboxy.
|
108
|
+
post_install_message:
|
114
109
|
rdoc_options: []
|
115
110
|
require_paths:
|
116
111
|
- lib
|
@@ -118,15 +113,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
118
113
|
requirements:
|
119
114
|
- - ">="
|
120
115
|
- !ruby/object:Gem::Version
|
121
|
-
version:
|
116
|
+
version: 2.3.0
|
122
117
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
123
118
|
requirements:
|
124
119
|
- - ">="
|
125
120
|
- !ruby/object:Gem::Version
|
126
121
|
version: '0'
|
127
122
|
requirements: []
|
128
|
-
|
129
|
-
rubygems_version: 2.7.3
|
123
|
+
rubygems_version: 3.0.3
|
130
124
|
signing_key:
|
131
125
|
specification_version: 4
|
132
126
|
summary: Virtual data-oriented environments for Rails
|
data/CHANGELOG.md
DELETED
@@ -1,32 +0,0 @@
|
|
1
|
-
# Changelog
|
2
|
-
|
3
|
-
### master
|
4
|
-
|
5
|
-
* nothing yet
|
6
|
-
|
7
|
-
### 3.0.0 - 2017-12-29
|
8
|
-
|
9
|
-
* features
|
10
|
-
* move `Sandbox` model into engine
|
11
|
-
* add support for multiple environments
|
12
|
-
* add `environment` attribute to `Sandbox` instances
|
13
|
-
|
14
|
-
### 2.0.0 - 2017-12-21
|
15
|
-
|
16
|
-
* features
|
17
|
-
* configuration by passing a block to `configure`
|
18
|
-
* generator generates initializer instead of yaml file
|
19
|
-
|
20
|
-
### 1.1.1 - 2017-08-27
|
21
|
-
|
22
|
-
* bugfixes
|
23
|
-
* fix undefined method `key` for `false`:Class
|
24
|
-
|
25
|
-
### 1.1.0 - 2017-08-27
|
26
|
-
|
27
|
-
* features
|
28
|
-
* use rack middleware to refresh environment defaults on new requests
|
29
|
-
|
30
|
-
### 1.0.0 - 2017-08-27
|
31
|
-
|
32
|
-
* initial release
|
@@ -1 +0,0 @@
|
|
1
|
-
Now run `rails db:migrate` to add sandboxy to your database.
|