sandboxy 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/issue_template.md +14 -0
- data/.github/pull_request_template.md +21 -0
- data/.gitignore +8 -0
- data/.travis.yml +3 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +46 -0
- data/CONTRIBUTING.md +1 -0
- data/DEPRECATIONS.md +3 -0
- data/Gemfile +9 -0
- data/INSTALL.md +3 -0
- data/LICENSE +21 -0
- data/README.md +175 -0
- data/Rakefile +16 -0
- data/examples/rails_example/.gitignore +23 -0
- data/examples/rails_example/Gemfile +53 -0
- data/examples/rails_example/README.md +24 -0
- data/examples/rails_example/Rakefile +6 -0
- data/examples/rails_example/app/assets/config/manifest.js +3 -0
- data/examples/rails_example/app/assets/images/.keep +0 -0
- data/examples/rails_example/app/assets/javascripts/application.js +15 -0
- data/examples/rails_example/app/assets/javascripts/bars.coffee +3 -0
- data/examples/rails_example/app/assets/javascripts/cable.js +13 -0
- data/examples/rails_example/app/assets/javascripts/channels/.keep +0 -0
- data/examples/rails_example/app/assets/javascripts/foos.coffee +3 -0
- data/examples/rails_example/app/assets/stylesheets/application.css +15 -0
- data/examples/rails_example/app/assets/stylesheets/bars.scss +3 -0
- data/examples/rails_example/app/assets/stylesheets/foos.scss +3 -0
- data/examples/rails_example/app/assets/stylesheets/scaffolds.scss +84 -0
- data/examples/rails_example/app/channels/application_cable/channel.rb +4 -0
- data/examples/rails_example/app/channels/application_cable/connection.rb +4 -0
- data/examples/rails_example/app/controllers/application_controller.rb +3 -0
- data/examples/rails_example/app/controllers/bars_controller.rb +74 -0
- data/examples/rails_example/app/controllers/concerns/.keep +0 -0
- data/examples/rails_example/app/controllers/foos_controller.rb +74 -0
- data/examples/rails_example/app/helpers/application_helper.rb +2 -0
- data/examples/rails_example/app/helpers/bars_helper.rb +2 -0
- data/examples/rails_example/app/helpers/foos_helper.rb +2 -0
- data/examples/rails_example/app/jobs/application_job.rb +2 -0
- data/examples/rails_example/app/mailers/application_mailer.rb +4 -0
- data/examples/rails_example/app/models/application_record.rb +3 -0
- data/examples/rails_example/app/models/bar.rb +2 -0
- data/examples/rails_example/app/models/concerns/.keep +0 -0
- data/examples/rails_example/app/models/foo.rb +2 -0
- data/examples/rails_example/app/models/sandbox.rb +7 -0
- data/examples/rails_example/app/models/shared_sandbox.rb +3 -0
- data/examples/rails_example/app/views/bars/_bar.json.jbuilder +2 -0
- data/examples/rails_example/app/views/bars/_form.html.erb +17 -0
- data/examples/rails_example/app/views/bars/edit.html.erb +6 -0
- data/examples/rails_example/app/views/bars/index.html.erb +25 -0
- data/examples/rails_example/app/views/bars/index.json.jbuilder +1 -0
- data/examples/rails_example/app/views/bars/new.html.erb +5 -0
- data/examples/rails_example/app/views/bars/show.html.erb +4 -0
- data/examples/rails_example/app/views/bars/show.json.jbuilder +1 -0
- data/examples/rails_example/app/views/foos/_foo.json.jbuilder +2 -0
- data/examples/rails_example/app/views/foos/_form.html.erb +17 -0
- data/examples/rails_example/app/views/foos/edit.html.erb +6 -0
- data/examples/rails_example/app/views/foos/index.html.erb +25 -0
- data/examples/rails_example/app/views/foos/index.json.jbuilder +1 -0
- data/examples/rails_example/app/views/foos/new.html.erb +5 -0
- data/examples/rails_example/app/views/foos/show.html.erb +4 -0
- data/examples/rails_example/app/views/foos/show.json.jbuilder +1 -0
- data/examples/rails_example/app/views/layouts/application.html.erb +14 -0
- data/examples/rails_example/app/views/layouts/mailer.html.erb +13 -0
- data/examples/rails_example/app/views/layouts/mailer.text.erb +1 -0
- data/examples/rails_example/bin/bundle +3 -0
- data/examples/rails_example/bin/rails +4 -0
- data/examples/rails_example/bin/rake +4 -0
- data/examples/rails_example/bin/setup +38 -0
- data/examples/rails_example/bin/update +29 -0
- data/examples/rails_example/bin/yarn +11 -0
- data/examples/rails_example/config/application.rb +18 -0
- data/examples/rails_example/config/boot.rb +3 -0
- data/examples/rails_example/config/cable.yml +10 -0
- data/examples/rails_example/config/database.yml +25 -0
- data/examples/rails_example/config/environment.rb +5 -0
- data/examples/rails_example/config/environments/development.rb +54 -0
- data/examples/rails_example/config/environments/production.rb +91 -0
- data/examples/rails_example/config/environments/test.rb +42 -0
- data/examples/rails_example/config/initializers/application_controller_renderer.rb +6 -0
- data/examples/rails_example/config/initializers/assets.rb +14 -0
- data/examples/rails_example/config/initializers/backtrace_silencers.rb +7 -0
- data/examples/rails_example/config/initializers/cookies_serializer.rb +5 -0
- data/examples/rails_example/config/initializers/filter_parameter_logging.rb +4 -0
- data/examples/rails_example/config/initializers/inflections.rb +16 -0
- data/examples/rails_example/config/initializers/mime_types.rb +4 -0
- data/examples/rails_example/config/initializers/sandboxy.rb +7 -0
- data/examples/rails_example/config/initializers/wrap_parameters.rb +14 -0
- data/examples/rails_example/config/locales/en.yml +33 -0
- data/examples/rails_example/config/puma.rb +56 -0
- data/examples/rails_example/config/routes.rb +5 -0
- data/examples/rails_example/config/secrets.yml +32 -0
- data/examples/rails_example/config.ru +5 -0
- data/examples/rails_example/db/migrate/20170826222427_sandboxy_migration.rb +14 -0
- data/examples/rails_example/db/migrate/20170826223227_create_foos.rb +8 -0
- data/examples/rails_example/db/migrate/20170826223243_create_bars.rb +8 -0
- data/examples/rails_example/db/schema.rb +24 -0
- data/examples/rails_example/db/seeds.rb +7 -0
- data/examples/rails_example/lib/assets/.keep +0 -0
- data/examples/rails_example/lib/tasks/.keep +0 -0
- data/examples/rails_example/package.json +5 -0
- data/examples/rails_example/public/404.html +67 -0
- data/examples/rails_example/public/422.html +67 -0
- data/examples/rails_example/public/500.html +66 -0
- 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 +1 -0
- data/examples/rails_example/test/application_system_test_case.rb +5 -0
- data/examples/rails_example/test/controllers/.keep +0 -0
- data/examples/rails_example/test/controllers/bars_controller_test.rb +48 -0
- data/examples/rails_example/test/controllers/foos_controller_test.rb +48 -0
- data/examples/rails_example/test/fixtures/.keep +0 -0
- data/examples/rails_example/test/fixtures/bars.yml +11 -0
- data/examples/rails_example/test/fixtures/files/.keep +0 -0
- data/examples/rails_example/test/fixtures/foos.yml +11 -0
- 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/bar_test.rb +7 -0
- data/examples/rails_example/test/models/foo_test.rb +7 -0
- data/examples/rails_example/test/system/.keep +0 -0
- data/examples/rails_example/test/system/bars_test.rb +9 -0
- data/examples/rails_example/test/system/foos_test.rb +9 -0
- data/examples/rails_example/test/test_helper.rb +9 -0
- data/examples/rails_example/tmp/.keep +0 -0
- data/examples/rails_example/vendor/.keep +0 -0
- data/init.rb +1 -0
- data/lib/generators/sandboxy_generator.rb +44 -0
- data/lib/generators/templates/README.md +1 -0
- data/lib/generators/templates/initializer.rb.erb +7 -0
- data/lib/generators/templates/migration.rb.erb +14 -0
- data/lib/generators/templates/model.rb +7 -0
- data/lib/sandboxy/railtie.rb +15 -0
- data/lib/sandboxy/sandboxed.rb +52 -0
- data/lib/sandboxy/version.rb +5 -0
- data/lib/sandboxy.rb +10 -0
- data/sandboxy.gemspec +30 -0
- data/test/dummy30/Gemfile +1 -0
- data/test/dummy30/Rakefile +4 -0
- data/test/dummy30/app/models/application_record.rb +3 -0
- data/test/dummy30/app/models/shared_sandbox.rb +4 -0
- data/test/dummy30/app/models/some.rb +3 -0
- data/test/dummy30/app/models/user.rb +3 -0
- data/test/dummy30/config/application.rb +14 -0
- data/test/dummy30/config/boot.rb +10 -0
- data/test/dummy30/config/database.yml +6 -0
- data/test/dummy30/config/environment.rb +5 -0
- data/test/dummy30/config/environments/development.rb +7 -0
- data/test/dummy30/config/environments/test.rb +6 -0
- data/test/dummy30/config/initializers/sandboxy.rb +1 -0
- data/test/dummy30/config/initializers/secret_token.rb +1 -0
- data/test/dummy30/config/initializers/session_store.rb +1 -0
- data/test/dummy30/config/locales/en.yml +2 -0
- data/test/dummy30/config/routes.rb +2 -0
- data/test/dummy30/config.ru +2 -0
- data/test/factories/somes.rb +17 -0
- data/test/factories/users.rb +5 -0
- data/test/sandbox_test.rb +41 -0
- data/test/sandboxed_test.rb +58 -0
- data/test/schema.rb +18 -0
- data/test/test_helper.rb +22 -0
- 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
|
File without changes
|
@@ -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,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
|
data/lib/sandboxy.rb
ADDED
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,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 @@
|
|
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,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,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
|