From 5b17b5777b72028b2338c347e333a804d9adca9b Mon Sep 17 00:00:00 2001 From: Kevin Reintjes Date: Tue, 2 Sep 2014 16:08:54 +0200 Subject: [PATCH] Add filename_proc option to Factory generator --- features/fixture_replacement_config.feature | 15 +++++++++++++++ features/step_definitions/rails_steps.rb | 13 ------------- .../factory_girl/model/model_generator.rb | 9 ++++++++- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/features/fixture_replacement_config.feature b/features/fixture_replacement_config.feature index 49ad6b7..23d21e6 100644 --- a/features/fixture_replacement_config.feature +++ b/features/fixture_replacement_config.feature @@ -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 | diff --git a/features/step_definitions/rails_steps.rb b/features/step_definitions/rails_steps.rb index 0757b3e..299f785 100644 --- a/features/step_definitions/rails_steps.rb +++ b/features/step_definitions/rails_steps.rb @@ -6,19 +6,6 @@ When /^I add "([^"]+)" as a dependency$/ do |gem_name| 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| diff --git a/lib/generators/factory_girl/model/model_generator.rb b/lib/generators/factory_girl/model/model_generator.rb index ced29e8..21a25b3 100644 --- a/lib/generators/factory_girl/model/model_generator.rb +++ b/lib/generators/factory_girl/model/model_generator.rb @@ -48,7 +48,6 @@ module FactoryGirl 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 @@ -75,6 +74,14 @@ RUBY 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