sandboxy 1.0.0 → 1.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 44501426bdcd615cc069e3327bbd6e37cc7f1496
4
- data.tar.gz: dee2033f5deddb78dd4130b7d17f90e91f263cc0
3
+ metadata.gz: 3af3fd5a81896b5f9480bf645add749cbd57d009
4
+ data.tar.gz: 6ebb750a5ec79e5a91d71446b0f60987da39f72a
5
5
  SHA512:
6
- metadata.gz: '071468637354c1ce9e6801f56bc8078ce8d7c7f09ac744e1a85469282c542c727302d68611a500ada44c57ef39fa0e47b077a816febf626c5441d45784147c68'
7
- data.tar.gz: '080dc870bcf6be859c2b1e8fd238281997af4a0298ec35192f25de5b70cdbb21159009ac59ed0893f6276fe49d32f56fffca47d06d29d9a69cf0240199351e97'
6
+ metadata.gz: 6029e39f313ae946fb6fdfa413dfbc88e0437c080873d6c073a01490c02c71b6dd3ee7d9de3cff54b61e24b7285bb5aaf1bcbae8e1d488bd5a0deb724c6c71b7
7
+ data.tar.gz: 9f8bb2178a429068a9389b1df16dd49f1a614269a18479fffadd9503c9e75d0b67585d1c882512adf792f55dcbee670b40ef72f0b8fbd1370f4e20757b2bde1c
data/CHANGELOG.md CHANGED
@@ -3,3 +3,12 @@
3
3
  ### master
4
4
 
5
5
  * nothing yet
6
+
7
+ ### 1.1.0 - 2017-08-27
8
+
9
+ * features
10
+ * use rack middleware to refresh environment defaults on new requests
11
+
12
+ ### 1.0.0 - 2017-08-27
13
+
14
+ * initial release
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  <img src="https://travis-ci.org/slooob/sandboxy.svg?branch=master" /> [![Gem Version](https://badge.fury.io/rb/sandboxy.svg)](https://badge.fury.io/rb/sandboxy)
4
4
 
5
- Sandboxy allows you to use two virtual data-oriented environments inside a Rails application with ActiveRecord while being able to switch in between at runtime.
5
+ Sandboxy allows you to use virtual data-oriented environments inside a Rails application while being able to switch in between at runtime. It achieves that by using a combination of Rack Middleware and ActiveRecord.
6
6
 
7
7
  ---
8
8
 
@@ -11,7 +11,9 @@ Sandboxy allows you to use two virtual data-oriented environments inside a Rails
11
11
  * [Installation](#installation)
12
12
  * [Usage](#usage)
13
13
  * [Setup](#setup)
14
+ * [Configuration](#configuration)
14
15
  * [Sandboxed methods](#sandboxed-methods)
16
+ * [Sandboxy methods](#sandboxy-methods)
15
17
  * [Switching environments](#switching-environments)
16
18
  * [Sandbox & APIs](#sandbox--apis)
17
19
  * [To Do](#to-do)
@@ -47,7 +49,11 @@ Now run the generator:
47
49
 
48
50
  $ rails g sandboxy
49
51
 
50
- You can specify that your application should use the sandbox by default by passing `--default true`. Learn more about switching environments [here](#switching-environments).
52
+ You can specify your applications default environment by passing `--default live` or `--default sandbox`. Learn more about switching environments [here](#switching-environments).
53
+
54
+ To set that your app should retain it's environment at runtime on new requests pass `--retain_environment true`.
55
+
56
+ You can always update your [configuration](#configuration) later.
51
57
 
52
58
  To wrap things up, migrate the changes into your database:
53
59
 
@@ -55,13 +61,13 @@ To wrap things up, migrate the changes into your database:
55
61
 
56
62
  **Note:** Use `rake db:migrate` instead if you run Rails < 5.
57
63
 
58
- This will create an initializer as well as a migration file and the `Sandbox` model.
64
+ This will create a configuration file under `config/sandboxy.yml` as well as a migration file and the `Sandbox` model.
59
65
 
60
66
  ## Usage
61
67
 
62
68
  ### Setup
63
69
 
64
- Add Sandboxy to the models where you want to separate live & sandbox reocrds:
70
+ Add Sandboxy to the models where you want to separate live & sandbox records:
65
71
 
66
72
  ```ruby
67
73
  class Foo < ApplicationRecord
@@ -72,14 +78,20 @@ end
72
78
  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:
73
79
 
74
80
  ```ruby
75
- class Sandboxy < ApplicationRecord
81
+ class SharedSandbox < ApplicationRecord
76
82
  sandboxy
77
83
  end
78
84
 
79
- class Foo < Sandboxy
85
+ class Foo < SharedSandbox
80
86
  end
81
87
  ```
82
88
 
89
+ ### Configuration
90
+
91
+ In `config/sandboxy.yml` you define your app's default environment. This can be either set to `live` or `sandbox`. It defaults to `live`.
92
+
93
+ Now this default gets refreshed before every new request. To retain any environment you [switched in at runtime](#switching-environments), you need to set `retain_environment` to `true`. Defaults to `false`.
94
+
83
95
  ### Sandboxed methods
84
96
 
85
97
  By default you can only access records belonging to the current environment (`live` or `sandbox`):
@@ -116,11 +128,27 @@ foo.make_sandboxed
116
128
  foo.sandboxed? # => true
117
129
  ```
118
130
 
119
- ### Switching environments
131
+ ### Sandboxy methods
132
+
133
+ To access your default environment setting:
134
+
135
+ ```ruby
136
+ Sandboxy.environment # => 'live' / 'sandbox'
137
+ Sandboxy.sandbox? # => true / false
138
+ Sandboxy.live? # => true / false
139
+ ```
140
+
141
+ **Note:** `Sandboxy.environment` does *NOT* return the apps current environment. For that use the [`$sandbox` variable](#switching-environments) instead.
142
+
143
+ You can also access whether your app retains your environment throughout request:
120
144
 
121
- In `config/initializers/sandboxy.rb` you define your app's default environment by setting the `$sandbox` variable.
145
+ ```ruby
146
+ Sandboxy.retain_environment # => true / false
147
+ ```
148
+
149
+ ### Switching environments
122
150
 
123
- You can override that variable anywhere in your application. That makes Sandboxy super flexible.
151
+ 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.
124
152
 
125
153
  #### Sandbox & APIs
126
154
 
@@ -0,0 +1,13 @@
1
+ # ----------
2
+ # Sandboxy
3
+ # ----------
4
+
5
+ # Set your environment default: Must be either `live` or `sandbox`.
6
+ # This is the environment that your app boots with.
7
+ # By default it gets refreshed with every new request to your server.
8
+ # environment: 'live'
9
+
10
+ # Specify whether to retain your current app environment on new requests.
11
+ # If set to true, your app will only load your environment default when starting.
12
+ # Every additional switch of your environment at runtime will then not be automatically resolved to your environment default on a new request.
13
+ # retain_environment: false
@@ -7,7 +7,8 @@ class SandboxyGenerator < Rails::Generators::Base
7
7
 
8
8
  source_root File.join File.dirname(__FILE__), 'templates'
9
9
  desc 'Install sandboxy'
10
- class_option :default, desc: 'Set the default $sandbox indicator', type: :boolean, default: false, aliases: '-d'
10
+ class_option :default, desc: 'Set to default environment. Accepts either `live` or `sandbox`.', type: :boolean, default: false, aliases: '-d'
11
+ class_option :retain_environment, desc: "Whether your app should retain it's environment at runtime on new requests.", type: :boolean, default: false, aliases: '-re'
11
12
 
12
13
  def self.next_migration_number dirname
13
14
  if ActiveRecord::Base.timestamped_migrations
@@ -21,8 +22,8 @@ class SandboxyGenerator < Rails::Generators::Base
21
22
  migration_template 'migration.rb.erb', 'db/migrate/sandboxy_migration.rb', migration_version: migration_version
22
23
  end
23
24
 
24
- def create_initializer
25
- template 'initializer.rb.erb', 'config/initializers/sandboxy.rb'
25
+ def create_configuration
26
+ template 'configuration.yml.erb', 'config/sandboxy.yml'
26
27
  end
27
28
 
28
29
  def create_model
@@ -0,0 +1,13 @@
1
+ # ----------
2
+ # Sandboxy
3
+ # ----------
4
+
5
+ # Set your environment default: Must be either `live` or `sandbox`.
6
+ # This is the environment that your app boots with.
7
+ # By default it gets refreshed with every new request to your server.
8
+ <% if options[:default] %>environment: <%= options[:default] %><% else %># environment: 'live'<% end %>
9
+
10
+ # Specify whether to retain your current app environment on new requests.
11
+ # If set to true, your app will only load your environment default when starting.
12
+ # Every additional switch of your environment at runtime will then not be automatically resolved to your environment default on a new request.
13
+ <% if options[:retain_environment] %>retain_environment: <%= options[:retain_environment] %><% else %># retain_environment: false<% end %>
@@ -0,0 +1,42 @@
1
+ module Sandboxy
2
+
3
+ def self.environment
4
+ config = get_config
5
+ if config&.key :environment
6
+ config[:environment]
7
+ else
8
+ 'live'
9
+ end
10
+ end
11
+
12
+ def self.sandbox?
13
+ Sandboxy.environment == 'sandbox' ? true : false
14
+ end
15
+
16
+ def self.live?
17
+ !Sandboxy.sandbox?
18
+ end
19
+
20
+ def self.retain_environment
21
+ config = get_config
22
+ if config&.key :retain_environment
23
+ config[:retain_environment]
24
+ else
25
+ false
26
+ end
27
+ end
28
+
29
+
30
+ private
31
+
32
+
33
+ def self.get_config
34
+ require 'yaml'
35
+
36
+ begin
37
+ YAML.load_file 'config/sandboxy.yml'
38
+ rescue Exception
39
+ end
40
+ end
41
+
42
+ end
@@ -0,0 +1,15 @@
1
+ module Sandboxy
2
+ class Middleware
3
+
4
+ def initialize app
5
+ @app = app
6
+ end
7
+
8
+ def call env
9
+ previous_sandbox = $sandbox
10
+ $sandbox = Sandboxy.environment == 'sandbox' ? true : false
11
+ puts 'Sandbox: Moved to ' + Sandboxy.environment + ' environment' if $sandbox != previous_sandbox
12
+ end
13
+
14
+ end
15
+ end
@@ -1,5 +1,4 @@
1
1
  require 'rails'
2
- require 'sandboxy/sandboxed'
3
2
 
4
3
 
5
4
  module Sandboxy
@@ -11,5 +10,11 @@ module Sandboxy
11
10
  end
12
11
  end
13
12
 
13
+ initializer 'sandboxy.configure_rails_initialization' do |app|
14
+ puts 'Sandboxy: Using ' + Sandboxy.environment + ' environment'
15
+ $sandbox = Sandboxy.environment == 'sandbox' ? true : false
16
+ app.middleware.use(Sandboxy::Middleware) unless Sandboxy.retain_environment
17
+ end
18
+
14
19
  end
15
20
  end
@@ -43,7 +43,7 @@ module Sandboxy
43
43
  end
44
44
 
45
45
  def live?
46
- !self.sandbox.present?
46
+ !self.sandboxed?
47
47
  end
48
48
 
49
49
  end
@@ -1,5 +1,5 @@
1
1
  module Sandboxy
2
2
 
3
- VERSION = '1.0.0'
3
+ VERSION = '1.1.0'
4
4
 
5
5
  end
data/lib/sandboxy.rb CHANGED
@@ -1,10 +1,12 @@
1
1
  require 'sandboxy/version'
2
2
 
3
3
 
4
- module ActsAsFavoritor
4
+ module Sandboxy
5
5
 
6
6
  autoload :Sandboxed, 'sandboxy/sandboxed'
7
7
 
8
+ require 'sandboxy/configuration'
9
+ require 'sandboxy/middleware'
8
10
  require 'sandboxy/railtie' # if defined?(Rails)
9
11
 
10
12
  end
data/sandboxy.gemspec CHANGED
@@ -6,7 +6,7 @@ Gem::Specification.new do |gem|
6
6
  gem.version = Sandboxy::VERSION
7
7
  gem.platform = Gem::Platform::RUBY
8
8
  gem.summary = 'Virtual data-oriented environments for Rails'
9
- gem.description = 'Sandboxy allows you to use two virtual data-oriented environments inside a Rails application with ActiveRecord while being able to switch in between at runtime.'
9
+ gem.description = 'Sandboxy allows you to use virtual data-oriented environments inside a Rails application while being able to switch in between at runtime. It achieves that by using a combination of Rack Middleware and ActiveRecord.'
10
10
  gem.authors = 'Slooob'
11
11
  gem.email = 'developer@slooob.com'
12
12
  gem.homepage = 'https://developer.slooob.com/open-source'
@@ -17,13 +17,13 @@ Gem::Specification.new do |gem|
17
17
 
18
18
  gem.post_install_message = IO.read('INSTALL.md')
19
19
 
20
- gem.required_ruby_version = '>= 2.0'
20
+ gem.required_ruby_version = '>= 2.3'
21
21
 
22
22
  gem.add_dependency 'activerecord', '>= 4.0'
23
23
 
24
24
  gem.add_development_dependency 'sqlite3', '~> 1.3'
25
- gem.add_development_dependency 'shoulda_create', '~> 0.0'
26
25
  gem.add_development_dependency 'shoulda', '~> 3.5'
26
+ gem.add_development_dependency 'shoulda_create', '~> 0.0'
27
27
  gem.add_development_dependency 'factory_girl', '~> 4.8'
28
28
  gem.add_development_dependency 'rails', '>= 4.0'
29
29
  gem.add_development_dependency 'tzinfo-data', '~> 1.2017'
@@ -0,0 +1 @@
1
+ # Use defaults
data/test/sandbox_test.rb CHANGED
@@ -2,7 +2,17 @@ require File.dirname(__FILE__) + '/test_helper'
2
2
 
3
3
  class SandboxTest < ActiveSupport::TestCase
4
4
 
5
- context '#sandbox' do
5
+ context 'scopes' do
6
+ should 'be defined' do
7
+ assert Some.respond_to? :live_scoped
8
+ assert Some.respond_to? :sandboxed_scoped
9
+ assert Some.respond_to? :live
10
+ assert Some.respond_to? :sandboxed
11
+ assert Some.respond_to? :desandbox
12
+ end
13
+ end
14
+
15
+ context 'sandbox' do
6
16
  setup do
7
17
  @post = FactoryGirl.create :post
8
18
  @purchase = FactoryGirl.create :purchase
@@ -11,7 +21,7 @@ class SandboxTest < ActiveSupport::TestCase
11
21
  @receipt = FactoryGirl.create :receipt
12
22
  end
13
23
 
14
- context 'desandbox' do
24
+ context '#desandbox' do
15
25
  should 'return all records' do
16
26
  assert_equal Some.unscope(:joins, :where).all, Some.desandbox
17
27
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sandboxy
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Slooob
@@ -39,33 +39,33 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1.3'
41
41
  - !ruby/object:Gem::Dependency
42
- name: shoulda_create
42
+ name: shoulda
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0.0'
47
+ version: '3.5'
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: '0.0'
54
+ version: '3.5'
55
55
  - !ruby/object:Gem::Dependency
56
- name: shoulda
56
+ name: shoulda_create
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '3.5'
61
+ version: '0.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: '3.5'
68
+ version: '0.0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: factory_girl
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -108,8 +108,9 @@ dependencies:
108
108
  - - "~>"
109
109
  - !ruby/object:Gem::Version
110
110
  version: '1.2017'
111
- description: Sandboxy allows you to use two virtual data-oriented environments inside
112
- a Rails application with ActiveRecord while being able to switch in between at runtime.
111
+ description: Sandboxy allows you to use virtual data-oriented environments inside
112
+ a Rails application while being able to switch in between at runtime. It achieves
113
+ that by using a combination of Rack Middleware and ActiveRecord.
113
114
  email: developer@slooob.com
114
115
  executables: []
115
116
  extensions: []
@@ -201,11 +202,11 @@ files:
201
202
  - examples/rails_example/config/initializers/filter_parameter_logging.rb
202
203
  - examples/rails_example/config/initializers/inflections.rb
203
204
  - examples/rails_example/config/initializers/mime_types.rb
204
- - examples/rails_example/config/initializers/sandboxy.rb
205
205
  - examples/rails_example/config/initializers/wrap_parameters.rb
206
206
  - examples/rails_example/config/locales/en.yml
207
207
  - examples/rails_example/config/puma.rb
208
208
  - examples/rails_example/config/routes.rb
209
+ - examples/rails_example/config/sandboxy.yml
209
210
  - examples/rails_example/config/secrets.yml
210
211
  - examples/rails_example/db/migrate/20170826222427_sandboxy_migration.rb
211
212
  - examples/rails_example/db/migrate/20170826223227_create_foos.rb
@@ -245,10 +246,12 @@ files:
245
246
  - init.rb
246
247
  - lib/generators/sandboxy_generator.rb
247
248
  - lib/generators/templates/README.md
248
- - lib/generators/templates/initializer.rb.erb
249
+ - lib/generators/templates/configuration.yml.erb
249
250
  - lib/generators/templates/migration.rb.erb
250
251
  - lib/generators/templates/model.rb
251
252
  - lib/sandboxy.rb
253
+ - lib/sandboxy/configuration.rb
254
+ - lib/sandboxy/middleware.rb
252
255
  - lib/sandboxy/railtie.rb
253
256
  - lib/sandboxy/sandboxed.rb
254
257
  - lib/sandboxy/version.rb
@@ -266,11 +269,11 @@ files:
266
269
  - test/dummy30/config/environment.rb
267
270
  - test/dummy30/config/environments/development.rb
268
271
  - test/dummy30/config/environments/test.rb
269
- - test/dummy30/config/initializers/sandboxy.rb
270
272
  - test/dummy30/config/initializers/secret_token.rb
271
273
  - test/dummy30/config/initializers/session_store.rb
272
274
  - test/dummy30/config/locales/en.yml
273
275
  - test/dummy30/config/routes.rb
276
+ - test/dummy30/config/sandboxy.yml
274
277
  - test/factories/somes.rb
275
278
  - test/factories/users.rb
276
279
  - test/sandbox_test.rb
@@ -292,7 +295,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
292
295
  requirements:
293
296
  - - ">="
294
297
  - !ruby/object:Gem::Version
295
- version: '2.0'
298
+ version: '2.3'
296
299
  required_rubygems_version: !ruby/object:Gem::Requirement
297
300
  requirements:
298
301
  - - ">="
@@ -1,7 +0,0 @@
1
- # ----------
2
- # Sandboxy
3
- # ----------
4
-
5
- # Specify your default environment by setting `$sandbox`. Learn more: https://github.com/slooob/sandboxy
6
- $sandbox = false
7
- # $sandbox = true
@@ -1,7 +0,0 @@
1
- # ----------
2
- # Sandboxy
3
- # ----------
4
-
5
- # Specify your default environment by setting `$sandbox`. Learn more: https://github.com/slooob/sandboxy
6
- <% if options[:default] == true %># <% end %>$sandbox = false
7
- <% if options[:default] == false %># <% end %>$sandbox = true
@@ -1 +0,0 @@
1
- $sandbox = false