sandboxy 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (164) hide show
  1. checksums.yaml +7 -0
  2. data/.github/issue_template.md +14 -0
  3. data/.github/pull_request_template.md +21 -0
  4. data/.gitignore +8 -0
  5. data/.travis.yml +3 -0
  6. data/CHANGELOG.md +5 -0
  7. data/CODE_OF_CONDUCT.md +46 -0
  8. data/CONTRIBUTING.md +1 -0
  9. data/DEPRECATIONS.md +3 -0
  10. data/Gemfile +9 -0
  11. data/INSTALL.md +3 -0
  12. data/LICENSE +21 -0
  13. data/README.md +175 -0
  14. data/Rakefile +16 -0
  15. data/examples/rails_example/.gitignore +23 -0
  16. data/examples/rails_example/Gemfile +53 -0
  17. data/examples/rails_example/README.md +24 -0
  18. data/examples/rails_example/Rakefile +6 -0
  19. data/examples/rails_example/app/assets/config/manifest.js +3 -0
  20. data/examples/rails_example/app/assets/images/.keep +0 -0
  21. data/examples/rails_example/app/assets/javascripts/application.js +15 -0
  22. data/examples/rails_example/app/assets/javascripts/bars.coffee +3 -0
  23. data/examples/rails_example/app/assets/javascripts/cable.js +13 -0
  24. data/examples/rails_example/app/assets/javascripts/channels/.keep +0 -0
  25. data/examples/rails_example/app/assets/javascripts/foos.coffee +3 -0
  26. data/examples/rails_example/app/assets/stylesheets/application.css +15 -0
  27. data/examples/rails_example/app/assets/stylesheets/bars.scss +3 -0
  28. data/examples/rails_example/app/assets/stylesheets/foos.scss +3 -0
  29. data/examples/rails_example/app/assets/stylesheets/scaffolds.scss +84 -0
  30. data/examples/rails_example/app/channels/application_cable/channel.rb +4 -0
  31. data/examples/rails_example/app/channels/application_cable/connection.rb +4 -0
  32. data/examples/rails_example/app/controllers/application_controller.rb +3 -0
  33. data/examples/rails_example/app/controllers/bars_controller.rb +74 -0
  34. data/examples/rails_example/app/controllers/concerns/.keep +0 -0
  35. data/examples/rails_example/app/controllers/foos_controller.rb +74 -0
  36. data/examples/rails_example/app/helpers/application_helper.rb +2 -0
  37. data/examples/rails_example/app/helpers/bars_helper.rb +2 -0
  38. data/examples/rails_example/app/helpers/foos_helper.rb +2 -0
  39. data/examples/rails_example/app/jobs/application_job.rb +2 -0
  40. data/examples/rails_example/app/mailers/application_mailer.rb +4 -0
  41. data/examples/rails_example/app/models/application_record.rb +3 -0
  42. data/examples/rails_example/app/models/bar.rb +2 -0
  43. data/examples/rails_example/app/models/concerns/.keep +0 -0
  44. data/examples/rails_example/app/models/foo.rb +2 -0
  45. data/examples/rails_example/app/models/sandbox.rb +7 -0
  46. data/examples/rails_example/app/models/shared_sandbox.rb +3 -0
  47. data/examples/rails_example/app/views/bars/_bar.json.jbuilder +2 -0
  48. data/examples/rails_example/app/views/bars/_form.html.erb +17 -0
  49. data/examples/rails_example/app/views/bars/edit.html.erb +6 -0
  50. data/examples/rails_example/app/views/bars/index.html.erb +25 -0
  51. data/examples/rails_example/app/views/bars/index.json.jbuilder +1 -0
  52. data/examples/rails_example/app/views/bars/new.html.erb +5 -0
  53. data/examples/rails_example/app/views/bars/show.html.erb +4 -0
  54. data/examples/rails_example/app/views/bars/show.json.jbuilder +1 -0
  55. data/examples/rails_example/app/views/foos/_foo.json.jbuilder +2 -0
  56. data/examples/rails_example/app/views/foos/_form.html.erb +17 -0
  57. data/examples/rails_example/app/views/foos/edit.html.erb +6 -0
  58. data/examples/rails_example/app/views/foos/index.html.erb +25 -0
  59. data/examples/rails_example/app/views/foos/index.json.jbuilder +1 -0
  60. data/examples/rails_example/app/views/foos/new.html.erb +5 -0
  61. data/examples/rails_example/app/views/foos/show.html.erb +4 -0
  62. data/examples/rails_example/app/views/foos/show.json.jbuilder +1 -0
  63. data/examples/rails_example/app/views/layouts/application.html.erb +14 -0
  64. data/examples/rails_example/app/views/layouts/mailer.html.erb +13 -0
  65. data/examples/rails_example/app/views/layouts/mailer.text.erb +1 -0
  66. data/examples/rails_example/bin/bundle +3 -0
  67. data/examples/rails_example/bin/rails +4 -0
  68. data/examples/rails_example/bin/rake +4 -0
  69. data/examples/rails_example/bin/setup +38 -0
  70. data/examples/rails_example/bin/update +29 -0
  71. data/examples/rails_example/bin/yarn +11 -0
  72. data/examples/rails_example/config/application.rb +18 -0
  73. data/examples/rails_example/config/boot.rb +3 -0
  74. data/examples/rails_example/config/cable.yml +10 -0
  75. data/examples/rails_example/config/database.yml +25 -0
  76. data/examples/rails_example/config/environment.rb +5 -0
  77. data/examples/rails_example/config/environments/development.rb +54 -0
  78. data/examples/rails_example/config/environments/production.rb +91 -0
  79. data/examples/rails_example/config/environments/test.rb +42 -0
  80. data/examples/rails_example/config/initializers/application_controller_renderer.rb +6 -0
  81. data/examples/rails_example/config/initializers/assets.rb +14 -0
  82. data/examples/rails_example/config/initializers/backtrace_silencers.rb +7 -0
  83. data/examples/rails_example/config/initializers/cookies_serializer.rb +5 -0
  84. data/examples/rails_example/config/initializers/filter_parameter_logging.rb +4 -0
  85. data/examples/rails_example/config/initializers/inflections.rb +16 -0
  86. data/examples/rails_example/config/initializers/mime_types.rb +4 -0
  87. data/examples/rails_example/config/initializers/sandboxy.rb +7 -0
  88. data/examples/rails_example/config/initializers/wrap_parameters.rb +14 -0
  89. data/examples/rails_example/config/locales/en.yml +33 -0
  90. data/examples/rails_example/config/puma.rb +56 -0
  91. data/examples/rails_example/config/routes.rb +5 -0
  92. data/examples/rails_example/config/secrets.yml +32 -0
  93. data/examples/rails_example/config.ru +5 -0
  94. data/examples/rails_example/db/migrate/20170826222427_sandboxy_migration.rb +14 -0
  95. data/examples/rails_example/db/migrate/20170826223227_create_foos.rb +8 -0
  96. data/examples/rails_example/db/migrate/20170826223243_create_bars.rb +8 -0
  97. data/examples/rails_example/db/schema.rb +24 -0
  98. data/examples/rails_example/db/seeds.rb +7 -0
  99. data/examples/rails_example/lib/assets/.keep +0 -0
  100. data/examples/rails_example/lib/tasks/.keep +0 -0
  101. data/examples/rails_example/package.json +5 -0
  102. data/examples/rails_example/public/404.html +67 -0
  103. data/examples/rails_example/public/422.html +67 -0
  104. data/examples/rails_example/public/500.html +66 -0
  105. data/examples/rails_example/public/apple-touch-icon-precomposed.png +0 -0
  106. data/examples/rails_example/public/apple-touch-icon.png +0 -0
  107. data/examples/rails_example/public/favicon.ico +0 -0
  108. data/examples/rails_example/public/robots.txt +1 -0
  109. data/examples/rails_example/test/application_system_test_case.rb +5 -0
  110. data/examples/rails_example/test/controllers/.keep +0 -0
  111. data/examples/rails_example/test/controllers/bars_controller_test.rb +48 -0
  112. data/examples/rails_example/test/controllers/foos_controller_test.rb +48 -0
  113. data/examples/rails_example/test/fixtures/.keep +0 -0
  114. data/examples/rails_example/test/fixtures/bars.yml +11 -0
  115. data/examples/rails_example/test/fixtures/files/.keep +0 -0
  116. data/examples/rails_example/test/fixtures/foos.yml +11 -0
  117. data/examples/rails_example/test/helpers/.keep +0 -0
  118. data/examples/rails_example/test/integration/.keep +0 -0
  119. data/examples/rails_example/test/mailers/.keep +0 -0
  120. data/examples/rails_example/test/models/.keep +0 -0
  121. data/examples/rails_example/test/models/bar_test.rb +7 -0
  122. data/examples/rails_example/test/models/foo_test.rb +7 -0
  123. data/examples/rails_example/test/system/.keep +0 -0
  124. data/examples/rails_example/test/system/bars_test.rb +9 -0
  125. data/examples/rails_example/test/system/foos_test.rb +9 -0
  126. data/examples/rails_example/test/test_helper.rb +9 -0
  127. data/examples/rails_example/tmp/.keep +0 -0
  128. data/examples/rails_example/vendor/.keep +0 -0
  129. data/init.rb +1 -0
  130. data/lib/generators/sandboxy_generator.rb +44 -0
  131. data/lib/generators/templates/README.md +1 -0
  132. data/lib/generators/templates/initializer.rb.erb +7 -0
  133. data/lib/generators/templates/migration.rb.erb +14 -0
  134. data/lib/generators/templates/model.rb +7 -0
  135. data/lib/sandboxy/railtie.rb +15 -0
  136. data/lib/sandboxy/sandboxed.rb +52 -0
  137. data/lib/sandboxy/version.rb +5 -0
  138. data/lib/sandboxy.rb +10 -0
  139. data/sandboxy.gemspec +30 -0
  140. data/test/dummy30/Gemfile +1 -0
  141. data/test/dummy30/Rakefile +4 -0
  142. data/test/dummy30/app/models/application_record.rb +3 -0
  143. data/test/dummy30/app/models/shared_sandbox.rb +4 -0
  144. data/test/dummy30/app/models/some.rb +3 -0
  145. data/test/dummy30/app/models/user.rb +3 -0
  146. data/test/dummy30/config/application.rb +14 -0
  147. data/test/dummy30/config/boot.rb +10 -0
  148. data/test/dummy30/config/database.yml +6 -0
  149. data/test/dummy30/config/environment.rb +5 -0
  150. data/test/dummy30/config/environments/development.rb +7 -0
  151. data/test/dummy30/config/environments/test.rb +6 -0
  152. data/test/dummy30/config/initializers/sandboxy.rb +1 -0
  153. data/test/dummy30/config/initializers/secret_token.rb +1 -0
  154. data/test/dummy30/config/initializers/session_store.rb +1 -0
  155. data/test/dummy30/config/locales/en.yml +2 -0
  156. data/test/dummy30/config/routes.rb +2 -0
  157. data/test/dummy30/config.ru +2 -0
  158. data/test/factories/somes.rb +17 -0
  159. data/test/factories/users.rb +5 -0
  160. data/test/sandbox_test.rb +41 -0
  161. data/test/sandboxed_test.rb +58 -0
  162. data/test/schema.rb +18 -0
  163. data/test/test_helper.rb +22 -0
  164. metadata +307 -0
@@ -0,0 +1,48 @@
1
+ require 'test_helper'
2
+
3
+ class BarsControllerTest < ActionDispatch::IntegrationTest
4
+ setup do
5
+ @bar = bars(:one)
6
+ end
7
+
8
+ test "should get index" do
9
+ get bars_url
10
+ assert_response :success
11
+ end
12
+
13
+ test "should get new" do
14
+ get new_bar_url
15
+ assert_response :success
16
+ end
17
+
18
+ test "should create bar" do
19
+ assert_difference('Bar.count') do
20
+ post bars_url, params: { bar: { } }
21
+ end
22
+
23
+ assert_redirected_to bar_url(Bar.last)
24
+ end
25
+
26
+ test "should show bar" do
27
+ get bar_url(@bar)
28
+ assert_response :success
29
+ end
30
+
31
+ test "should get edit" do
32
+ get edit_bar_url(@bar)
33
+ assert_response :success
34
+ end
35
+
36
+ test "should update bar" do
37
+ patch bar_url(@bar), params: { bar: { } }
38
+ assert_redirected_to bar_url(@bar)
39
+ end
40
+
41
+ test "should destroy bar" do
42
+ assert_difference('Bar.count', -1) do
43
+ delete bar_url(@bar)
44
+ end
45
+
46
+ assert_redirected_to bars_url
47
+ end
48
+ end
@@ -0,0 +1,48 @@
1
+ require 'test_helper'
2
+
3
+ class FoosControllerTest < ActionDispatch::IntegrationTest
4
+ setup do
5
+ @foo = foos(:one)
6
+ end
7
+
8
+ test "should get index" do
9
+ get foos_url
10
+ assert_response :success
11
+ end
12
+
13
+ test "should get new" do
14
+ get new_foo_url
15
+ assert_response :success
16
+ end
17
+
18
+ test "should create foo" do
19
+ assert_difference('Foo.count') do
20
+ post foos_url, params: { foo: { } }
21
+ end
22
+
23
+ assert_redirected_to foo_url(Foo.last)
24
+ end
25
+
26
+ test "should show foo" do
27
+ get foo_url(@foo)
28
+ assert_response :success
29
+ end
30
+
31
+ test "should get edit" do
32
+ get edit_foo_url(@foo)
33
+ assert_response :success
34
+ end
35
+
36
+ test "should update foo" do
37
+ patch foo_url(@foo), params: { foo: { } }
38
+ assert_redirected_to foo_url(@foo)
39
+ end
40
+
41
+ test "should destroy foo" do
42
+ assert_difference('Foo.count', -1) do
43
+ delete foo_url(@foo)
44
+ end
45
+
46
+ assert_redirected_to foos_url
47
+ end
48
+ end
File without changes
@@ -0,0 +1,11 @@
1
+ # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
2
+
3
+ # This model initially had no columns defined. If you add columns to the
4
+ # model remove the '{}' from the fixture names and add the columns immediately
5
+ # below each fixture, per the syntax in the comments below
6
+ #
7
+ one: {}
8
+ # column: value
9
+ #
10
+ two: {}
11
+ # column: value
File without changes
@@ -0,0 +1,11 @@
1
+ # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
2
+
3
+ # This model initially had no columns defined. If you add columns to the
4
+ # model remove the '{}' from the fixture names and add the columns immediately
5
+ # below each fixture, per the syntax in the comments below
6
+ #
7
+ one: {}
8
+ # column: value
9
+ #
10
+ two: {}
11
+ # column: value
File without changes
File without changes
File without changes
File without changes
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class BarTest < ActiveSupport::TestCase
4
+ # test "the truth" do
5
+ # assert true
6
+ # end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class FooTest < ActiveSupport::TestCase
4
+ # test "the truth" do
5
+ # assert true
6
+ # end
7
+ end
File without changes
@@ -0,0 +1,9 @@
1
+ require "application_system_test_case"
2
+
3
+ class BarsTest < ApplicationSystemTestCase
4
+ # test "visiting the index" do
5
+ # visit bars_url
6
+ #
7
+ # assert_selector "h1", text: "Bar"
8
+ # end
9
+ end
@@ -0,0 +1,9 @@
1
+ require "application_system_test_case"
2
+
3
+ class FoosTest < ApplicationSystemTestCase
4
+ # test "visiting the index" do
5
+ # visit foos_url
6
+ #
7
+ # assert_selector "h1", text: "Foo"
8
+ # end
9
+ end
@@ -0,0 +1,9 @@
1
+ require File.expand_path('../../config/environment', __FILE__)
2
+ require 'rails/test_help'
3
+
4
+ class ActiveSupport::TestCase
5
+ # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
6
+ fixtures :all
7
+
8
+ # Add more helper methods to be used by all tests here...
9
+ end
File without changes
File without changes
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'sandboxy'
@@ -0,0 +1,44 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ class SandboxyGenerator < Rails::Generators::Base
5
+
6
+ include Rails::Generators::Migration
7
+
8
+ source_root File.join File.dirname(__FILE__), 'templates'
9
+ desc 'Install sandboxy'
10
+ class_option :default, desc: 'Set the default $sandbox indicator', type: :boolean, default: false, aliases: '-d'
11
+
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
+ "%.3d" % (current_migration_number(dirname) + 1)
17
+ end
18
+ end
19
+
20
+ def create_migration_file
21
+ migration_template 'migration.rb.erb', 'db/migrate/sandboxy_migration.rb', migration_version: migration_version
22
+ end
23
+
24
+ def create_initializer
25
+ template 'initializer.rb.erb', 'config/initializers/sandboxy.rb'
26
+ end
27
+
28
+ def create_model
29
+ template 'model.rb', 'app/models/sandbox.rb'
30
+ end
31
+
32
+ def show_readme
33
+ readme 'README.md'
34
+ end
35
+
36
+ private
37
+
38
+ def migration_version
39
+ if Rails.version >= '5.0.0'
40
+ "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
41
+ end
42
+ end
43
+
44
+ end
@@ -0,0 +1 @@
1
+ Now run `rails db:migrate` to add sandboxy to your database.
@@ -0,0 +1,7 @@
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
@@ -0,0 +1,14 @@
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
5
+ t.timestamps
6
+ 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
+ end
14
+ end
@@ -0,0 +1,7 @@
1
+ class Sandbox < ActiveRecord::Base
2
+
3
+ self.table_name = 'sandboxy'
4
+
5
+ belongs_to :sandboxed, polymorphic: true
6
+
7
+ end
@@ -0,0 +1,15 @@
1
+ require 'rails'
2
+ require 'sandboxy/sandboxed'
3
+
4
+
5
+ module Sandboxy
6
+ class Railtie < Rails::Railtie
7
+
8
+ initializer 'sandboxy.active_record' do
9
+ ActiveSupport.on_load :active_record do
10
+ include Sandboxy::Sandboxed
11
+ end
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,52 @@
1
+ module Sandboxy
2
+ module Sandboxed
3
+
4
+ def self.included base
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+
10
+ def sandboxy
11
+ has_one :sandbox, as: :sandboxed, dependent: :destroy
12
+ include Sandboxy::Sandboxed::InstanceMethods
13
+
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 }
24
+ scope :desandbox, -> { unscope(:joins, :where).all }
25
+
26
+ # before_save :make_sandboxed # -> should be handled automatically through default_scope
27
+ end
28
+
29
+ end
30
+
31
+ module InstanceMethods
32
+
33
+ def make_sandboxed
34
+ self.build_sandbox unless self.sandbox.present?
35
+ end
36
+
37
+ def make_live
38
+ self.sandbox.destroy if self.sandbox.present?
39
+ end
40
+
41
+ def sandboxed?
42
+ self.sandbox.present?
43
+ end
44
+
45
+ def live?
46
+ !self.sandbox.present?
47
+ end
48
+
49
+ end
50
+
51
+ end
52
+ end
@@ -0,0 +1,5 @@
1
+ module Sandboxy
2
+
3
+ VERSION = '1.0.0'
4
+
5
+ end
data/lib/sandboxy.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'sandboxy/version'
2
+
3
+
4
+ module ActsAsFavoritor
5
+
6
+ autoload :Sandboxed, 'sandboxy/sandboxed'
7
+
8
+ require 'sandboxy/railtie' # if defined?(Rails)
9
+
10
+ end
data/sandboxy.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path(File.join('..', 'lib', 'sandboxy', 'version'), __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.name = 'sandboxy'
6
+ gem.version = Sandboxy::VERSION
7
+ gem.platform = Gem::Platform::RUBY
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.'
10
+ gem.authors = 'Slooob'
11
+ gem.email = 'developer@slooob.com'
12
+ gem.homepage = 'https://developer.slooob.com/open-source'
13
+ gem.license = 'MIT'
14
+
15
+ gem.files = `git ls-files`.split("\n")
16
+ gem.require_paths = ['lib']
17
+
18
+ gem.post_install_message = IO.read('INSTALL.md')
19
+
20
+ gem.required_ruby_version = '>= 2.0'
21
+
22
+ gem.add_dependency 'activerecord', '>= 4.0'
23
+
24
+ gem.add_development_dependency 'sqlite3', '~> 1.3'
25
+ gem.add_development_dependency 'shoulda_create', '~> 0.0'
26
+ gem.add_development_dependency 'shoulda', '~> 3.5'
27
+ gem.add_development_dependency 'factory_girl', '~> 4.8'
28
+ gem.add_development_dependency 'rails', '>= 4.0'
29
+ gem.add_development_dependency 'tzinfo-data', '~> 1.2017'
30
+ end
@@ -0,0 +1 @@
1
+ gem 'rails', '~> 5.1.3'
@@ -0,0 +1,4 @@
1
+ require File.expand_path '../config/application', __FILE__
2
+ require 'rake'
3
+
4
+ Dummy::Application.load_tasks
@@ -0,0 +1,3 @@
1
+ class ApplicationRecord < ActiveRecord::Base
2
+ self.abstract_class = true
3
+ end
@@ -0,0 +1,4 @@
1
+ class SharedSandbox < ApplicationRecord
2
+ self.abstract_class = true
3
+ sandboxy
4
+ end
@@ -0,0 +1,3 @@
1
+ class Some < SharedSandbox
2
+ validates_presence_of :name
3
+ end
@@ -0,0 +1,3 @@
1
+ class User < SharedSandbox
2
+ validates_presence_of :name
3
+ end
@@ -0,0 +1,14 @@
1
+ require File.expand_path '../boot', __FILE__
2
+
3
+ require 'active_model/railtie'
4
+ require 'active_record/railtie'
5
+
6
+ Bundler.require
7
+ require 'sandboxy'
8
+
9
+ module Dummy
10
+ class Application < Rails::Application
11
+ config.encoding = 'utf-8'
12
+ config.filter_parameters += [:password]
13
+ end
14
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ gemfile = File.expand_path '../../../../Gemfile', __FILE__
3
+
4
+ if File.exist?(gemfile)
5
+ ENV['BUNDLE_GEMFILE'] = gemfile
6
+ require 'bundler'
7
+ Bundler.setup
8
+ end
9
+
10
+ $:.unshift File.expand_path '../../../../lib', __FILE__
@@ -0,0 +1,6 @@
1
+ test:
2
+ adapter: sqlite3
3
+ database: ':memory:'
4
+ development:
5
+ adapter: sqlite3
6
+ database: ':memory:'
@@ -0,0 +1,5 @@
1
+ # Load the rails application
2
+ require File.expand_path '../application', __FILE__
3
+
4
+ # Initialize the rails application
5
+ Dummy::Application.initialize!
@@ -0,0 +1,7 @@
1
+ Dummy::Application.configure do
2
+ config.cache_classes = false
3
+ config.whiny_nils = true
4
+ config.consider_all_requests_local = true
5
+ config.active_support.deprecation = :log
6
+ config.eager_load = false
7
+ end
@@ -0,0 +1,6 @@
1
+ Dummy::Application.configure do
2
+ config.cache_classes = true
3
+ config.consider_all_requests_local = true
4
+ config.active_support.deprecation = :stderr
5
+ config.eager_load = false
6
+ end
@@ -0,0 +1 @@
1
+ $sandbox = false
@@ -0,0 +1 @@
1
+ Dummy::Application.config.secret_token = '7b0ce915dc4c2ee60581c2769798abb5e4078292ad23670fc8d728953fc13aec19863558873234816b58a54f6a35be58b2b0a26749a7dfddcd2f02ee82d7e94f'
@@ -0,0 +1 @@
1
+ Dummy::Application.config.session_store :cookie_store, key: '_dummy_session'
@@ -0,0 +1,2 @@
1
+ en:
2
+ hello: 'Hello world'
@@ -0,0 +1,2 @@
1
+ Dummy::Application.routes.draw do
2
+ end
@@ -0,0 +1,2 @@
1
+ require ::File.expand_path '../config/environment', __FILE__
2
+ run Dummy::Application
@@ -0,0 +1,17 @@
1
+ FactoryGirl.define do
2
+ factory :post, class: Some do |b|
3
+ b.name 'Post'
4
+ end
5
+
6
+ factory :purchase, class: Some do |b|
7
+ b.name 'Purchase'
8
+ end
9
+
10
+ factory :book, class: Some do |b|
11
+ b.name 'Book'
12
+ end
13
+
14
+ factory :receipt, class: Some do |b|
15
+ b.name 'Receipt'
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ FactoryGirl.define do
2
+ factory :sam, class: User do |u|
3
+ u.name 'Sam'
4
+ end
5
+ end
@@ -0,0 +1,41 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class SandboxTest < ActiveSupport::TestCase
4
+
5
+ context '#sandbox' do
6
+ setup do
7
+ @post = FactoryGirl.create :post
8
+ @purchase = FactoryGirl.create :purchase
9
+ $sandbox = true
10
+ @book = FactoryGirl.create :book
11
+ @receipt = FactoryGirl.create :receipt
12
+ end
13
+
14
+ context 'desandbox' do
15
+ should 'return all records' do
16
+ assert_equal Some.unscope(:joins, :where).all, Some.desandbox
17
+ end
18
+ end
19
+
20
+ context '#live' do
21
+ setup do
22
+ $sandbox = false
23
+ end
24
+
25
+ should 'return all live records' do
26
+ assert_equal Some.all, Some.live
27
+ end
28
+ end
29
+
30
+ context '#sandboxed' do
31
+ setup do
32
+ $sandbox = true
33
+ end
34
+
35
+ should 'return all sandboxed records' do
36
+ assert_equal Some.all, Some.sandboxed
37
+ end
38
+ end
39
+ end
40
+
41
+ end
@@ -0,0 +1,58 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class SandboxedTest < ActiveSupport::TestCase
4
+
5
+ context 'instance methods' do
6
+ setup do
7
+ @post = FactoryGirl.create :post
8
+ end
9
+
10
+ should 'be defined' do
11
+ assert @post.respond_to? :make_sandboxed
12
+ assert @post.respond_to? :make_live
13
+ assert @post.respond_to? :sandboxed?
14
+ assert @post.respond_to? :live?
15
+ end
16
+ end
17
+
18
+ context 'sandboxed' do
19
+ setup do
20
+ @post = FactoryGirl.create :post
21
+ @purchase = FactoryGirl.create :purchase
22
+ $sandbox = true
23
+ @book = FactoryGirl.create :book
24
+ @receipt = FactoryGirl.create :receipt
25
+ end
26
+
27
+ context '#sandboxed?' do
28
+ should 'be true for @book & @receipt' do
29
+ @book.make_sandboxed ##### SHOULD NOT BE NECESSARY #####
30
+ @receipt.make_sandboxed ##### SHOULD NOT BE NECESSARY #####
31
+ assert_equal true, @book.sandboxed?
32
+ assert_equal true, @receipt.sandboxed?
33
+ end
34
+ end
35
+
36
+ context '#live?' do
37
+ should 'be true for @post & @purchase' do
38
+ assert_equal true, @post.live?
39
+ assert_equal true, @purchase.live?
40
+ end
41
+ end
42
+
43
+ context '#make_sandboxed' do
44
+ should 'move record to sandboxed environment' do
45
+ @purchase.make_sandboxed
46
+ assert_equal true, @purchase.sandboxed?
47
+ end
48
+ end
49
+
50
+ context '#make_live' do
51
+ should 'move record to live environment' do
52
+ @receipt.make_live
53
+ assert_equal true, @receipt.live?
54
+ end
55
+ end
56
+ end
57
+
58
+ end
data/test/schema.rb ADDED
@@ -0,0 +1,18 @@
1
+ ActiveRecord::Schema.define version: 0 do
2
+
3
+ create_table :sandboxy, force: true do |t|
4
+ t.integer :sandboxed_id, null: false
5
+ t.string :sandboxed_type, null: false
6
+ t.datetime :created_at
7
+ t.datetime :updated_at
8
+ end
9
+
10
+ create_table :users, force: true do |t|
11
+ t.column :name, :string
12
+ end
13
+
14
+ create_table :somes, force: true do |t|
15
+ t.column :name, :string
16
+ end
17
+
18
+ end