thoughtbot / factory_bot_rails

Factory Bot ♥ Rails
https://thoughtbot.com/services/ruby-on-rails
MIT License
3.06k stars 364 forks source link

Automatically populate attributes and types when generating factories for existing models #404

Open stevecondylios opened 2 years ago

stevecondylios commented 2 years ago

This shows how to generate a factory for an existing model, for example;

rails g factory_bot:model Car name speed:integer

It would be pretty awesome if factory_bot could accept rails g factory_bot:model Car and assume that when no attributes/types are provided that the factory should be generated with every attribute in the model. That would make it even easier to add factory_bot to existing applications.

FanaHOVA commented 2 years ago

Sounds cool, going to give this a shot!

FanaHOVA commented 1 year ago

Sorry about the delay here, it was my first week on a new job :) I've spent some time trying to make this work within factory_bot_rails but it's just hard for me to figure out how it hooks into the main gem and best practices for it. Given that this use case should be a one-of task, as the gem already does support attribute pre-filling for new models, I've created a separate gem for it here: https://github.com/FanaHOVA/factory_bot_rails_seeder

You can simply install it and run FactoryBotRailsSeeder.generate_for_all_classes to generate factories for all existing models. You can also generate factories for a subset of them, or for all but [X, Y].

The gem literally just generates the command string and runs it with system so it's not very sophisticated, but at least it's a start!