Skip to content

Commit

Permalink
Chore: Automate Setting New UUID's When Running the Seeds Task
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
KevinMulhern committed Feb 23, 2023
1 parent 7d269d8 commit 631bca3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
load './db/fixtures/lessons/node_js_lessons.rb'
load './db/fixtures/lessons/git_lessons.rb'

Rails::Generators.invoke('seed_uuids') if Rails.env.development?
SeedFu.seed

# GENERATE SUCCESS STORY Content
Expand Down
5 changes: 5 additions & 0 deletions lib/generators/rails/seed_uuids/USAGE
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Description:
bin/rails generate seed_uuids

This will replace "create_uuid" in the seeds file with a real uuid

9 changes: 9 additions & 0 deletions lib/generators/rails/seed_uuids/seed_uuids_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Rails::SeedUuidsGenerator < Rails::Generators::Base
def generate_uuids
seed_files = Dir.glob('db/fixtures/**/*').reject { |f| File.directory?(f) }

seed_files.each do |file_name|
gsub_file(file_name, 'create_uuid', SecureRandom.uuid)
end
end
end

0 comments on commit 631bca3

Please sign in to comment.