Module: AutoSeeder

Defined in:
lib/auto_seeder.rb,
lib/auto_seeder/cli.rb,
lib/auto_seeder/version.rb

Defined Under Namespace

Classes: CLI

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.generate_attributes_for_model(model) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/auto_seeder.rb', line 23

def self.generate_attributes_for_model(model)
  attributes = {}

  # Loop through each column of the model
  model.columns.each do |column|
    next if %w[id created_at updated_at].include?(column.name)

    # Generate data based on column type
    attributes[column.name] = case column.type
                              when :string
                                Faker::Lorem.sentence
                              when :text
                                Faker::Lorem.paragraph
                              when :integer
                                handle_integer_column(model, column)
                              when :datetime
                                Faker::Time.between(from: 1.year.ago, to: Time.current)
                              when :boolean
                                [true, false].sample
                              else
                                nil
                              end
  end

  # Handle model associations
  handle_associations(model, attributes)

  attributes
end

.handle_associations(model, attributes) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/auto_seeder.rb', line 62

def self.handle_associations(model, attributes)
  model.reflect_on_all_associations.each do |association|
    case association.macro
    when :belongs_to
      associated_record = association.klass.order("RANDOM()").first || association.klass.create!(name: Faker::Name.name)
      attributes[association.foreign_key] = associated_record.id
    when :has_one
      attributes[association.name] = association.klass.create!(name: Faker::Name.name)
    when :has_many
      2.times { association.klass.create!(name: Faker::Name.name, model_name.foreign_key => model.id) }
    end
  end
end

.handle_integer_column(model, column) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/auto_seeder.rb', line 53

def self.handle_integer_column(model, column)
  if column.name.end_with?("_id") # Foreign key
    associated_model = column.name.gsub("_id", "").classify.constantize
    associated_model.pluck(:id).sample || associated_model.create!(name: Faker::Name.name).id
  else
    rand(1..100)
  end
end

.seed(model_name, count) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/auto_seeder.rb', line 8

def self.seed(model_name, count)
  model = model_name.classify.constantize
  count.times do
    attributes = generate_attributes_for_model(model)

    # Create the record if valid
    record = model.new(attributes)
    if record.save
      puts "Created #{model_name} with ID: #{record.id}"
    else
      puts "Failed to create #{model_name}: #{record.errors.full_messages.join(', ')}"
    end
  end
end