Class: DatabaseRewinder::Cleaner

Inherits:
Object
  • Object
show all
Defined in:
lib/database_rewinder/cleaner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db: nil, connection_name: nil, only: nil, except: nil) ⇒ Cleaner

Returns a new instance of Cleaner.



5
6
7
8
# File 'lib/database_rewinder/cleaner.rb', line 5

def initialize(db: nil, connection_name: nil, only: nil, except: nil)
  @db, @connection_name, @only, @except = db, connection_name, Array(only), Array(except)
  reset
end

Instance Attribute Details

#connection_nameObject

Returns the value of attribute connection_name.



3
4
5
# File 'lib/database_rewinder/cleaner.rb', line 3

def connection_name
  @connection_name
end

#dbObject

Returns the value of attribute db.



3
4
5
# File 'lib/database_rewinder/cleaner.rb', line 3

def db
  @db
end

#inserted_tablesObject

Returns the value of attribute inserted_tables.



3
4
5
# File 'lib/database_rewinder/cleaner.rb', line 3

def inserted_tables
  @inserted_tables
end

#poolObject

Returns the value of attribute pool.



3
4
5
# File 'lib/database_rewinder/cleaner.rb', line 3

def pool
  @pool
end

Instance Method Details

#cleanObject



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/database_rewinder/cleaner.rb', line 10

def clean
  return if !pool || inserted_tables.empty?

  # When the application uses multiple database connections, a connection
  # pool used in test could be already removed (i.e., pool.connected? = false).
  # In this case, we have to reconnect to the database to clean inserted
  # tables.
  with_automatic_reconnect(pool) do
    delete_all (ar_conn = pool.connection), DatabaseRewinder.all_table_names(ar_conn) & inserted_tables
  end
  reset
end

#clean_allObject



23
24
25
26
27
28
# File 'lib/database_rewinder/cleaner.rb', line 23

def clean_all
  ar_conn = pool ? pool.connection : ActiveRecord::Base.connection

  delete_all ar_conn, DatabaseRewinder.all_table_names(ar_conn)
  reset
end

#clean_with(_strategy, only: nil, except: nil) ⇒ Object



30
31
32
33
34
# File 'lib/database_rewinder/cleaner.rb', line 30

def clean_with(_strategy, only: nil, except: nil, **)
  @only += Array(only) unless only.blank?
  @except += Array(except) unless except.blank?
  clean_all
end

#strategy=(args) ⇒ Object

for database_cleaner compat



37
38
39
40
41
# File 'lib/database_rewinder/cleaner.rb', line 37

def strategy=(args)
  options = args.is_a?(Array) ? args.extract_options! : {}
  @only += Array(options[:only]) unless options[:only].blank?
  @except += Array(options[:except]) unless options[:except].blank?
end