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

Model generation ignores --force-plural

armstnp opened this issue · comments

When generating a model using --force-plural, the factory_bot adapter still emits the singular form as the factory name.

Example:

> rails g model FileMedia filename:string --force-plural
...
invoke      factory_bot
create        spec/factories/file_media.rb

This is working as expected. The contents of the factory, however, are:

FactoryBot.define do
  factory :file_medium, class: 'FileMedia' do
    filename { "MyString" }
  end
end

Notice that the auto-inflected singular form :file_medium is used instead of the requested plural, :file_media.

The problematic line in question gets generated at

factory :#{singular_table_name}#{explicit_class_option} do
if anybody wants to look into making a PR.

We are using the singular_table_name method: https://github.com/rails/rails/blob/05a6cd5565a808e1063bc6c7cbdfe5ae6a46b72c/railties/lib/rails/generators/named_base.rb#L113-L115 but maybe there is something else we could be using.

Walking up the inheritance chain, I think the flag may be visible via pluralize_table_names? on the model generator object.

If this is fixed, the factory we define would look like

FactoryBot.define do
  factory :file_media, class: 'FileMedia' do
    filename { "MyString" }
  end
end