Skip to content

Commit 631bca3

Browse files
committed
Chore: Automate Setting New UUID's When Running the Seeds Task
Because: * It is frustrating and time consuming having to manually generate uuids and then copy and paste them into the seed files. * To get a random uuid automatically created for you, you just need to give any identifier_uuid attribute the value of "create_uuid" and then run the seeds as normal. This commit. * Adds a seed_uuids generator which will look for any "create_uuid" strings within the fixtures files and replace any instances with a new random uuid. * New generator can be invoked form the command line manually with `bin/rails g seed_uuids`. * Generator is invoked during the seed task everything is done with one command instead of needing to manually run the uuid generator first and then and then the seeds task.
1 parent 7d269d8 commit 631bca3

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

db/seeds.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
load './db/fixtures/lessons/node_js_lessons.rb'
1111
load './db/fixtures/lessons/git_lessons.rb'
1212

13+
Rails::Generators.invoke('seed_uuids') if Rails.env.development?
1314
SeedFu.seed
1415

1516
# GENERATE SUCCESS STORY Content

lib/generators/rails/seed_uuids/USAGE

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Description:
2+
bin/rails generate seed_uuids
3+
4+
This will replace "create_uuid" in the seeds file with a real uuid
5+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Rails::SeedUuidsGenerator < Rails::Generators::Base
2+
def generate_uuids
3+
seed_files = Dir.glob('db/fixtures/**/*').reject { |f| File.directory?(f) }
4+
5+
seed_files.each do |file_name|
6+
gsub_file(file_name, 'create_uuid', SecureRandom.uuid)
7+
end
8+
end
9+
end

0 commit comments

Comments
 (0)