1
0
Fork 0
mirror of https://github.com/thoughtbot/factory_bot_rails.git synced 2022-11-09 11:49:18 -05:00

Add filename_proc option to Factory generator

This commit is contained in:
Kevin Reintjes 2014-09-02 16:08:54 +02:00 committed by Joshua Clayton
parent 3ac64d789f
commit 5b17b5777b
3 changed files with 23 additions and 14 deletions

View file

@ -115,3 +115,18 @@ Feature:
| spec/factories/users_suffix.rb | | spec/factories/users_suffix.rb |
Then the following files should not exist: Then the following files should not exist:
| spec/factories/users.rb | | spec/factories/users.rb |
Scenario: Use a filename_proc with the Factory Girl generator
When I add "rspec-rails" as a dependency
When I configure the factories as:
"""
config.generators do |g|
g.factory_girl filename_proc: Proc.new { |tb| "prefix_#{tb.singularize}_suffix" }
end
"""
And I run `bundle install` with a clean environment
And I run `bundle exec rails generate model User name:string` with a clean environment
Then the following files should exist:
| spec/factories/prefix_user_suffix.rb |
Then the following files should not exist:
| spec/factories/users.rb |

View file

@ -6,19 +6,6 @@ When /^I add "([^"]+)" as a dependency$/ do |gem_name|
append_to_file('Gemfile', %{gem "#{gem_name}"\n}) append_to_file('Gemfile', %{gem "#{gem_name}"\n})
end end
When /^I set the FactoryGirl :suffix option to "([^"]+)"$/ do |suffix|
append_to_file('config/application.rb', <<-RUBY)
module Testapp
class Application < Rails::Application
config.generators do |g|
g.fixture_replacement :factory_girl, :suffix => '#{suffix}'
end
end
end
RUBY
end
When /^I print out "([^"]*)"$/ do |path| When /^I print out "([^"]*)"$/ do |path|
in_current_dir do in_current_dir do
File.open(path, 'r') do |f| File.open(path, 'r') do |f|

View file

@ -48,7 +48,6 @@ module FactoryGirl
end end
def create_factory_file def create_factory_file
filename = [table_name, filename_suffix].compact.join('_')
file = File.join(options[:dir], "#{filename}.rb") file = File.join(options[:dir], "#{filename}.rb")
create_file(file, single_file_factory_definition) create_file(file, single_file_factory_definition)
end end
@ -75,6 +74,14 @@ RUBY
end.join("\n") end.join("\n")
end end
def filename
if factory_girl_options[:filename_proc].present?
factory_girl_options[:filename_proc].call(table_name)
else
[table_name, filename_suffix].compact.join('_')
end
end
def filename_suffix def filename_suffix
factory_girl_options[:suffix] || options[:suffix] factory_girl_options[:suffix] || options[:suffix]
end end