Class: DatabaseRewinder::Cleaner
- Inherits:
-
Object
- Object
- DatabaseRewinder::Cleaner
- Defined in:
- lib/database_rewinder/cleaner.rb
Instance Attribute Summary collapse
-
#connection_name ⇒ Object
Returns the value of attribute connection_name.
-
#db ⇒ Object
Returns the value of attribute db.
-
#inserted_tables ⇒ Object
Returns the value of attribute inserted_tables.
-
#pool ⇒ Object
Returns the value of attribute pool.
Instance Method Summary collapse
- #clean ⇒ Object
- #clean_all ⇒ Object
- #clean_with(_strategy, only: nil, except: nil) ⇒ Object
-
#initialize(db: nil, connection_name: nil, only: nil, except: nil) ⇒ Cleaner
constructor
A new instance of Cleaner.
-
#strategy=(args) ⇒ Object
for database_cleaner compat.
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_name ⇒ Object
Returns the value of attribute connection_name.
3 4 5 |
# File 'lib/database_rewinder/cleaner.rb', line 3 def connection_name @connection_name end |
#db ⇒ Object
Returns the value of attribute db.
3 4 5 |
# File 'lib/database_rewinder/cleaner.rb', line 3 def db @db end |
#inserted_tables ⇒ Object
Returns the value of attribute inserted_tables.
3 4 5 |
# File 'lib/database_rewinder/cleaner.rb', line 3 def inserted_tables @inserted_tables end |
#pool ⇒ Object
Returns the value of attribute pool.
3 4 5 |
# File 'lib/database_rewinder/cleaner.rb', line 3 def pool @pool end |
Instance Method Details
#clean ⇒ Object
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_all ⇒ Object
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) = args.is_a?(Array) ? args. : {} @only += Array([:only]) unless [:only].blank? @except += Array([:except]) unless [:except].blank? end |