Module: RunningMan::ActiveRecordBlock::TestHelper

Included in:
RunningMan::ActiveRecordBlock
Defined in:
lib/running_man/active_record_block.rb

Instance Method Summary collapse

Instance Method Details

#clear_databaseObject



27
28
29
30
31
32
# File 'lib/running_man/active_record_block.rb', line 27

def clear_database
  conn = ActiveRecord::Base.connection
  conn.tables.each do |table|
    conn.delete "DELETE FROM #{table}"
  end
end

#setup_transactionObject

Open a new transaction before running any test.



10
11
12
13
14
15
16
# File 'lib/running_man/active_record_block.rb', line 10

def setup_transaction
  ActiveRecord::Base.connection.increment_open_transactions
  if ActiveRecord::Base.connection.respond_to?(:transaction_joinable=)
    ActiveRecord::Base.connection.transaction_joinable = false
  end
  ActiveRecord::Base.connection.begin_db_transaction
end

#teardown_transactionObject

Rollback our transaction, returning our fixtures to a pristine state.



19
20
21
22
23
24
25
# File 'lib/running_man/active_record_block.rb', line 19

def teardown_transaction
  if ActiveRecord::Base.connection.open_transactions != 0
    ActiveRecord::Base.connection.rollback_db_transaction
    ActiveRecord::Base.connection.decrement_open_transactions
  end
  ActiveRecord::Base.clear_active_connections!
end