diff --git a/features/generators.feature b/features/generators.feature index 7f5bcae..4a1980b 100644 --- a/features/generators.feature +++ b/features/generators.feature @@ -24,6 +24,18 @@ Feature: """ And the file "test/factories/namespaced/users.rb" should contain "factory :namespaced_user, class: 'Namespaced::User' do" + Scenario: The factory_bot_rails generators create a factory file with correct naming when I use --force-plural + When I run `bundle exec rails generate model UserMedia filename:string --force-plural` with a clean environment + Then the output should contain "test/factories/user_media.rb" + And the file "test/factories/user_media.rb" should contain exactly: + """ + FactoryBot.define do + factory :user_media do + filename { "MyString" } + end + end + """ + Scenario: The factory_bot_rails generators add a factory in the correct spot When I write to "test/factories.rb" with: """ diff --git a/lib/generators/factory_bot.rb b/lib/generators/factory_bot.rb index 7a80f45..ef7551e 100644 --- a/lib/generators/factory_bot.rb +++ b/lib/generators/factory_bot.rb @@ -14,8 +14,12 @@ module FactoryBot File.expand_path(path) end + def factory_name + class_name.gsub("::", "").underscore + end + def explicit_class_option - return if class_name == singular_table_name.camelize + return if class_name.underscore == factory_name ", class: '#{class_name}'" end diff --git a/lib/generators/factory_bot/model/model_generator.rb b/lib/generators/factory_bot/model/model_generator.rb index f6e3992..4d28fb6 100644 --- a/lib/generators/factory_bot/model/model_generator.rb +++ b/lib/generators/factory_bot/model/model_generator.rb @@ -54,7 +54,7 @@ module FactoryBot def factory_definition <<~RUBY - factory :#{singular_table_name}#{explicit_class_option} do + factory :#{factory_name}#{explicit_class_option} do #{factory_attributes.gsub(/^/, ' ')} end