Module: FactoryBot::Preload
- Defined in:
- lib/factory_bot/preload.rb,
lib/factory_bot/preload/helpers.rb,
lib/factory_bot/preload/version.rb,
lib/factory_bot/preload/minitest.rb
Defined Under Namespace
Modules: Helpers, MinitestSetup, Version
Class Attribute Summary collapse
Class Method Summary
collapse
Class Attribute Details
.clean_with ⇒ Object
Returns the value of attribute clean_with.
23
24
25
|
# File 'lib/factory_bot/preload.rb', line 23
def clean_with
@clean_with
end
|
.factories ⇒ Object
Returns the value of attribute factories.
21
22
23
|
# File 'lib/factory_bot/preload.rb', line 21
def factories
@factories
end
|
.preloaders ⇒ Object
Returns the value of attribute preloaders.
20
21
22
|
# File 'lib/factory_bot/preload.rb', line 20
def preloaders
@preloaders
end
|
.record_ids ⇒ Object
Returns the value of attribute record_ids.
22
23
24
|
# File 'lib/factory_bot/preload.rb', line 22
def record_ids
@record_ids
end
|
Class Method Details
.active_record ⇒ Object
31
32
33
|
# File 'lib/factory_bot/preload.rb', line 31
def self.active_record
ActiveRecord::Base
end
|
.active_record_names ⇒ Object
68
69
70
71
72
|
# File 'lib/factory_bot/preload.rb', line 68
def self.active_record_names
names = active_record.descendants.collect(&:table_name).uniq.compact
reserved_tables = %w[ar_internal_metadata schema_migrations]
names.reject {|name| reserved_tables.include?(name) }
end
|
.clean(*names) ⇒ Object
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/factory_bot/preload.rb', line 49
def self.clean(*names)
query = case clean_with
when :truncation
try_truncation_query
when :deletion
"DELETE FROM %s"
else
raise "Couldn't find #{clean_with} clean type"
end
names = active_record_names if names.empty?
connection.disable_referential_integrity do
names.each do |table|
connection.execute(query % connection.quote_table_name(table))
end
end
end
|
.connection ⇒ Object
35
36
37
|
# File 'lib/factory_bot/preload.rb', line 35
def self.connection
active_record.connection
end
|
.reload_factories ⇒ Object
74
75
76
77
78
79
80
|
# File 'lib/factory_bot/preload.rb', line 74
def self.reload_factories
factories.each do |class_name, group|
group.each do |name, _factory|
factories[class_name][name] = nil
end
end
end
|
.run ⇒ Object
39
40
41
42
43
44
45
46
47
|
# File 'lib/factory_bot/preload.rb', line 39
def self.run
helper = Object.new.extend(Helpers)
connection.transaction requires_new: true do
preloaders.each do |block|
helper.instance_eval(&block)
end
end
end
|
.try_truncation_query ⇒ Object
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/factory_bot/preload.rb', line 82
def self.try_truncation_query
case connection.adapter_name
when "SQLite"
"DELETE FROM %s"
when "PostgreSQL"
"TRUNCATE TABLE %s RESTART IDENTITY CASCADE"
else
"TRUNCATE TABLE %s"
end
end
|