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

Add suffix option for Rails config

Closes #78
This commit is contained in:
Eric Hu 2012-11-29 02:46:29 -08:00 committed by Joshua Clayton
parent 6189e6028c
commit e85aa34ace
7 changed files with 94 additions and 40 deletions

View file

@ -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
--------------------

View file

@ -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"

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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