thoughtbot / factory_bot_rails

Factory Bot ♥ Rails

Home Page:https://thoughtbot.com/services/ruby-on-rails

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Feature Request] Install Generator

okuramasafumi opened this issue · comments

We often adopt factory_bot_rails from the very early stage (even before generating the first model).
However, in some cases, we adopt factory_bot_rails in the middle of the development with some models. In those cases, we need to trigger generator for many times, which is not comfortable.
Can we have install generator used like:

# Given we have foo, bar and buzz models
bundle exec rails g factory_bot:install
  create spec/factories/foos.rb
  create spec/factories/bars.rb
  create spec/factories/buzzs.rb

That is an interesting idea! I think the command would be rails generate factory_bot:install, along the same lines as rails generate rspec:install.

To get the list of models maybe we could load every file in app/models and then call ActiveRecord::Base.descendants?

I am not sure this is something we would use much here at thoughtbot since we start all of our Rails projects with factory_bot already included (we generate new projects with https://github.com/thoughtbot/suspenders).

Thank you for your comment, I did a typo on the command so I fixed it.
I understand that in most cases we'd use factory_bot from the very beginning of project.

Can't we check each model's validations and create an auto-generated factory to be valid?
If we can do that, this generator can be useful for much more use cases.

Since there has been no movement in this, and since I am not sure this would be widely used enough to warrant adding a new feature to factory_bot_rails, I am going to close this for now.

It might be possible to run something like

rails runner 'Rails.application.eager_load!; ApplicationRecord.descendants.each { |descendant| `rails g model #{descendant.name} --skip` }'

if you need to generate lots of things at once. That probably isn't a perfect solution, but it might get you close enough.

Sorry for doing nothing, I left from the project which requires this feature and I didn't work on this since then.
And thank you for a useful tip :)