Skip to content

Commit

Permalink
Add filename_proc option to Factory generator
Browse files Browse the repository at this point in the history
  • Loading branch information
kreintjes authored and joshuaclayton committed Mar 16, 2015
1 parent 3ac64d7 commit 5b17b57
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
15 changes: 15 additions & 0 deletions features/fixture_replacement_config.feature
Expand Up @@ -115,3 +115,18 @@ Feature:
| spec/factories/users_suffix.rb |
Then the following files should not exist:
| 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 |
13 changes: 0 additions & 13 deletions features/step_definitions/rails_steps.rb
Expand Up @@ -6,19 +6,6 @@
append_to_file('Gemfile', %{gem "#{gem_name}"\n})
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|
in_current_dir do
File.open(path, 'r') do |f|
Expand Down
9 changes: 8 additions & 1 deletion lib/generators/factory_girl/model/model_generator.rb
Expand Up @@ -48,7 +48,6 @@ def insert_factory_into_existing_file
end

def create_factory_file
filename = [table_name, filename_suffix].compact.join('_')
file = File.join(options[:dir], "#{filename}.rb")
create_file(file, single_file_factory_definition)
end
Expand All @@ -75,6 +74,14 @@ def factory_attributes
end.join("\n")
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
factory_girl_options[:suffix] || options[:suffix]
end
Expand Down

0 comments on commit 5b17b57

Please sign in to comment.