Module: FixtureBot::FixtureCreator

Extended by:
FactoryBot::Syntax::Methods, Helpers
Defined in:
lib/fixture_bot/fixture_creator.rb

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Helpers

included

Class Attribute Details

.fixturesObject

Returns the value of attribute fixtures.



9
10
11
# File 'lib/fixture_bot/fixture_creator.rb', line 9

def fixtures
  @fixtures
end

.fixtures_with_idObject

Returns the value of attribute fixtures_with_id.



9
10
11
# File 'lib/fixture_bot/fixture_creator.rb', line 9

def fixtures_with_id
  @fixtures_with_id
end

.record_idsObject

Returns the value of attribute record_ids.



9
10
11
# File 'lib/fixture_bot/fixture_creator.rb', line 9

def record_ids
  @record_ids
end

.tablesObject

Returns the value of attribute tables.



9
10
11
# File 'lib/fixture_bot/fixture_creator.rb', line 9

def tables
  @tables
end

Class Method Details

.load_to_dbObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fixture_bot/fixture_creator.rb', line 17

def load_to_db
  tmp_fixtures = []
  tmp_fixtures_with_id = []

  tables.each do |table, blocks|
    table_preload = TableLoader.new(table)

    blocks.each do |block|
      table_preload.instance_eval(&block)
    end

    tmp_fixtures.concat(table_preload.fixtures)
    tmp_fixtures_with_id.concat(table_preload.fixtures_with_id)
  end

  self.fixtures = tmp_fixtures.to_h
  self.fixtures_with_id = tmp_fixtures_with_id.to_h

  ::ActiveRecord::Base.connection.transaction requires_new: true do
    fixtures_with_id.each do |key, block|
      table, fixture_name = key.split("/")
      fixture_with_id(table.to_sym, fixture_name.to_sym, &block)
    end

    fixtures.each do |key, block|
      table, fixture_name = key.split("/")
      fixture(table.to_sym, fixture_name.to_sym, &block)
    end
  end
end