Permalink
Browse files

Add a strategy for @javascript database cleanups #166

  • Loading branch information...
1 parent baea748 commit 14a9e4c002538c167d6d6c390d80bd98b4b5349e @mattwynne mattwynne committed Nov 3, 2011
Showing with 65 additions and 12 deletions.
  1. +1 −0 lib/cucumber/rails.rb
  2. +62 −0 lib/cucumber/rails/database.rb
  3. +2 −12 lib/cucumber/rails/hooks/active_record.rb
@@ -21,6 +21,7 @@
require 'cucumber/rails/world'
require 'cucumber/rails/hooks'
require 'cucumber/rails/capybara'
+ require 'cucumber/rails/database'
require 'cucumber/web/tableish'
else
@@ -0,0 +1,62 @@
+module Cucumber
+ module Rails
+ module Database
+ class << self
+ def javascript_strategy=(strategy)
+ strategy_type = map[strategy] || raise("The strategy '#{strategy}' is not understood. Please use one of #{map.keys.join(',')}")
+ @strategy = strategy_type.new
+ end
+
+ def before_js
+ @strategy.before_js
+ end
+
+ def before_non_js
+ @strategy.before_non_js
+ end
+ private
+
+ def map
+ {
+ :truncation => TruncationStrategy,
+ :shared_connection => SharedConnectionStrategy,
+ :transaction => SharedConnectionStrategy
+ }
+ end
+ end
+
+ class SharedConnectionStrategy
+ def before_js
+ # Forces all threads to share a connection on a per-model basis,
+ # as connections may vary per model as per establish_connection. This works
+ # on Capybara because it starts the web server in a thread.
+ ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection
+ ActiveRecord::Base.descendants.each do |model|
+ model.shared_connection = model.connection
+ end
+ end
+
+ def before_non_js
+ # Do not use a shared connection unless we're in a @javascript scenario
+ ActiveRecord::Base.shared_connection = nil
+ ActiveRecord::Base.descendants.each do |model|
+ model.shared_connection = nil
+ end
+ end
+ end
+
+ class TruncationStrategy
+ def before_js
+ @original_strategy = DatabaseCleaner.strategy
+ DatabaseCleaner.strategy = :truncation
+ end
+
+ def before_non_js
+ DatabaseCleaner.strategy = @original_strategy
+ end
+ end
+
+ self.javascript_strategy = :transaction
+ end
+ end
+end
@@ -8,20 +8,10 @@ def self.connection
end
Before('@javascript') do
- # Forces all threads to share a connection on a per-model basis,
- # as connections may vary per model as per establish_connection. This works
- # on Capybara because it starts the web server in a thread.
- ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection
- ActiveRecord::Base.descendants.each do |model|
- model.shared_connection = model.connection
- end
+ Cucumber::Rails::Database.before_js
end
Before('~@javascript') do
- # Do not use a shared connection unless we're in a @javascript scenario
- ActiveRecord::Base.shared_connection = nil
- ActiveRecord::Base.descendants.each do |model|
- model.shared_connection = nil
- end
+ Cucumber::Rails::Database.before_non_js
end
end

0 comments on commit 14a9e4c

Please sign in to comment.