Class: AutoSeed::Seeder

Inherits:
Object
  • Object
show all
Defined in:
lib/auto_seed/seeder.rb

Constant Summary collapse

DEVISE_COLUMNS =
%w[
  last_sign_in_at current_sign_in_at last_sign_in_ip current_sign_in_ip
  encrypted_password reset_password_token reset_password_sent_at
  remember_created_at sign_in_count
].freeze

Class Method Summary collapse

Class Method Details

.generate_seed_fileObject



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