sandboxy 2.0.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (208) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +7 -0
  3. data/README.md +26 -36
  4. data/{examples/rails_example/app → app}/models/sandbox.rb +1 -1
  5. data/lib/generators/sandboxy_generator.rb +0 -4
  6. data/lib/generators/templates/initializer.rb +7 -7
  7. data/lib/generators/templates/migration.rb.erb +4 -9
  8. data/lib/sandboxy.rb +2 -9
  9. data/lib/sandboxy/configuration.rb +37 -10
  10. data/lib/sandboxy/engine.rb +6 -0
  11. data/lib/sandboxy/middleware.rb +3 -3
  12. data/lib/sandboxy/railtie.rb +7 -6
  13. data/lib/sandboxy/sandboxed.rb +69 -19
  14. data/lib/sandboxy/version.rb +1 -1
  15. metadata +5 -197
  16. data/.github/issue_template.md +0 -14
  17. data/.github/pull_request_template.md +0 -21
  18. data/.gitignore +0 -8
  19. data/.travis.yml +0 -3
  20. data/CODE_OF_CONDUCT.md +0 -46
  21. data/CONTRIBUTING.md +0 -1
  22. data/DEPRECATIONS.md +0 -5
  23. data/Gemfile +0 -9
  24. data/INSTALL.md +0 -3
  25. data/Rakefile +0 -15
  26. data/examples/rails_example/.gitignore +0 -23
  27. data/examples/rails_example/Gemfile +0 -54
  28. data/examples/rails_example/README.md +0 -24
  29. data/examples/rails_example/Rakefile +0 -6
  30. data/examples/rails_example/app/api/books/api.rb +0 -33
  31. data/examples/rails_example/app/assets/config/manifest.js +0 -3
  32. data/examples/rails_example/app/assets/images/.keep +0 -0
  33. data/examples/rails_example/app/assets/javascripts/application.js +0 -15
  34. data/examples/rails_example/app/assets/javascripts/applications.coffee +0 -3
  35. data/examples/rails_example/app/assets/javascripts/authors.coffee +0 -3
  36. data/examples/rails_example/app/assets/javascripts/books.coffee +0 -3
  37. data/examples/rails_example/app/assets/javascripts/cable.js +0 -13
  38. data/examples/rails_example/app/assets/javascripts/channels/.keep +0 -0
  39. data/examples/rails_example/app/assets/javascripts/libraries.coffee +0 -3
  40. data/examples/rails_example/app/assets/javascripts/sandbox.coffee +0 -3
  41. data/examples/rails_example/app/assets/stylesheets/application.css +0 -15
  42. data/examples/rails_example/app/assets/stylesheets/applications.scss +0 -3
  43. data/examples/rails_example/app/assets/stylesheets/authors.scss +0 -3
  44. data/examples/rails_example/app/assets/stylesheets/books.scss +0 -3
  45. data/examples/rails_example/app/assets/stylesheets/libraries.scss +0 -3
  46. data/examples/rails_example/app/assets/stylesheets/sandbox.scss +0 -3
  47. data/examples/rails_example/app/assets/stylesheets/scaffolds.scss +0 -84
  48. data/examples/rails_example/app/channels/application_cable/channel.rb +0 -4
  49. data/examples/rails_example/app/channels/application_cable/connection.rb +0 -4
  50. data/examples/rails_example/app/controllers/application_controller.rb +0 -7
  51. data/examples/rails_example/app/controllers/applications_controller.rb +0 -74
  52. data/examples/rails_example/app/controllers/authors_controller.rb +0 -77
  53. data/examples/rails_example/app/controllers/books_controller.rb +0 -77
  54. data/examples/rails_example/app/controllers/concerns/.keep +0 -0
  55. data/examples/rails_example/app/controllers/libraries_controller.rb +0 -77
  56. data/examples/rails_example/app/controllers/sandbox_controller.rb +0 -6
  57. data/examples/rails_example/app/helpers/application_helper.rb +0 -2
  58. data/examples/rails_example/app/helpers/applications_helper.rb +0 -2
  59. data/examples/rails_example/app/helpers/authors_helper.rb +0 -2
  60. data/examples/rails_example/app/helpers/books_helper.rb +0 -2
  61. data/examples/rails_example/app/helpers/libraries_helper.rb +0 -2
  62. data/examples/rails_example/app/helpers/sandbox_helper.rb +0 -2
  63. data/examples/rails_example/app/jobs/application_job.rb +0 -2
  64. data/examples/rails_example/app/mailers/application_mailer.rb +0 -4
  65. data/examples/rails_example/app/models/application.rb +0 -14
  66. data/examples/rails_example/app/models/application_record.rb +0 -3
  67. data/examples/rails_example/app/models/author.rb +0 -2
  68. data/examples/rails_example/app/models/book.rb +0 -2
  69. data/examples/rails_example/app/models/concerns/.keep +0 -0
  70. data/examples/rails_example/app/models/library.rb +0 -2
  71. data/examples/rails_example/app/views/applications/_application.json.jbuilder +0 -2
  72. data/examples/rails_example/app/views/applications/_form.html.erb +0 -19
  73. data/examples/rails_example/app/views/applications/edit.html.erb +0 -6
  74. data/examples/rails_example/app/views/applications/index.html.erb +0 -25
  75. data/examples/rails_example/app/views/applications/index.json.jbuilder +0 -1
  76. data/examples/rails_example/app/views/applications/new.html.erb +0 -5
  77. data/examples/rails_example/app/views/applications/show.html.erb +0 -7
  78. data/examples/rails_example/app/views/applications/show.json.jbuilder +0 -1
  79. data/examples/rails_example/app/views/authors/_author.json.jbuilder +0 -2
  80. data/examples/rails_example/app/views/authors/_form.html.erb +0 -19
  81. data/examples/rails_example/app/views/authors/edit.html.erb +0 -6
  82. data/examples/rails_example/app/views/authors/index.html.erb +0 -25
  83. data/examples/rails_example/app/views/authors/index.json.jbuilder +0 -1
  84. data/examples/rails_example/app/views/authors/new.html.erb +0 -5
  85. data/examples/rails_example/app/views/authors/show.html.erb +0 -4
  86. data/examples/rails_example/app/views/authors/show.json.jbuilder +0 -1
  87. data/examples/rails_example/app/views/books/_book.json.jbuilder +0 -2
  88. data/examples/rails_example/app/views/books/_form.html.erb +0 -19
  89. data/examples/rails_example/app/views/books/edit.html.erb +0 -6
  90. data/examples/rails_example/app/views/books/index.html.erb +0 -25
  91. data/examples/rails_example/app/views/books/index.json.jbuilder +0 -1
  92. data/examples/rails_example/app/views/books/new.html.erb +0 -5
  93. data/examples/rails_example/app/views/books/show.html.erb +0 -4
  94. data/examples/rails_example/app/views/books/show.json.jbuilder +0 -1
  95. data/examples/rails_example/app/views/layouts/application.html.erb +0 -14
  96. data/examples/rails_example/app/views/layouts/mailer.html.erb +0 -13
  97. data/examples/rails_example/app/views/layouts/mailer.text.erb +0 -1
  98. data/examples/rails_example/app/views/libraries/_form.html.erb +0 -19
  99. data/examples/rails_example/app/views/libraries/_library.json.jbuilder +0 -2
  100. data/examples/rails_example/app/views/libraries/edit.html.erb +0 -6
  101. data/examples/rails_example/app/views/libraries/index.html.erb +0 -25
  102. data/examples/rails_example/app/views/libraries/index.json.jbuilder +0 -1
  103. data/examples/rails_example/app/views/libraries/new.html.erb +0 -5
  104. data/examples/rails_example/app/views/libraries/show.html.erb +0 -4
  105. data/examples/rails_example/app/views/libraries/show.json.jbuilder +0 -1
  106. data/examples/rails_example/bin/bundle +0 -3
  107. data/examples/rails_example/bin/rails +0 -4
  108. data/examples/rails_example/bin/rake +0 -4
  109. data/examples/rails_example/bin/setup +0 -38
  110. data/examples/rails_example/bin/update +0 -29
  111. data/examples/rails_example/bin/yarn +0 -11
  112. data/examples/rails_example/config.ru +0 -5
  113. data/examples/rails_example/config/application.rb +0 -18
  114. data/examples/rails_example/config/boot.rb +0 -3
  115. data/examples/rails_example/config/cable.yml +0 -10
  116. data/examples/rails_example/config/database.yml +0 -25
  117. data/examples/rails_example/config/environment.rb +0 -5
  118. data/examples/rails_example/config/environments/development.rb +0 -54
  119. data/examples/rails_example/config/environments/production.rb +0 -91
  120. data/examples/rails_example/config/environments/test.rb +0 -42
  121. data/examples/rails_example/config/initializers/application_controller_renderer.rb +0 -6
  122. data/examples/rails_example/config/initializers/assets.rb +0 -14
  123. data/examples/rails_example/config/initializers/backtrace_silencers.rb +0 -7
  124. data/examples/rails_example/config/initializers/cookies_serializer.rb +0 -5
  125. data/examples/rails_example/config/initializers/filter_parameter_logging.rb +0 -4
  126. data/examples/rails_example/config/initializers/grape.rb +0 -2
  127. data/examples/rails_example/config/initializers/inflections.rb +0 -16
  128. data/examples/rails_example/config/initializers/mime_types.rb +0 -4
  129. data/examples/rails_example/config/initializers/sandboxy.rb +0 -13
  130. data/examples/rails_example/config/initializers/wrap_parameters.rb +0 -14
  131. data/examples/rails_example/config/locales/en.yml +0 -33
  132. data/examples/rails_example/config/puma.rb +0 -56
  133. data/examples/rails_example/config/routes.rb +0 -13
  134. data/examples/rails_example/config/secrets.yml +0 -32
  135. data/examples/rails_example/db/migrate/20170827184048_create_books.rb +0 -9
  136. data/examples/rails_example/db/migrate/20170827184054_create_authors.rb +0 -9
  137. data/examples/rails_example/db/migrate/20170827184106_create_libraries.rb +0 -9
  138. data/examples/rails_example/db/migrate/20170827184304_create_applications.rb +0 -11
  139. data/examples/rails_example/db/migrate/20170827190815_sandboxy_migration.rb +0 -14
  140. data/examples/rails_example/db/schema.rb +0 -50
  141. data/examples/rails_example/db/seeds.rb +0 -7
  142. data/examples/rails_example/lib/assets/.keep +0 -0
  143. data/examples/rails_example/lib/tasks/.keep +0 -0
  144. data/examples/rails_example/package.json +0 -5
  145. data/examples/rails_example/public/404.html +0 -67
  146. data/examples/rails_example/public/422.html +0 -67
  147. data/examples/rails_example/public/500.html +0 -66
  148. data/examples/rails_example/public/apple-touch-icon-precomposed.png +0 -0
  149. data/examples/rails_example/public/apple-touch-icon.png +0 -0
  150. data/examples/rails_example/public/favicon.ico +0 -0
  151. data/examples/rails_example/public/robots.txt +0 -1
  152. data/examples/rails_example/test/application_system_test_case.rb +0 -5
  153. data/examples/rails_example/test/controllers/.keep +0 -0
  154. data/examples/rails_example/test/controllers/applications_controller_test.rb +0 -48
  155. data/examples/rails_example/test/controllers/authors_controller_test.rb +0 -48
  156. data/examples/rails_example/test/controllers/books_controller_test.rb +0 -48
  157. data/examples/rails_example/test/controllers/libraries_controller_test.rb +0 -48
  158. data/examples/rails_example/test/controllers/sandbox_controller_test.rb +0 -7
  159. data/examples/rails_example/test/fixtures/.keep +0 -0
  160. data/examples/rails_example/test/fixtures/applications.yml +0 -11
  161. data/examples/rails_example/test/fixtures/authors.yml +0 -11
  162. data/examples/rails_example/test/fixtures/books.yml +0 -11
  163. data/examples/rails_example/test/fixtures/files/.keep +0 -0
  164. data/examples/rails_example/test/fixtures/libraries.yml +0 -11
  165. data/examples/rails_example/test/helpers/.keep +0 -0
  166. data/examples/rails_example/test/integration/.keep +0 -0
  167. data/examples/rails_example/test/mailers/.keep +0 -0
  168. data/examples/rails_example/test/models/.keep +0 -0
  169. data/examples/rails_example/test/models/application_test.rb +0 -7
  170. data/examples/rails_example/test/models/author_test.rb +0 -7
  171. data/examples/rails_example/test/models/book_test.rb +0 -7
  172. data/examples/rails_example/test/models/library_test.rb +0 -7
  173. data/examples/rails_example/test/system/.keep +0 -0
  174. data/examples/rails_example/test/system/applications_test.rb +0 -9
  175. data/examples/rails_example/test/system/authors_test.rb +0 -9
  176. data/examples/rails_example/test/system/books_test.rb +0 -9
  177. data/examples/rails_example/test/system/libraries_test.rb +0 -9
  178. data/examples/rails_example/test/test_helper.rb +0 -9
  179. data/examples/rails_example/tmp/.keep +0 -0
  180. data/examples/rails_example/vendor/.keep +0 -0
  181. data/init.rb +0 -1
  182. data/lib/generators/templates/model.rb +0 -7
  183. data/sandboxy.gemspec +0 -28
  184. data/test/dummy30/Gemfile +0 -1
  185. data/test/dummy30/Rakefile +0 -4
  186. data/test/dummy30/app/models/application_record.rb +0 -3
  187. data/test/dummy30/app/models/shared_sandbox.rb +0 -4
  188. data/test/dummy30/app/models/some.rb +0 -3
  189. data/test/dummy30/app/models/user.rb +0 -3
  190. data/test/dummy30/config.ru +0 -2
  191. data/test/dummy30/config/application.rb +0 -14
  192. data/test/dummy30/config/boot.rb +0 -10
  193. data/test/dummy30/config/database.yml +0 -6
  194. data/test/dummy30/config/environment.rb +0 -5
  195. data/test/dummy30/config/environments/development.rb +0 -7
  196. data/test/dummy30/config/environments/test.rb +0 -6
  197. data/test/dummy30/config/initializers/sandboxy.rb +0 -13
  198. data/test/dummy30/config/initializers/secret_token.rb +0 -1
  199. data/test/dummy30/config/initializers/session_store.rb +0 -1
  200. data/test/dummy30/config/locales/en.yml +0 -2
  201. data/test/dummy30/config/routes.rb +0 -2
  202. data/test/dummy30/config/sandboxy.yml +0 -1
  203. data/test/factories/somes.rb +0 -17
  204. data/test/factories/users.rb +0 -5
  205. data/test/sandbox_test.rb +0 -51
  206. data/test/sandboxed_test.rb +0 -58
  207. data/test/schema.rb +0 -18
  208. data/test/test_helper.rb +0 -20
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a5eac48d43fcaf454f691ca0169d9ba357f31e46b30aa452e4de0d99f172e9a7
4
- data.tar.gz: 61eeb91dfcd1b788cd95b4f8944684b792552e1f378b46c910eb46e8b2a454a0
3
+ metadata.gz: 179b0c17257a3a969fee06ad285d9e832a5f4b90adfbeb3e1b8bd2c8eef0a45b
4
+ data.tar.gz: e6eb6c261f8abee9fb55c15c4d51ccd284953ecfc3727397eaaf1519f1fed29b
5
5
  SHA512:
6
- metadata.gz: be725877526b7a6e616670c73a0ea5348a0feee76e05d600ad4aa404e04d64b5e1dce0dcb17a2aadadcd692235e3ac09a6f7ad942b51abe652ce17ec3890d1ec
7
- data.tar.gz: 51d58e7689ffef821e4d44a71be4aea455b3e19976c58c963caa1c968789754f25f12eb4f4dcaffe0bd46cc00ba80fb043aca9665c88939bb4d7babae4333e5d
6
+ metadata.gz: 43b75cf5aa42131b4bca6ae0d9c2b3bfaa90d6b88453de2d75401b6c28a27da69b8589f6cbebdd55dba68ddcdb9e7f79acd1f4704a6826f4f15905505e7a8436
7
+ data.tar.gz: 050d0c42b7b47bffbc1161b38d184ba315d7bbd2431fefb5efc53dea25d1a25fb0923937cec88bdbd83e74b888b2a021a2308399ca8a5218d3c943fc2bbd5ba9
@@ -4,6 +4,13 @@
4
4
 
5
5
  * nothing yet
6
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
+
7
14
  ### 2.0.0 - 2017-12-21
8
15
 
9
16
  * features
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.0 onwards. You can add it to your `Gemfile` with:
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 live & sandbox records:
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 (`live` or `sandbox`):
84
+ By default you can only access records belonging to the current environment (defined by `Sandboxy.environment`):
85
85
 
86
86
  ```ruby
87
- $sandbox = true
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 group regardless of your current environment, you can use:
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.live # => returns all live foo's
95
- Foo.sandboxed # => returns all sandbox foo's
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.live? # => false
104
- foo.sandboxed? # => true
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.make_live
113
- foo.live? # => true
114
- foo.make_sandboxed
115
- foo.sandboxed? # => true
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
- ### `Sandboxy` class methods
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
- You can also access whether your app retains your environment throughout requests:
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.configuration.retain_environment # => true / false
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 the `$sandbox` variable 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.
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.environment = 'sandbox'
146
+ config.default = 'sandbox'
157
147
  end
158
148
  ```
159
149
 
160
- **`environment`** Set your environment default: Must be either `live` or `sandbox`. This is the environment that your app boots with. By default it gets refreshed with every new request to your server. Defaults to `'live'`.
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
- **`retain_environment`** Specify whether to retain your current app environment on new requests. If set to `true`, your app will only load your environment default when starting. Every additional switch of your environment at runtime will then not be automatically resolved to your environment default on a new request. Takes a boolean. Defaults to `false`.
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. Uncomment SimpleCov in the Gemfile
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
 
@@ -1,4 +1,4 @@
1
- class Sandbox < ActiveRecord::Base
1
+ class Sandbox < ApplicationRecord
2
2
 
3
3
  self.table_name = 'sandboxy'
4
4
 
@@ -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: Must be either `live` or `sandbox`.
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
- # config.environment = 'live'
5
+ # Takes a string.
6
+ # config.default = 'live'
7
7
 
8
- # Specify whether to retain your current app environment on new requests.
9
- # If set to true, your app will only load your environment default when starting.
10
- # Every additional switch of your environment at runtime will then not be automatically resolved to your environment default on a new request.
11
- # config.retain_environment = false
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 self.up
3
- create_table :sandboxy, force: true do |t|
4
- t.references :sandboxed, polymorphic: true, null: false
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
@@ -2,16 +2,9 @@ require 'sandboxy/version'
2
2
 
3
3
  module Sandboxy
4
4
 
5
- autoload :Configuration, 'sandboxy/configuration'
5
+ require 'sandboxy/configuration'
6
6
 
7
- class << self
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
- attr_accessor :environment
5
- attr_accessor :retain_environment
3
+ class << self
4
+ attr_accessor :configuration
6
5
 
7
- def initialize
8
- @environment = 'live'
9
- @retain_environment = false
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 sandbox?
13
- self.environment == 'sandbox' ? true : false
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 live?
17
- !self.sandbox?
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
@@ -0,0 +1,6 @@
1
+ require 'rails/railtie'
2
+
3
+ module Sandboxy
4
+ class Engine < ::Rails::Engine
5
+ end
6
+ end
@@ -8,10 +8,10 @@ module Sandboxy
8
8
  def call env
9
9
  require 'sandboxy'
10
10
 
11
- previous_sandbox = $sandbox
12
- $sandbox = Sandboxy.configuration.environment == 'sandbox' ? true : false
11
+ previous_environment = Sandboxy.environment
12
+ $sandboxy = nil
13
13
 
14
- puts 'Sandbox: Moved to ' + Sandboxy.configuration.environment.to_s + ' environment' if $sandbox != previous_sandbox
14
+ puts "Sandboxy: Moved to #{Sandboxy.configuration.default} environment" if Sandboxy.environment != previous_environment
15
15
  end
16
16
 
17
17
  end
@@ -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.configure_rails_initialization' do |app|
14
- require 'sandboxy'
15
- puts 'Sandboxy: Using ' + Sandboxy.configuration.environment + ' environment'
14
+ initializer 'sandboxy.middleware' do |app|
15
+ app.middleware.use(Sandboxy::Middleware) unless Sandboxy.configuration&.retain
16
+ end
16
17
 
17
- $sandbox = Sandboxy.configuration.environment == 'sandbox' ? true : false
18
- app.middleware.use(Sandboxy::Middleware) unless Sandboxy.configuration.retain_environment
18
+ config.after_initialize do
19
+ puts "Sandboxy: Using #{Sandboxy.configuration.default} environment"
19
20
  end
20
21
 
21
22
  end
@@ -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
- scope :live_scoped, -> { left_outer_joins(:sandbox).where(sandbox: { id: nil }) }
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
- # before_save :make_sandboxed # -> should be handled automatically through default_scope
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 make_sandboxed
34
- self.build_sandbox unless self.sandbox.present?
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 make_live
38
- self.sandbox.destroy if self.sandbox.present?
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 sandboxed?
42
- self.sandbox.present?
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 live?
46
- !self.sandboxed?
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