Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
stevecondylios opened this issue Jul 13, 2022 · 2 comments
Labels

Comments

@stevecondylios
Copy link

stevecondylios commented Jul 13, 2022

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
Copy link

FanaHOVA commented Sep 9, 2022

Sounds cool, going to give this a shot!

@FanaHOVA
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants