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
-
.clean_with ⇒ Object
Returns the value of attribute clean_with.
-
.default_helper_name ⇒ Object
Returns the value of attribute default_helper_name.
-
.factories ⇒ Object
Returns the value of attribute factories.
-
.helper_name ⇒ Object
Returns the value of attribute helper_name.
-
.preloaders ⇒ Object
Returns the value of attribute preloaders.
-
.record_ids ⇒ Object
Returns the value of attribute record_ids.
-
.reserved_tables ⇒ Object
Returns the value of attribute reserved_tables.
Class Method Summary collapse
- .active_record ⇒ Object
- .active_record_names ⇒ Object
- .clean(*names) ⇒ Object
- .connection ⇒ Object
- .minitest ⇒ Object
- .reload_factories ⇒ Object
- .run ⇒ Object
- .try_truncation_query ⇒ Object
Class Attribute Details
.clean_with ⇒ Object
Returns the value of attribute clean_with.
12 13 14 |
# File 'lib/factory_bot/preload.rb', line 12 def clean_with @clean_with end |
.default_helper_name ⇒ Object
Returns the value of attribute default_helper_name.
13 14 15 |
# File 'lib/factory_bot/preload.rb', line 13 def default_helper_name @default_helper_name end |
.factories ⇒ Object
Returns the value of attribute factories.
10 11 12 |
# File 'lib/factory_bot/preload.rb', line 10 def factories @factories end |
.helper_name ⇒ Object
Returns the value of attribute helper_name.
14 15 16 |
# File 'lib/factory_bot/preload.rb', line 14 def helper_name @helper_name end |
.preloaders ⇒ Object
Returns the value of attribute preloaders.
9 10 11 |
# File 'lib/factory_bot/preload.rb', line 9 def preloaders @preloaders end |
.record_ids ⇒ Object
Returns the value of attribute record_ids.
11 12 13 |
# File 'lib/factory_bot/preload.rb', line 11 def record_ids @record_ids end |
.reserved_tables ⇒ Object
Returns the value of attribute reserved_tables.
15 16 17 |
# File 'lib/factory_bot/preload.rb', line 15 def reserved_tables @reserved_tables end |
Class Method Details
.active_record ⇒ Object
40 41 42 |
# File 'lib/factory_bot/preload.rb', line 40 def self.active_record ActiveRecord::Base end |
.active_record_names ⇒ Object
77 78 79 80 81 |
# File 'lib/factory_bot/preload.rb', line 77 def self.active_record_names names = active_record.descendants.collect(&:table_name).uniq.compact names.reject {|name| reserved_tables.include?(name) } end |
.clean(*names) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/factory_bot/preload.rb', line 58 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
44 45 46 |
# File 'lib/factory_bot/preload.rb', line 44 def self.connection active_record.connection end |
.minitest ⇒ Object
8 9 10 11 |
# File 'lib/factory_bot/preload/minitest.rb', line 8 def self.minitest FactoryBot::Preload.clean FactoryBot::Preload.run end |
.reload_factories ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/factory_bot/preload.rb', line 83 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
48 49 50 51 52 53 54 55 56 |
# File 'lib/factory_bot/preload.rb', line 48 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
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/factory_bot/preload.rb', line 91 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 |