Skip to content

Commit

Permalink
Add suffix option for Rails config
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Hu authored and joshuaclayton committed Dec 7, 2012
1 parent 6189e60 commit e85aa34
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 40 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------------------

Expand Down
14 changes: 14 additions & 0 deletions features/generators.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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"
13 changes: 13 additions & 0 deletions features/step_definitions/rails_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,16 @@
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
8 changes: 4 additions & 4 deletions gemfiles/rails30.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -109,7 +109,7 @@ GEM
treetop (1.4.12)
polyglot
polyglot (>= 0.3.1)
tzinfo (0.3.34)
tzinfo (0.3.35)

PLATFORMS
ruby
Expand Down
8 changes: 4 additions & 4 deletions gemfiles/rails31.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -121,7 +121,7 @@ GEM
treetop (1.4.12)
polyglot
polyglot (>= 0.3.1)
tzinfo (0.3.34)
tzinfo (0.3.35)

PLATFORMS
ruby
Expand Down
61 changes: 31 additions & 30 deletions gemfiles/rails32.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -119,7 +120,7 @@ GEM
treetop (1.4.12)
polyglot
polyglot (>= 0.3.1)
tzinfo (0.3.34)
tzinfo (0.3.35)

PLATFORMS
ruby
Expand Down
20 changes: 18 additions & 2 deletions lib/generators/factory_girl/model/model_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,25 @@ module Generators
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
Expand Down

0 comments on commit e85aa34

Please sign in to comment.