9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/auto_seed/seeder.rb', line 9
def self.generate_seed_file
File.open('db/seeds.rb', 'w') do |file|
ActiveRecord::Base.descendants.each do |model|
next if skip_model?(model)
file.puts "## #{model.name} seeds"
begin
attributes = fetch_attributes(model)
attribute_values = generate_attributes(attributes)
file.puts "#{model.name}.create!("
attribute_values.each { |attr, value| file.puts " #{attr}: #{value.inspect}," }
associations = handle_associations(model)
associations.each { |assoc| file.puts " #{assoc[:name]}: #{assoc[:value]}," }
file.puts ");"
rescue StandardError => e
file.puts "# Error generating seed for #{model.name}: #{e.message}"
end
file.puts "\n"
end
end
end
|