Skip to content
This repository
Browse code

Add a strategy for @javascript database cleanups #166

  • Loading branch information...
commit 14a9e4c002538c167d6d6c390d80bd98b4b5349e 1 parent baea748
Matt Wynne authored November 03, 2011
1  lib/cucumber/rails.rb
@@ -21,6 +21,7 @@
21 21
   require 'cucumber/rails/world'
22 22
   require 'cucumber/rails/hooks'
23 23
   require 'cucumber/rails/capybara'
  24
+  require 'cucumber/rails/database'
24 25
 
25 26
   require 'cucumber/web/tableish'
26 27
 else
62  lib/cucumber/rails/database.rb
... ...
@@ -0,0 +1,62 @@
  1
+module Cucumber
  2
+  module Rails
  3
+    module Database
  4
+      class << self
  5
+        def javascript_strategy=(strategy)
  6
+          strategy_type = map[strategy] || raise("The strategy '#{strategy}' is not understood. Please use one of #{map.keys.join(',')}")
  7
+          @strategy = strategy_type.new
  8
+        end
  9
+        
  10
+        def before_js
  11
+          @strategy.before_js
  12
+        end
  13
+        
  14
+        def before_non_js
  15
+          @strategy.before_non_js
  16
+        end
  17
+      private
  18
+      
  19
+        def map
  20
+          { 
  21
+            :truncation => TruncationStrategy,
  22
+            :shared_connection => SharedConnectionStrategy,
  23
+            :transaction => SharedConnectionStrategy
  24
+          }
  25
+        end
  26
+      end
  27
+      
  28
+      class SharedConnectionStrategy
  29
+        def before_js
  30
+          # Forces all threads to share a connection on a per-model basis,
  31
+          # as connections may vary per model as per establish_connection. This works
  32
+          # on Capybara because it starts the web server in a thread.
  33
+          ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection
  34
+          ActiveRecord::Base.descendants.each do |model|
  35
+            model.shared_connection = model.connection
  36
+          end
  37
+        end
  38
+        
  39
+        def before_non_js
  40
+          # Do not use a shared connection unless we're in a @javascript scenario
  41
+          ActiveRecord::Base.shared_connection = nil
  42
+          ActiveRecord::Base.descendants.each do |model|
  43
+            model.shared_connection = nil
  44
+          end
  45
+        end
  46
+      end
  47
+      
  48
+      class TruncationStrategy
  49
+        def before_js
  50
+          @original_strategy = DatabaseCleaner.strategy
  51
+          DatabaseCleaner.strategy = :truncation
  52
+        end
  53
+        
  54
+        def before_non_js
  55
+          DatabaseCleaner.strategy = @original_strategy
  56
+        end
  57
+      end
  58
+      
  59
+      self.javascript_strategy = :transaction
  60
+    end
  61
+  end
  62
+end
14  lib/cucumber/rails/hooks/active_record.rb
@@ -8,20 +8,10 @@ def self.connection
8 8
   end
9 9
   
10 10
   Before('@javascript') do
11  
-    # Forces all threads to share a connection on a per-model basis,
12  
-    # as connections may vary per model as per establish_connection. This works
13  
-    # on Capybara because it starts the web server in a thread.
14  
-    ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection
15  
-    ActiveRecord::Base.descendants.each do |model|
16  
-      model.shared_connection = model.connection
17  
-    end
  11
+    Cucumber::Rails::Database.before_js
18 12
   end
19 13
 
20 14
   Before('~@javascript') do
21  
-    # Do not use a shared connection unless we're in a @javascript scenario
22  
-    ActiveRecord::Base.shared_connection = nil
23  
-    ActiveRecord::Base.descendants.each do |model|
24  
-      model.shared_connection = nil
25  
-    end
  15
+    Cucumber::Rails::Database.before_non_js
26 16
   end
27 17
 end

0 notes on commit 14a9e4c

Please sign in to comment.
Something went wrong with that request. Please try again.