Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Use a previously-selected :dir from application.rb #73

Closed
wants to merge 1 commit into from

3 participants

Ernie Miller Simon Coffey Joshua Clayton
Ernie Miller

factory_girl_rails currently ignores a previously-supplied :dir option from application.rb, and therefore always generates factories at spec/factories when RSpec is being used.

This patch will respect the user-supplied option, if given.

Simon Coffey

Great minds think alike - I opened pull request #64 a few months ago to address the same issue. :-)

Ernie Miller

@urbanautomaton Yeah, sorry -- I noticed yours after I submitted mine :(

Joshua Clayton joshuaclayton commented on the diff
lib/factory_girl_rails/railtie.rb
@@ -4,11 +4,12 @@
module FactoryGirl
class Railtie < Rails::Railtie
- initializer "factory_girl.set_fixture_replacement" do
- generators = config.respond_to?(:app_generators) ? config.app_generators : config.generators
+ generators = config.respond_to?(:app_generators) ? config.app_generators : config.generators
Joshua Clayton Admin

Was this supposed to be moved outside of the initializer block? Does it have any implications surrounding loading etc.?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 19, 2012
  1. Ernie Miller
This page is out of date. Refresh to see the latest.
10 features/fixture_replacement_config.feature
View
@@ -29,3 +29,13 @@ Feature:
| spec/factories/users.rb |
And the following files should not exist:
| spec/fixtures/users.yml |
+
+ Scenario: Using Factory Girl and Factory Girl Rails with RSpec should generate a factory file in a preferred location
+ And I add "rspec-rails" as a dependency
+ And I set the FactoryGirl :dir option to "spec/support/factories"
+ And I successfully run `bundle install`
+ And I successfully run `bundle exec rails generate model User name:string`
+ Then the following files should exist:
+ | spec/support/factories/users.rb |
+ And the following files should not exist:
+ | spec/fixtures/users.yml |
12 features/step_definitions/rails_steps.rb
View
@@ -5,3 +5,15 @@
When /^I add "([^"]+)" as a dependency$/ do |gem_name|
append_to_file('Gemfile', %{gem "#{gem_name}"\n})
end
+
+When /^I set the FactoryGirl :dir option to "([^"]+)"$/ do |directory|
+ append_to_file('config/application.rb', <<-RUBY)
+ module Testapp
+ class Application < Rails::Application
+ config.generators do |g|
+ g.fixture_replacement :factory_girl, :dir => '#{directory}'
+ end
+ end
+ end
+ RUBY
+end
18 lib/factory_girl_rails/railtie.rb
View
@@ -4,11 +4,12 @@
module FactoryGirl
class Railtie < Rails::Railtie
- initializer "factory_girl.set_fixture_replacement" do
- generators = config.respond_to?(:app_generators) ? config.app_generators : config.generators
+ generators = config.respond_to?(:app_generators) ? config.app_generators : config.generators
Joshua Clayton Admin

Was this supposed to be moved outside of the initializer block? Does it have any implications surrounding loading etc.?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ initializer "factory_girl.set_fixture_replacement" do
if generators.options[:rails][:test_framework] == :rspec
- generators.fixture_replacement :factory_girl, :dir => 'spec/factories'
+ opts = {:dir => 'spec/factories'}.merge(generators.options[:factory_girl])
+ generators.fixture_replacement :factory_girl, opts
else
generators.test_framework :test_unit, :fixture => false, :fixture_replacement => :factory_girl
end
@@ -17,9 +18,15 @@ class Railtie < Rails::Railtie
initializer "factory_girl.set_factory_paths" do
FactoryGirl.definition_file_paths = [
File.join(Rails.root, 'factories'),
- File.join(Rails.root, 'test', 'factories'),
- File.join(Rails.root, 'spec', 'factories')
+ File.join(Rails.root, 'test', 'factories')
]
+ # Don't trigger creation of an empty hash
+ if generators.options[:factory_girl].has_key?(:dir)
+ directory = generators.options[:factory_girl][:dir]
+ FactoryGirl.definition_file_paths << File.join(
+ Rails.root, *directory.split(File::SEPARATOR)
+ )
+ end
end
config.after_initialize do
@@ -27,4 +34,3 @@ class Railtie < Rails::Railtie
end
end
end
-
Something went wrong with that request. Please try again.