sandboxy 2.0.0 → 3.0.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 +7 -0
- data/README.md +26 -36
- data/{examples/rails_example/app → app}/models/sandbox.rb +1 -1
- data/lib/generators/sandboxy_generator.rb +0 -4
- data/lib/generators/templates/initializer.rb +7 -7
- data/lib/generators/templates/migration.rb.erb +4 -9
- data/lib/sandboxy.rb +2 -9
- data/lib/sandboxy/configuration.rb +37 -10
- data/lib/sandboxy/engine.rb +6 -0
- data/lib/sandboxy/middleware.rb +3 -3
- data/lib/sandboxy/railtie.rb +7 -6
- data/lib/sandboxy/sandboxed.rb +69 -19
- data/lib/sandboxy/version.rb +1 -1
- metadata +5 -197
- data/.github/issue_template.md +0 -14
- data/.github/pull_request_template.md +0 -21
- data/.gitignore +0 -8
- data/.travis.yml +0 -3
- data/CODE_OF_CONDUCT.md +0 -46
- data/CONTRIBUTING.md +0 -1
- data/DEPRECATIONS.md +0 -5
- data/Gemfile +0 -9
- data/INSTALL.md +0 -3
- data/Rakefile +0 -15
- data/examples/rails_example/.gitignore +0 -23
- data/examples/rails_example/Gemfile +0 -54
- data/examples/rails_example/README.md +0 -24
- data/examples/rails_example/Rakefile +0 -6
- data/examples/rails_example/app/api/books/api.rb +0 -33
- data/examples/rails_example/app/assets/config/manifest.js +0 -3
- data/examples/rails_example/app/assets/images/.keep +0 -0
- data/examples/rails_example/app/assets/javascripts/application.js +0 -15
- data/examples/rails_example/app/assets/javascripts/applications.coffee +0 -3
- data/examples/rails_example/app/assets/javascripts/authors.coffee +0 -3
- data/examples/rails_example/app/assets/javascripts/books.coffee +0 -3
- data/examples/rails_example/app/assets/javascripts/cable.js +0 -13
- data/examples/rails_example/app/assets/javascripts/channels/.keep +0 -0
- data/examples/rails_example/app/assets/javascripts/libraries.coffee +0 -3
- data/examples/rails_example/app/assets/javascripts/sandbox.coffee +0 -3
- data/examples/rails_example/app/assets/stylesheets/application.css +0 -15
- data/examples/rails_example/app/assets/stylesheets/applications.scss +0 -3
- data/examples/rails_example/app/assets/stylesheets/authors.scss +0 -3
- data/examples/rails_example/app/assets/stylesheets/books.scss +0 -3
- data/examples/rails_example/app/assets/stylesheets/libraries.scss +0 -3
- data/examples/rails_example/app/assets/stylesheets/sandbox.scss +0 -3
- data/examples/rails_example/app/assets/stylesheets/scaffolds.scss +0 -84
- data/examples/rails_example/app/channels/application_cable/channel.rb +0 -4
- data/examples/rails_example/app/channels/application_cable/connection.rb +0 -4
- data/examples/rails_example/app/controllers/application_controller.rb +0 -7
- data/examples/rails_example/app/controllers/applications_controller.rb +0 -74
- data/examples/rails_example/app/controllers/authors_controller.rb +0 -77
- data/examples/rails_example/app/controllers/books_controller.rb +0 -77
- data/examples/rails_example/app/controllers/concerns/.keep +0 -0
- data/examples/rails_example/app/controllers/libraries_controller.rb +0 -77
- data/examples/rails_example/app/controllers/sandbox_controller.rb +0 -6
- data/examples/rails_example/app/helpers/application_helper.rb +0 -2
- data/examples/rails_example/app/helpers/applications_helper.rb +0 -2
- data/examples/rails_example/app/helpers/authors_helper.rb +0 -2
- data/examples/rails_example/app/helpers/books_helper.rb +0 -2
- data/examples/rails_example/app/helpers/libraries_helper.rb +0 -2
- data/examples/rails_example/app/helpers/sandbox_helper.rb +0 -2
- data/examples/rails_example/app/jobs/application_job.rb +0 -2
- data/examples/rails_example/app/mailers/application_mailer.rb +0 -4
- data/examples/rails_example/app/models/application.rb +0 -14
- data/examples/rails_example/app/models/application_record.rb +0 -3
- data/examples/rails_example/app/models/author.rb +0 -2
- data/examples/rails_example/app/models/book.rb +0 -2
- data/examples/rails_example/app/models/concerns/.keep +0 -0
- data/examples/rails_example/app/models/library.rb +0 -2
- data/examples/rails_example/app/views/applications/_application.json.jbuilder +0 -2
- data/examples/rails_example/app/views/applications/_form.html.erb +0 -19
- data/examples/rails_example/app/views/applications/edit.html.erb +0 -6
- data/examples/rails_example/app/views/applications/index.html.erb +0 -25
- data/examples/rails_example/app/views/applications/index.json.jbuilder +0 -1
- data/examples/rails_example/app/views/applications/new.html.erb +0 -5
- data/examples/rails_example/app/views/applications/show.html.erb +0 -7
- data/examples/rails_example/app/views/applications/show.json.jbuilder +0 -1
- data/examples/rails_example/app/views/authors/_author.json.jbuilder +0 -2
- data/examples/rails_example/app/views/authors/_form.html.erb +0 -19
- data/examples/rails_example/app/views/authors/edit.html.erb +0 -6
- data/examples/rails_example/app/views/authors/index.html.erb +0 -25
- data/examples/rails_example/app/views/authors/index.json.jbuilder +0 -1
- data/examples/rails_example/app/views/authors/new.html.erb +0 -5
- data/examples/rails_example/app/views/authors/show.html.erb +0 -4
- data/examples/rails_example/app/views/authors/show.json.jbuilder +0 -1
- data/examples/rails_example/app/views/books/_book.json.jbuilder +0 -2
- data/examples/rails_example/app/views/books/_form.html.erb +0 -19
- data/examples/rails_example/app/views/books/edit.html.erb +0 -6
- data/examples/rails_example/app/views/books/index.html.erb +0 -25
- data/examples/rails_example/app/views/books/index.json.jbuilder +0 -1
- data/examples/rails_example/app/views/books/new.html.erb +0 -5
- data/examples/rails_example/app/views/books/show.html.erb +0 -4
- data/examples/rails_example/app/views/books/show.json.jbuilder +0 -1
- data/examples/rails_example/app/views/layouts/application.html.erb +0 -14
- data/examples/rails_example/app/views/layouts/mailer.html.erb +0 -13
- data/examples/rails_example/app/views/layouts/mailer.text.erb +0 -1
- data/examples/rails_example/app/views/libraries/_form.html.erb +0 -19
- data/examples/rails_example/app/views/libraries/_library.json.jbuilder +0 -2
- data/examples/rails_example/app/views/libraries/edit.html.erb +0 -6
- data/examples/rails_example/app/views/libraries/index.html.erb +0 -25
- data/examples/rails_example/app/views/libraries/index.json.jbuilder +0 -1
- data/examples/rails_example/app/views/libraries/new.html.erb +0 -5
- data/examples/rails_example/app/views/libraries/show.html.erb +0 -4
- data/examples/rails_example/app/views/libraries/show.json.jbuilder +0 -1
- data/examples/rails_example/bin/bundle +0 -3
- data/examples/rails_example/bin/rails +0 -4
- data/examples/rails_example/bin/rake +0 -4
- data/examples/rails_example/bin/setup +0 -38
- data/examples/rails_example/bin/update +0 -29
- data/examples/rails_example/bin/yarn +0 -11
- data/examples/rails_example/config.ru +0 -5
- data/examples/rails_example/config/application.rb +0 -18
- data/examples/rails_example/config/boot.rb +0 -3
- data/examples/rails_example/config/cable.yml +0 -10
- data/examples/rails_example/config/database.yml +0 -25
- data/examples/rails_example/config/environment.rb +0 -5
- data/examples/rails_example/config/environments/development.rb +0 -54
- data/examples/rails_example/config/environments/production.rb +0 -91
- data/examples/rails_example/config/environments/test.rb +0 -42
- data/examples/rails_example/config/initializers/application_controller_renderer.rb +0 -6
- data/examples/rails_example/config/initializers/assets.rb +0 -14
- data/examples/rails_example/config/initializers/backtrace_silencers.rb +0 -7
- data/examples/rails_example/config/initializers/cookies_serializer.rb +0 -5
- data/examples/rails_example/config/initializers/filter_parameter_logging.rb +0 -4
- data/examples/rails_example/config/initializers/grape.rb +0 -2
- data/examples/rails_example/config/initializers/inflections.rb +0 -16
- data/examples/rails_example/config/initializers/mime_types.rb +0 -4
- data/examples/rails_example/config/initializers/sandboxy.rb +0 -13
- data/examples/rails_example/config/initializers/wrap_parameters.rb +0 -14
- data/examples/rails_example/config/locales/en.yml +0 -33
- data/examples/rails_example/config/puma.rb +0 -56
- data/examples/rails_example/config/routes.rb +0 -13
- data/examples/rails_example/config/secrets.yml +0 -32
- data/examples/rails_example/db/migrate/20170827184048_create_books.rb +0 -9
- data/examples/rails_example/db/migrate/20170827184054_create_authors.rb +0 -9
- data/examples/rails_example/db/migrate/20170827184106_create_libraries.rb +0 -9
- data/examples/rails_example/db/migrate/20170827184304_create_applications.rb +0 -11
- data/examples/rails_example/db/migrate/20170827190815_sandboxy_migration.rb +0 -14
- data/examples/rails_example/db/schema.rb +0 -50
- data/examples/rails_example/db/seeds.rb +0 -7
- data/examples/rails_example/lib/assets/.keep +0 -0
- data/examples/rails_example/lib/tasks/.keep +0 -0
- data/examples/rails_example/package.json +0 -5
- data/examples/rails_example/public/404.html +0 -67
- data/examples/rails_example/public/422.html +0 -67
- data/examples/rails_example/public/500.html +0 -66
- data/examples/rails_example/public/apple-touch-icon-precomposed.png +0 -0
- data/examples/rails_example/public/apple-touch-icon.png +0 -0
- data/examples/rails_example/public/favicon.ico +0 -0
- data/examples/rails_example/public/robots.txt +0 -1
- data/examples/rails_example/test/application_system_test_case.rb +0 -5
- data/examples/rails_example/test/controllers/.keep +0 -0
- data/examples/rails_example/test/controllers/applications_controller_test.rb +0 -48
- data/examples/rails_example/test/controllers/authors_controller_test.rb +0 -48
- data/examples/rails_example/test/controllers/books_controller_test.rb +0 -48
- data/examples/rails_example/test/controllers/libraries_controller_test.rb +0 -48
- data/examples/rails_example/test/controllers/sandbox_controller_test.rb +0 -7
- data/examples/rails_example/test/fixtures/.keep +0 -0
- data/examples/rails_example/test/fixtures/applications.yml +0 -11
- data/examples/rails_example/test/fixtures/authors.yml +0 -11
- data/examples/rails_example/test/fixtures/books.yml +0 -11
- data/examples/rails_example/test/fixtures/files/.keep +0 -0
- data/examples/rails_example/test/fixtures/libraries.yml +0 -11
- data/examples/rails_example/test/helpers/.keep +0 -0
- data/examples/rails_example/test/integration/.keep +0 -0
- data/examples/rails_example/test/mailers/.keep +0 -0
- data/examples/rails_example/test/models/.keep +0 -0
- data/examples/rails_example/test/models/application_test.rb +0 -7
- data/examples/rails_example/test/models/author_test.rb +0 -7
- data/examples/rails_example/test/models/book_test.rb +0 -7
- data/examples/rails_example/test/models/library_test.rb +0 -7
- data/examples/rails_example/test/system/.keep +0 -0
- data/examples/rails_example/test/system/applications_test.rb +0 -9
- data/examples/rails_example/test/system/authors_test.rb +0 -9
- data/examples/rails_example/test/system/books_test.rb +0 -9
- data/examples/rails_example/test/system/libraries_test.rb +0 -9
- data/examples/rails_example/test/test_helper.rb +0 -9
- data/examples/rails_example/tmp/.keep +0 -0
- data/examples/rails_example/vendor/.keep +0 -0
- data/init.rb +0 -1
- data/lib/generators/templates/model.rb +0 -7
- data/sandboxy.gemspec +0 -28
- data/test/dummy30/Gemfile +0 -1
- data/test/dummy30/Rakefile +0 -4
- data/test/dummy30/app/models/application_record.rb +0 -3
- data/test/dummy30/app/models/shared_sandbox.rb +0 -4
- data/test/dummy30/app/models/some.rb +0 -3
- data/test/dummy30/app/models/user.rb +0 -3
- data/test/dummy30/config.ru +0 -2
- data/test/dummy30/config/application.rb +0 -14
- data/test/dummy30/config/boot.rb +0 -10
- data/test/dummy30/config/database.yml +0 -6
- data/test/dummy30/config/environment.rb +0 -5
- data/test/dummy30/config/environments/development.rb +0 -7
- data/test/dummy30/config/environments/test.rb +0 -6
- data/test/dummy30/config/initializers/sandboxy.rb +0 -13
- data/test/dummy30/config/initializers/secret_token.rb +0 -1
- data/test/dummy30/config/initializers/session_store.rb +0 -1
- data/test/dummy30/config/locales/en.yml +0 -2
- data/test/dummy30/config/routes.rb +0 -2
- data/test/dummy30/config/sandboxy.yml +0 -1
- data/test/factories/somes.rb +0 -17
- data/test/factories/users.rb +0 -5
- data/test/sandbox_test.rb +0 -51
- data/test/sandboxed_test.rb +0 -58
- data/test/schema.rb +0 -18
- data/test/test_helper.rb +0 -20
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 179b0c17257a3a969fee06ad285d9e832a5f4b90adfbeb3e1b8bd2c8eef0a45b
|
4
|
+
data.tar.gz: e6eb6c261f8abee9fb55c15c4d51ccd284953ecfc3727397eaaf1519f1fed29b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 43b75cf5aa42131b4bca6ae0d9c2b3bfaa90d6b88453de2d75401b6c28a27da69b8589f6cbebdd55dba68ddcdb9e7f79acd1f4704a6826f4f15905505e7a8436
|
7
|
+
data.tar.gz: 050d0c42b7b47bffbc1161b38d184ba315d7bbd2431fefb5efc53dea25d1a25fb0923937cec88bdbd83e74b888b2a021a2308399ca8a5218d3c943fc2bbd5ba9
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -12,7 +12,6 @@ Sandboxy allows you to use virtual data-oriented environments inside a Rails app
|
|
12
12
|
* [Usage](#usage)
|
13
13
|
* [Setup](#setup)
|
14
14
|
* [`sandboxy` methods](#sandboxy-methods)
|
15
|
-
* [Sandboxy class methods](#sandboxy-class-methods)
|
16
15
|
* [Switching environments](#switching-environments)
|
17
16
|
* [Sandbox & APIs](#sandbox--apis)
|
18
17
|
* [Configuration](#configuration)
|
@@ -28,7 +27,7 @@ Sandboxy allows you to use virtual data-oriented environments inside a Rails app
|
|
28
27
|
|
29
28
|
## Installation
|
30
29
|
|
31
|
-
Sandboxy works with Rails 5
|
30
|
+
Sandboxy works with Rails 5 onwards. You can add it to your `Gemfile` with:
|
32
31
|
|
33
32
|
```ruby
|
34
33
|
gem 'sandboxy'
|
@@ -60,7 +59,7 @@ To wrap things up, migrate the changes into your database:
|
|
60
59
|
|
61
60
|
### Setup
|
62
61
|
|
63
|
-
Add Sandboxy to the models where you want to separate
|
62
|
+
Add Sandboxy to the models where you want to separate records depending on their environments:
|
64
63
|
|
65
64
|
```ruby
|
66
65
|
class Foo < ApplicationRecord
|
@@ -72,6 +71,7 @@ In most use cases you would want to add `sandboxy` to a lot of ActiveRecord mode
|
|
72
71
|
|
73
72
|
```ruby
|
74
73
|
class SharedSandbox < ApplicationRecord
|
74
|
+
self.abstract_class = true
|
75
75
|
sandboxy
|
76
76
|
end
|
77
77
|
|
@@ -81,18 +81,20 @@ end
|
|
81
81
|
|
82
82
|
### `sandboxy` methods
|
83
83
|
|
84
|
-
By default you can only access records belonging to the current environment (
|
84
|
+
By default you can only access records belonging to the current environment (defined by `Sandboxy.environment`):
|
85
85
|
|
86
86
|
```ruby
|
87
|
-
|
87
|
+
Sandboxy.environment = 'test'
|
88
|
+
Foo.all # => returns all test foo's
|
89
|
+
Sandboxy.environment = 'sandbox'
|
88
90
|
Foo.all # => returns all sandbox foo's
|
89
91
|
```
|
90
92
|
|
91
|
-
Now to access the records belonging to a certain
|
93
|
+
Now to access the records belonging to a certain environment regardless of your current environment, you can use:
|
92
94
|
|
93
95
|
```ruby
|
94
|
-
Foo.
|
95
|
-
Foo.
|
96
|
+
Foo.live_environment # => returns all live foo's
|
97
|
+
Foo.sandbox_environment # => returns all sandbox foo's
|
96
98
|
Foo.desandbox # => returns all foo's
|
97
99
|
```
|
98
100
|
|
@@ -100,8 +102,8 @@ Let's check to which environment this `Foo` belongs:
|
|
100
102
|
|
101
103
|
```ruby
|
102
104
|
foo = Foo.create!
|
103
|
-
foo.
|
104
|
-
foo.
|
105
|
+
foo.live_environment? # => false
|
106
|
+
foo.sandbox_environment? # => true
|
105
107
|
```
|
106
108
|
|
107
109
|
You should keep in mind that when you create a new record, it will automatically belong to your app's current environment.
|
@@ -109,41 +111,29 @@ You should keep in mind that when you create a new record, it will automatically
|
|
109
111
|
Don't worry, you can move records between environments:
|
110
112
|
|
111
113
|
```ruby
|
112
|
-
foo.
|
113
|
-
foo.
|
114
|
-
foo.
|
115
|
-
foo.
|
114
|
+
foo.move_environment_live
|
115
|
+
foo.live_environment? # => true
|
116
|
+
foo.move_environment_sandbox
|
117
|
+
foo.sandbox_environment? # => true
|
116
118
|
```
|
117
119
|
|
118
|
-
###
|
119
|
-
|
120
|
-
To access your default environment setting:
|
121
|
-
|
122
|
-
```ruby
|
123
|
-
Sandboxy.configuration.environment # => 'live' / 'sandbox'
|
124
|
-
Sandboxy.configuration.sandbox? # => true / false
|
125
|
-
Sandboxy.configuration.live? # => true / false
|
126
|
-
```
|
127
|
-
|
128
|
-
**Note:** `Sandboxy.configuration.environment` does *NOT* return the apps current environment. For that use the [`$sandbox` variable](#switching-environments) instead.
|
120
|
+
### Switching environments
|
129
121
|
|
130
|
-
|
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.
|
131
123
|
|
132
124
|
```ruby
|
133
|
-
Sandboxy.
|
125
|
+
Sandboxy.environment = 'live'
|
126
|
+
Sandboxy.live_environment? # => true
|
127
|
+
Sandboxy.sandbox_environment? # => true
|
134
128
|
```
|
135
129
|
|
136
|
-
### Switching environments
|
137
|
-
|
138
|
-
At runtime you can always switch environments by using the `$sandbox` variable anywhere in your application. Set it to `true` to enable the `sandbox` environment. Set it to `false` to enable the `live` environment. That makes Sandboxy super flexible.
|
139
|
-
|
140
130
|
#### Sandbox & APIs
|
141
131
|
|
142
132
|
It's flexibility allows Sandboxy to work really well with APIs.
|
143
133
|
|
144
134
|
Typically an API provides two sets of authentication credentials for a consumer - one for live access and one for sandbox/testing.
|
145
135
|
|
146
|
-
Whenever you authenticate your API's consumer, just make sure to set
|
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.
|
147
137
|
|
148
138
|
---
|
149
139
|
|
@@ -153,13 +143,13 @@ You can configure Sandboxy by passing a block to `configure`. This can be done i
|
|
153
143
|
|
154
144
|
```ruby
|
155
145
|
Sandboxy.configure do |config|
|
156
|
-
config.
|
146
|
+
config.default = 'sandbox'
|
157
147
|
end
|
158
148
|
```
|
159
149
|
|
160
|
-
**`
|
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'`.
|
161
151
|
|
162
|
-
**`
|
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`.
|
163
153
|
|
164
154
|
---
|
165
155
|
|
@@ -181,7 +171,7 @@ Tests are written with Shoulda on top of `Test::Unit` with Factory Girl being us
|
|
181
171
|
|
182
172
|
Test coverage can be calculated using SimpleCov. Make sure you have the [simplecov gem](https://github.com/colszowka/simplecov) installed.
|
183
173
|
|
184
|
-
1.
|
174
|
+
1. Add SimpleCov to the Gemfile
|
185
175
|
2. Uncomment the relevant section in `test/test_helper.rb`
|
186
176
|
3. Run tests
|
187
177
|
|
@@ -24,10 +24,6 @@ class SandboxyGenerator < Rails::Generators::Base
|
|
24
24
|
migration_template 'migration.rb.erb', 'db/migrate/sandboxy_migration.rb', migration_version: migration_version
|
25
25
|
end
|
26
26
|
|
27
|
-
def create_model
|
28
|
-
template 'model.rb', 'app/models/sandbox.rb'
|
29
|
-
end
|
30
|
-
|
31
27
|
def show_readme
|
32
28
|
readme 'README.md'
|
33
29
|
end
|
@@ -1,13 +1,13 @@
|
|
1
1
|
Sandboxy.configure do |config|
|
2
2
|
|
3
|
-
# Set your environment default
|
4
|
-
# This is the environment that your app boots with.
|
3
|
+
# Set your environment default. This is the environment that your app boots with.
|
5
4
|
# By default it gets refreshed with every new request to your server.
|
6
|
-
#
|
5
|
+
# Takes a string.
|
6
|
+
# config.default = 'live'
|
7
7
|
|
8
|
-
#
|
9
|
-
# If set to
|
10
|
-
#
|
11
|
-
# config.
|
8
|
+
# Retain your current app environment on new requests.
|
9
|
+
# If set to `false`, your app will return to your default environment on every new request.
|
10
|
+
# Takes a boolean.
|
11
|
+
# config.retain = false
|
12
12
|
|
13
13
|
end
|
@@ -1,14 +1,9 @@
|
|
1
1
|
class SandboxyMigration < ActiveRecord::Migration<%= migration_version %>
|
2
|
-
def
|
3
|
-
create_table :sandboxy
|
4
|
-
t.references :sandboxed, polymorphic: true,
|
2
|
+
def change
|
3
|
+
create_table :sandboxy do |t|
|
4
|
+
t.references :sandboxed, polymorphic: true, index: true
|
5
|
+
t.string :environment, index: true
|
5
6
|
t.timestamps
|
6
7
|
end
|
7
|
-
|
8
|
-
add_index :sandboxy, ['sandboxed_id', 'sandboxed_type'], name: 'sandboxy_sandboxed'
|
9
|
-
end
|
10
|
-
|
11
|
-
def self.down
|
12
|
-
drop_table :sandboxy
|
13
8
|
end
|
14
9
|
end
|
data/lib/sandboxy.rb
CHANGED
@@ -2,16 +2,9 @@ require 'sandboxy/version'
|
|
2
2
|
|
3
3
|
module Sandboxy
|
4
4
|
|
5
|
-
|
5
|
+
require 'sandboxy/configuration'
|
6
6
|
|
7
|
-
|
8
|
-
attr_accessor :configuration
|
9
|
-
end
|
10
|
-
|
11
|
-
def self.configure
|
12
|
-
self.configuration ||= Configuration.new
|
13
|
-
yield configuration
|
14
|
-
end
|
7
|
+
require 'sandboxy/engine'
|
15
8
|
|
16
9
|
autoload :Sandboxed, 'sandboxy/sandboxed'
|
17
10
|
|
@@ -1,20 +1,47 @@
|
|
1
1
|
module Sandboxy
|
2
|
-
class Configuration
|
3
2
|
|
4
|
-
|
5
|
-
attr_accessor :
|
3
|
+
class << self
|
4
|
+
attr_accessor :configuration
|
6
5
|
|
7
|
-
def
|
8
|
-
|
9
|
-
|
6
|
+
def environment
|
7
|
+
$sandboxy ||= Sandboxy.configuration.default
|
8
|
+
$sandboxy
|
9
|
+
end
|
10
|
+
|
11
|
+
def environment= value
|
12
|
+
$sandboxy = value
|
10
13
|
end
|
11
14
|
|
12
|
-
def
|
13
|
-
|
15
|
+
def method_missing m, *args
|
16
|
+
if m.to_s[/(.+)_environment?/]
|
17
|
+
self.environment? $1
|
18
|
+
else
|
19
|
+
super
|
20
|
+
end
|
14
21
|
end
|
15
22
|
|
16
|
-
def
|
17
|
-
|
23
|
+
def respond_to? m, include_private = false
|
24
|
+
super || m.to_s[/(.+)_environment?/]
|
25
|
+
end
|
26
|
+
|
27
|
+
def environment? value
|
28
|
+
self.environment == value
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.configure
|
33
|
+
self.configuration ||= Configuration.new
|
34
|
+
yield configuration
|
35
|
+
end
|
36
|
+
|
37
|
+
class Configuration
|
38
|
+
|
39
|
+
attr_accessor :default
|
40
|
+
attr_accessor :retain
|
41
|
+
|
42
|
+
def initialize
|
43
|
+
@default = 'live'
|
44
|
+
@retain = false
|
18
45
|
end
|
19
46
|
|
20
47
|
end
|
data/lib/sandboxy/middleware.rb
CHANGED
@@ -8,10 +8,10 @@ module Sandboxy
|
|
8
8
|
def call env
|
9
9
|
require 'sandboxy'
|
10
10
|
|
11
|
-
|
12
|
-
$
|
11
|
+
previous_environment = Sandboxy.environment
|
12
|
+
$sandboxy = nil
|
13
13
|
|
14
|
-
puts
|
14
|
+
puts "Sandboxy: Moved to #{Sandboxy.configuration.default} environment" if Sandboxy.environment != previous_environment
|
15
15
|
end
|
16
16
|
|
17
17
|
end
|
data/lib/sandboxy/railtie.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
require 'rails'
|
1
|
+
require 'rails/railtie'
|
2
|
+
require 'active_record'
|
2
3
|
|
3
4
|
|
4
5
|
module Sandboxy
|
@@ -10,12 +11,12 @@ module Sandboxy
|
|
10
11
|
end
|
11
12
|
end
|
12
13
|
|
13
|
-
initializer 'sandboxy.
|
14
|
-
|
15
|
-
|
14
|
+
initializer 'sandboxy.middleware' do |app|
|
15
|
+
app.middleware.use(Sandboxy::Middleware) unless Sandboxy.configuration&.retain
|
16
|
+
end
|
16
17
|
|
17
|
-
|
18
|
-
|
18
|
+
config.after_initialize do
|
19
|
+
puts "Sandboxy: Using #{Sandboxy.configuration.default} environment"
|
19
20
|
end
|
20
21
|
|
21
22
|
end
|
data/lib/sandboxy/sandboxed.rb
CHANGED
@@ -9,41 +9,91 @@ module Sandboxy
|
|
9
9
|
|
10
10
|
def sandboxy
|
11
11
|
has_one :sandbox, as: :sandboxed, dependent: :destroy
|
12
|
+
before_create :set_environment
|
12
13
|
include Sandboxy::Sandboxed::InstanceMethods
|
13
14
|
|
14
|
-
|
15
|
-
scope :sandboxed_scoped, -> { left_outer_joins(:sandbox).where.not(sandbox: { id: nil }) }
|
16
|
-
default_scope {
|
17
|
-
case $sandbox
|
18
|
-
when true then sandboxed_scoped
|
19
|
-
when false then live_scoped
|
20
|
-
end
|
21
|
-
}
|
22
|
-
scope :live, -> { unscope(:joins, :where).live_scoped }
|
23
|
-
scope :sandboxed, -> { unscope(:joins, :where).sandboxed_scoped }
|
15
|
+
default_scope { self.environment_scoped(Sandboxy.environment) }
|
24
16
|
scope :desandbox, -> { unscope(:joins, :where).all }
|
25
17
|
|
26
|
-
|
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
|
27
44
|
end
|
28
45
|
|
29
46
|
end
|
30
47
|
|
31
48
|
module InstanceMethods
|
32
49
|
|
33
|
-
def
|
34
|
-
|
50
|
+
def method_missing m, *args
|
51
|
+
if m.to_s[/move_environment_(.+)/]
|
52
|
+
self.move_environment $1
|
53
|
+
elsif m.to_s[/(.+)_environment?/]
|
54
|
+
self.environment? $1
|
55
|
+
else
|
56
|
+
super
|
57
|
+
end
|
35
58
|
end
|
36
59
|
|
37
|
-
def
|
38
|
-
|
60
|
+
def respond_to? m, include_private = false
|
61
|
+
super || m.to_s[/move_environment_(.+)/] || m.to_s[/(.+)_environment?/]
|
39
62
|
end
|
40
63
|
|
41
|
-
def
|
42
|
-
|
64
|
+
def move_environment value
|
65
|
+
case value
|
66
|
+
when Sandboxy.configuration.default
|
67
|
+
Sandbox.where(sandboxed_id: self.id, sandboxed_type: self.class.name).destroy_all
|
68
|
+
self.sandbox = nil
|
69
|
+
self.save!
|
70
|
+
else
|
71
|
+
unless self.sandbox.nil?
|
72
|
+
self.sandbox.update_attributes environment: value
|
73
|
+
else
|
74
|
+
sandbox = self.build_sandbox
|
75
|
+
sandbox.environment = value
|
76
|
+
sandbox.save!
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def environment? value
|
82
|
+
self.environment == value
|
43
83
|
end
|
44
84
|
|
45
|
-
def
|
46
|
-
|
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
|
47
97
|
end
|
48
98
|
|
49
99
|
end
|