Fix for factory class naming (#366)

Changing the process of name selection for factory defining

Example:

rails g model UserMedia filename:string --force-plural
...
invoke      factory_bot
create        spec/factories/user_media.rb

it generates the correct factory naming:

FactoryBot.define do
  factory :user_media do
    filename { "MyString" }
  end
end

Fixes: #356
This commit is contained in:
Alex Golubenko 2020-04-04 04:50:47 +03:00 committed by GitHub
parent 5c52981fcc
commit 7780065bd2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 2 deletions

View File

@ -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:
"""

View File

@ -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

View File

@ -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