From e85aa34ace30169118f2383f1e45cf69ea5ece3c Mon Sep 17 00:00:00 2001 From: Eric Hu Date: Thu, 29 Nov 2012 02:46:29 -0800 Subject: [PATCH] Add suffix option for Rails config Closes #78 --- README.md | 10 +++ features/generators.feature | 14 +++++ features/step_definitions/rails_steps.rb | 13 ++++ gemfiles/rails30.gemfile.lock | 8 +-- gemfiles/rails31.gemfile.lock | 8 +-- gemfiles/rails32.gemfile.lock | 61 ++++++++++--------- .../factory_girl/model/model_generator.rb | 20 +++++- 7 files changed, 94 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index e1d86e0..1265cce 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,16 @@ Add factory_girl_rails to your Gemfile: gem 'factory_girl_rails' +Optionally, to have rails generators automatically generate factories instead +of fixtures, add the following to your application.rb file: + + config.generators do |g| + g.fixture_replacement :factory_girl + end + +`fixture_replacement :factory_girl` takes an option `:suffix => 'some_suffix'` +to generate factories as "modelname_some_suffix.rb" + Cucumber Integration -------------------- diff --git a/features/generators.feature b/features/generators.feature index fbfd34c..569833c 100644 --- a/features/generators.feature +++ b/features/generators.feature @@ -15,3 +15,17 @@ Feature: And the output should contain "test/factories/namespaced_users.rb" And the file "test/factories/users.rb" should contain "factory :user do" And the file "test/factories/namespaced_users.rb" should contain "factory :namespaced_user, :class => 'Namespaced::User' do" + + Scenario: The factory_girl_rails generators create a factory file with a + custom name for each model that I generate + When I successfully run `bundle exec rails new testapp` + And I cd to "testapp" + And I add "factory_girl_rails" from this project as a dependency + When I successfully run `bundle install` + And I set the FactoryGirl :suffix option to "factory" + And I successfully run `bundle exec rails generate model User name:string --fixture-replacement=factory_girl` + And I successfully run `bundle exec rails generate model Namespaced::User name:string --fixture-replacement=factory_girl` + Then the output should contain "test/factories/users_factory.rb" + And the output should contain "test/factories/namespaced_users_factory.rb" + And the file "test/factories/users_factory.rb" should contain "factory :user do" + And the file "test/factories/namespaced_users_factory.rb" should contain "factory :namespaced_user, :class => 'Namespaced::User' do" diff --git a/features/step_definitions/rails_steps.rb b/features/step_definitions/rails_steps.rb index 798b29d..16ee0b8 100644 --- a/features/step_definitions/rails_steps.rb +++ b/features/step_definitions/rails_steps.rb @@ -5,3 +5,16 @@ end 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 diff --git a/gemfiles/rails30.gemfile.lock b/gemfiles/rails30.gemfile.lock index 39187f0..23b9b1d 100644 --- a/gemfiles/rails30.gemfile.lock +++ b/gemfiles/rails30.gemfile.lock @@ -35,7 +35,7 @@ GEM activemodel (= 3.0.17) activesupport (= 3.0.17) activesupport (3.0.17) - appraisal (0.5.0) + appraisal (0.5.1) bundler rake arel (2.0.10) @@ -61,7 +61,7 @@ GEM abstract (>= 1.0.0) factory_girl (4.1.0) activesupport (>= 3.0.0) - ffi (1.1.5) + ffi (1.2.0) gherkin (2.4.21) json (>= 1.4.6) i18n (0.5.0) @@ -92,7 +92,7 @@ GEM rake (>= 0.8.7) rdoc (~> 3.4) thor (~> 0.14.4) - rake (0.9.2.2) + rake (10.0.2) rdiscount (1.6.8) rdoc (3.12) json (~> 1.4) @@ -109,7 +109,7 @@ GEM treetop (1.4.12) polyglot polyglot (>= 0.3.1) - tzinfo (0.3.34) + tzinfo (0.3.35) PLATFORMS ruby diff --git a/gemfiles/rails31.gemfile.lock b/gemfiles/rails31.gemfile.lock index 937ed3e..0d3af27 100644 --- a/gemfiles/rails31.gemfile.lock +++ b/gemfiles/rails31.gemfile.lock @@ -36,7 +36,7 @@ GEM activesupport (= 3.1.8) activesupport (3.1.8) multi_json (>= 1.0, < 1.3) - appraisal (0.5.0) + appraisal (0.5.1) bundler rake arel (2.2.3) @@ -61,7 +61,7 @@ GEM erubis (2.7.0) factory_girl (4.1.0) activesupport (>= 3.0.0) - ffi (1.1.5) + ffi (1.2.0) gherkin (2.4.21) json (>= 1.4.6) hike (1.2.1) @@ -98,7 +98,7 @@ GEM rake (>= 0.8.7) rdoc (~> 3.4) thor (~> 0.14.6) - rake (0.9.2.2) + rake (10.0.2) rdiscount (1.6.8) rdoc (3.12) json (~> 1.4) @@ -121,7 +121,7 @@ GEM treetop (1.4.12) polyglot polyglot (>= 0.3.1) - tzinfo (0.3.34) + tzinfo (0.3.35) PLATFORMS ruby diff --git a/gemfiles/rails32.gemfile.lock b/gemfiles/rails32.gemfile.lock index 8ba9aa3..b7a7185 100644 --- a/gemfiles/rails32.gemfile.lock +++ b/gemfiles/rails32.gemfile.lock @@ -8,34 +8,34 @@ PATH GEM remote: http://rubygems.org/ specs: - actionmailer (3.2.8) - actionpack (= 3.2.8) + actionmailer (3.2.9) + actionpack (= 3.2.9) mail (~> 2.4.4) - actionpack (3.2.8) - activemodel (= 3.2.8) - activesupport (= 3.2.8) + actionpack (3.2.9) + activemodel (= 3.2.9) + activesupport (= 3.2.9) builder (~> 3.0.0) erubis (~> 2.7.0) journey (~> 1.0.4) rack (~> 1.4.0) rack-cache (~> 1.2) rack-test (~> 0.6.1) - sprockets (~> 2.1.3) - activemodel (3.2.8) - activesupport (= 3.2.8) + sprockets (~> 2.2.1) + activemodel (3.2.9) + activesupport (= 3.2.9) builder (~> 3.0.0) - activerecord (3.2.8) - activemodel (= 3.2.8) - activesupport (= 3.2.8) + activerecord (3.2.9) + activemodel (= 3.2.9) + activesupport (= 3.2.9) arel (~> 3.0.2) tzinfo (~> 0.3.29) - activeresource (3.2.8) - activemodel (= 3.2.8) - activesupport (= 3.2.8) - activesupport (3.2.8) + activeresource (3.2.9) + activemodel (= 3.2.9) + activesupport (= 3.2.9) + activesupport (3.2.9) i18n (~> 0.6) multi_json (~> 1.0) - appraisal (0.5.0) + appraisal (0.5.1) bundler rake arel (3.0.2) @@ -60,7 +60,7 @@ GEM erubis (2.7.0) factory_girl (4.1.0) activesupport (>= 3.0.0) - ffi (1.1.5) + ffi (1.2.0) gherkin (2.4.21) json (>= 1.4.6) hike (1.2.1) @@ -81,22 +81,22 @@ GEM rack rack-test (0.6.2) rack (>= 1.0) - rails (3.2.8) - actionmailer (= 3.2.8) - actionpack (= 3.2.8) - activerecord (= 3.2.8) - activeresource (= 3.2.8) - activesupport (= 3.2.8) + rails (3.2.9) + actionmailer (= 3.2.9) + actionpack (= 3.2.9) + activerecord (= 3.2.9) + activeresource (= 3.2.9) + activesupport (= 3.2.9) bundler (~> 1.0) - railties (= 3.2.8) - railties (3.2.8) - actionpack (= 3.2.8) - activesupport (= 3.2.8) + railties (= 3.2.9) + railties (3.2.9) + actionpack (= 3.2.9) + activesupport (= 3.2.9) rack-ssl (~> 1.3.2) rake (>= 0.8.7) rdoc (~> 3.4) thor (>= 0.14.6, < 2.0) - rake (0.9.2.2) + rake (10.0.2) rdiscount (1.6.8) rdoc (3.12) json (~> 1.4) @@ -108,8 +108,9 @@ GEM rspec-expectations (2.11.3) diff-lcs (~> 1.1.3) rspec-mocks (2.11.3) - sprockets (2.1.3) + sprockets (2.2.2) hike (~> 1.2) + multi_json (~> 1.0) rack (~> 1.0) tilt (~> 1.1, != 1.3.0) sqlite3 (1.3.6) @@ -119,7 +120,7 @@ GEM treetop (1.4.12) polyglot polyglot (>= 0.3.1) - tzinfo (0.3.34) + tzinfo (0.3.35) PLATFORMS ruby diff --git a/lib/generators/factory_girl/model/model_generator.rb b/lib/generators/factory_girl/model/model_generator.rb index 851d1ee..899bed3 100644 --- a/lib/generators/factory_girl/model/model_generator.rb +++ b/lib/generators/factory_girl/model/model_generator.rb @@ -5,9 +5,25 @@ module FactoryGirl class ModelGenerator < Base argument :attributes, :type => :array, :default => [], :banner => "field:type field:type" class_option :dir, :type => :string, :default => "test/factories", :desc => "The directory where the factories should go" - + def create_fixture_file - template 'fixtures.erb', File.join(options[:dir], "#{table_name}.rb") + filename = [table_name, filename_suffix].compact.join('_') + template 'fixtures.erb', File.join(options[:dir], "#{filename}.rb") + end + + private + + def filename_suffix + factory_girl_options[:suffix] + end + + def generators + config = FactoryGirl::Railtie.config + config.respond_to?(:app_generators) ? config.app_generators : config.generators + end + + def factory_girl_options + generators.options[:factory_girl] || {} end end end