Class: FixturesFromFactories::FixtureGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/fixtures_from_factories/fixture_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output_path, excluded_tables = FixturesFromFactories.configuration.excluded_tables) ⇒ FixtureGenerator

Returns a new instance of FixtureGenerator.



10
11
12
13
14
15
16
17
18
# File 'lib/fixtures_from_factories/fixture_generator.rb', line 10

def initialize(
  output_path,
  excluded_tables = FixturesFromFactories.configuration.excluded_tables
)
  @model_cache = {}
  @name_config = {}
  @output_path = output_path
  @excluded_tables = excluded_tables
end

Instance Attribute Details

#excluded_tablesObject (readonly)

Returns the value of attribute excluded_tables.



8
9
10
# File 'lib/fixtures_from_factories/fixture_generator.rb', line 8

def excluded_tables
  @excluded_tables
end

#model_cacheObject (readonly)

Returns the value of attribute model_cache.



8
9
10
# File 'lib/fixtures_from_factories/fixture_generator.rb', line 8

def model_cache
  @model_cache
end

#name_configObject (readonly)

Returns the value of attribute name_config.



8
9
10
# File 'lib/fixtures_from_factories/fixture_generator.rb', line 8

def name_config
  @name_config
end

#output_pathObject (readonly)

Returns the value of attribute output_path.



8
9
10
# File 'lib/fixtures_from_factories/fixture_generator.rb', line 8

def output_path
  @output_path
end

Instance Method Details

#add_collection(collection, collection_name: nil) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/fixtures_from_factories/fixture_generator.rb', line 89

def add_collection(collection, collection_name: nil)
  c_name = collection_name || collection.name.underscore
  added_count = 0
  collection.find_in_batches do |group|
    group.each do |model|
      id = block_given? ? yield(model) : model.id.to_s
      name = "#{c_name}_#{id}".to_sym
      add_record(name, model)
      added_count += 1
    end
  end
  added_count
end

#add_record(item_name, record) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/fixtures_from_factories/fixture_generator.rb', line 81

def add_record(item_name, record)
  raise "Adding nil record! #{item_name}" if record.nil?
  Rails.logger.info "Adding record #{item_name}..."
  model_cache.merge!({item_name => cached_record(record)}) do |key|
    raise "Duplicate key: #{key}"
  end
end

#configure_name(model_klass, *attrs) ⇒ Object

Setup naming logic for a specific table



74
75
76
77
78
79
# File 'lib/fixtures_from_factories/fixture_generator.rb', line 74

def configure_name(model_klass, *attrs)
  name_config[model_klass.name] = {klass: model_klass, attrs: attrs}
  name_config[model_klass.name] = ->(record, other) do
    attrs.all? { |attr| record.attributes[attr.to_s] == other[attr.to_s] }
  end
end

#create(name_or_prefix, factory_or_index, *bot_args) ⇒ Object

Create a single named record using FactoryBot



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fixtures_from_factories/fixture_generator.rb', line 29

def create(name_or_prefix, factory_or_index, *bot_args)
  item_name, factory =
    if factory_or_index.is_a?(Symbol) || factory_or_index.is_a?(Array)
      [name_or_prefix, factory_or_index]
    else
      ["#{name_or_prefix}_#{factory_or_index}".to_sym, bot_args.shift]
    end

  Rails.logger.info "Creating record: #{item_name}"
  record = FactoryBot.create(*(Array.wrap(factory) + bot_args))
  if record.nil? || !record.persisted?
    raise StandardError, "A record failed to save, #{record.class.name}, #{bot_args}"
  end
  add_record(item_name, record)
  record
end

#create_multiple(name, factory, start_index, end_index, items = nil) ⇒ Object

Create multiple records from factory within index range, where the name has the index appended



47
48
49
50
51
52
# File 'lib/fixtures_from_factories/fixture_generator.rb', line 47

def create_multiple(name, factory, start_index, end_index, items = nil)
  (start_index..end_index).map.with_index do |instance_num, index|
    bot_args = block_given? ? yield(instance_num) : items&.at(index)
    create(name, instance_num, factory, bot_args)
  end
end

#create_multiple_with_names(name_prefix, factory, names, items = nil) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/fixtures_from_factories/fixture_generator.rb', line 54

def create_multiple_with_names(name_prefix, factory, names, items = nil)
  names.map.with_index do |config, index|
    parts = config.is_a?(Hash) ? config[:names] : config
    n_index = Array.wrap(parts).map { |s| s.to_s.parameterize.underscore }.join("_").to_sym
    n = "#{name_prefix}_#{n_index}".to_sym
    bot_args = block_given? ? yield(n, n_index, config, index) : items&.at(index)
    create(n, factory, bot_args)
  end
end

#generate(&blk) ⇒ Object



20
21
22
23
24
# File 'lib/fixtures_from_factories/fixture_generator.rb', line 20

def generate(&blk)
  instance_eval(&blk)
  dump_tables
  dump_record_index
end

#generate_name_from_humanized(model, *parts) ⇒ Object



69
70
71
# File 'lib/fixtures_from_factories/fixture_generator.rb', line 69

def generate_name_from_humanized(model, *parts)
  "#{model}_#{parts.map { |s| s.to_s.parameterize.underscore }.join("_")}".to_sym
end

#get(name, index = nil) ⇒ Object

Get an existing record



65
66
67
# File 'lib/fixtures_from_factories/fixture_generator.rb', line 65

def get(name, index = nil)
  model_cache.fetch(index.present? ? "#{name}_#{index}".to_sym : name)[:record]
end

#make_fake_time(from, to) ⇒ Object



103
104
105
# File 'lib/fixtures_from_factories/fixture_generator.rb', line 103

def make_fake_time(from, to)
  Faker::Time.between(from: from.utc, to: to.utc).utc
end