mirror of
https://github.com/thoughtbot/factory_bot_rails.git
synced 2022-11-09 11:49:18 -05:00
Allow using custom template for the generator
PR #135 introduced the ability to generate factories in one file, but it also removed the ability to use custom templates for generating factories. This PR adds back the ability to use custom templates when generating factories in separate files. I doubt this was a heavily used feature, but it has come up a few times and it I think supporting it simplifies the generator. Closes #147 Closes #148 Closes #193 PR #193 basically reverted #135, which is undesirable. thoughtbot projects generally prefer putting all the factories in one file. Co-authored-by: Nick Sanford <me@nicksanford.io>
This commit is contained in:
parent
79ab64d8ad
commit
b48cff7d99
4 changed files with 28 additions and 16 deletions
|
@ -132,6 +132,15 @@ config.generators do |g|
|
|||
end
|
||||
```
|
||||
|
||||
To override the [default factory template][], define your own template in
|
||||
`lib/templates/factory\_bot/models/factories.erb`. This template will have
|
||||
access to any methods available in `FactoryBot::Generators::ModelGenerator`.
|
||||
Note that factory\_bot\_rails will only use this custom template if you are
|
||||
generating each factory in a separate file; it will have no effect if you are
|
||||
generating all of your factories in `test/factories.rb` or `spec/factories.rb`.
|
||||
|
||||
[default factory template]: https://github.com/thoughtbot/factory_bot_rails/tree/master/lib/generators/factory_bot/model/templates/factories.erb
|
||||
|
||||
## Contributing
|
||||
|
||||
Please see [CONTRIBUTING.md](CONTRIBUTING.md).
|
||||
|
|
|
@ -7,10 +7,10 @@ Feature:
|
|||
Given I successfully run `bundle exec rails new testapp`
|
||||
And I cd to "testapp"
|
||||
And I add "factory_bot_rails" from this project as a dependency
|
||||
And I run `bundle install` with a clean environment
|
||||
|
||||
Scenario: The factory_bot_rails generators create a factory file for each model if there is not a factories.rb file
|
||||
When I run `bundle install` with a clean environment
|
||||
And I run `bundle exec rails generate model User name:string age:integer` with a clean environment
|
||||
When I run `bundle exec rails generate model User name:string age:integer` with a clean environment
|
||||
And I run `bundle exec rails generate model Namespaced::User name:string` with a clean environment
|
||||
Then the output should contain "test/factories/users.rb"
|
||||
And the output should contain "test/factories/namespaced_users.rb"
|
||||
|
@ -22,13 +22,11 @@ Feature:
|
|||
age { 1 }
|
||||
end
|
||||
end
|
||||
|
||||
"""
|
||||
And the file "test/factories/namespaced_users.rb" should contain "factory :namespaced_user, class: 'Namespaced::User' do"
|
||||
|
||||
Scenario: The factory_bot_rails generators add a factory in the correct spot
|
||||
When I run `bundle install` with a clean environment
|
||||
And I write to "test/factories.rb" with:
|
||||
When I write to "test/factories.rb" with:
|
||||
"""
|
||||
FactoryBot.define do
|
||||
end
|
||||
|
@ -50,11 +48,21 @@ Feature:
|
|||
"""
|
||||
|
||||
Scenario: The factory_bot_rails generators does not create a factory file for each model if there is a factories.rb file in the test directory
|
||||
When I run `bundle install` with a clean environment
|
||||
And I write to "test/factories.rb" with:
|
||||
When I write to "test/factories.rb" with:
|
||||
"""
|
||||
FactoryBot.define do
|
||||
end
|
||||
"""
|
||||
And I run `bundle exec rails generate model User name:string` with a clean environment
|
||||
Then the file "test/factories/users.rb" should not contain "factory :user do"
|
||||
|
||||
Scenario: The factory_bot_rails generators use a custom template
|
||||
When I write to "lib/templates/factory_bot/model/factories.erb" with:
|
||||
"""
|
||||
<%= "Custom factory definition" %>
|
||||
"""
|
||||
And I run `bundle exec rails generate model User` with a clean environment
|
||||
Then the file "test/factories/users.rb" should contain exactly:
|
||||
"""
|
||||
Custom factory definition
|
||||
"""
|
||||
|
|
|
@ -49,7 +49,7 @@ module FactoryBot
|
|||
|
||||
def create_factory_file
|
||||
file = File.join(options[:dir], "#{filename}.rb")
|
||||
create_file(file, single_file_factory_definition)
|
||||
template "factories.erb", file
|
||||
end
|
||||
|
||||
def factory_definition
|
||||
|
@ -61,14 +61,6 @@ module FactoryBot
|
|||
RUBY
|
||||
end
|
||||
|
||||
def single_file_factory_definition
|
||||
<<~RUBY
|
||||
FactoryBot.define do
|
||||
#{factory_definition.rstrip}
|
||||
end
|
||||
RUBY
|
||||
end
|
||||
|
||||
def factory_attributes
|
||||
attributes.map do |attribute|
|
||||
"#{attribute.name} { #{attribute.default.inspect} }"
|
||||
|
|
3
lib/generators/factory_bot/model/templates/factories.erb
Normal file
3
lib/generators/factory_bot/model/templates/factories.erb
Normal file
|
@ -0,0 +1,3 @@
|
|||
FactoryBot.define do
|
||||
<%= factory_definition.rstrip %>
|
||||
end
|
Loading…
Reference in a new issue