This repository has been archived by the owner on Jan 1, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Autoconnect singleton and determine Playground connection with it.
* Refactor Playground#initialize to use #autoconnect. * Closes #81.
- Loading branch information
1 parent
817a21e
commit 2a37b07
Showing
5 changed files
with
122 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
module Vanity | ||
# A singleton responsible for determining if the playground should connect | ||
# to the datastore. | ||
module Autoconnect | ||
|
||
BLACKLISTED_RAILS_RAKE_TASKS = [ | ||
'about', | ||
'assets:clean', | ||
'assets:clobber', | ||
'assets:environment', | ||
'assets:precompile', | ||
'assets:precompile:all', | ||
'db:create', | ||
'db:drop', | ||
'db:fixtures:load', | ||
'db:migrate', | ||
'db:migrate:status', | ||
'db:rollback', | ||
'db:schema:cache:clear', | ||
'db:schema:cache:dump', | ||
'db:schema:dump', | ||
'db:schema:load', | ||
'db:seed', | ||
'db:setup', | ||
'db:structure:dump', | ||
'db:version', | ||
'doc:app', | ||
'log:clear', | ||
'middleware', | ||
'notes', | ||
'notes:custom', | ||
'rails:template', | ||
'rails:update', | ||
'routes', | ||
'secret', | ||
'stats', | ||
'time:zones:all', | ||
'tmp:clear', | ||
'tmp:create' | ||
] | ||
ENVIRONMENT_VANITY_DISABLED_FLAG = "VANITY_DISABLED" | ||
|
||
def self.playground_should_autoconnect? | ||
!environment_disabled? && !in_blacklisted_rake_task? | ||
end | ||
|
||
def self.environment_disabled? | ||
!!ENV[ENVIRONMENT_VANITY_DISABLED_FLAG] | ||
end | ||
|
||
def self.in_blacklisted_rake_task? | ||
!(current_rake_tasks & BLACKLISTED_RAILS_RAKE_TASKS).empty? | ||
end | ||
|
||
def self.current_rake_tasks | ||
::Rake.application.top_level_tasks | ||
rescue => e | ||
[] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require "test/test_helper" | ||
|
||
context ".playground_should_autoconnect?" do | ||
|
||
setup do | ||
end | ||
|
||
test "returns true by default" do | ||
autoconnect = Vanity::Autoconnect.playground_should_autoconnect? | ||
assert autoconnect == true | ||
end | ||
|
||
test "returns false if environment flag is set" do | ||
ENV['VANITY_DISABLED'] = '1' | ||
autoconnect = Vanity::Autoconnect.playground_should_autoconnect? | ||
assert autoconnect == false | ||
ENV['VANITY_DISABLED'] = nil | ||
end | ||
|
||
test "returns false if in assets:precompile rake task" do | ||
Rake.expects(:application).returns(stub(:top_level_tasks => ['assets:precompile'])) | ||
autoconnect = Vanity::Autoconnect.playground_should_autoconnect? | ||
assert autoconnect == false | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters