Class: DatabaseCleaner::ActiveRecord::Truncation

Inherits:
Base
  • Object
show all
Defined in:
lib/database_cleaner/active_record/truncation.rb

Direct Known Subclasses

Deletion

Instance Attribute Summary

Attributes inherited from Base

#connection_hash

Instance Method Summary collapse

Methods inherited from Base

#connection_class, #db=, exclusion_condition, migration_table_name

Constructor Details

#initialize(opts = {}) ⇒ Truncation

Returns a new instance of Truncation.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/database_cleaner/active_record/truncation.rb', line 7

def initialize(opts={})
  if !opts.empty? && !(opts.keys - [:only, :except, :pre_count, :cache_tables, :reset_ids]).empty?
    raise ArgumentError, "The only valid options are :only, :except, :pre_count, :reset_ids, and :cache_tables. You specified #{opts.keys.join(',')}."
  end

  @only = Array(opts[:only]).dup
  @except = Array(opts[:except]).dup

  @reset_ids = opts[:reset_ids]
  @pre_count = opts[:pre_count]
  @cache_tables = opts.has_key?(:cache_tables) ? !!opts[:cache_tables] : true
end

Instance Method Details

#cleanObject



20
21
22
23
24
25
26
27
28
# File 'lib/database_cleaner/active_record/truncation.rb', line 20

def clean
  connection.disable_referential_integrity do
    if pre_count? && connection.respond_to?(:pre_count_truncate_tables)
      connection.pre_count_truncate_tables(tables_to_clean(connection))
    else
      connection.truncate_tables(tables_to_clean(connection))
    end
  end
end