Skip to content

Commit

Permalink
Tweaked factory_girls_initialization process to allow other engines t…
Browse files Browse the repository at this point in the history
…o advertise its factories for use in a recipient project

When a (mountable) engine defines models and factories to test the models, sometimes it is convenient to use this factories
from a project which depends on the engine.

But current factory_girl_rail initialization processes do not allow to do so without hacks.
(after_initializer resets all FactoryGirl.definition_file_paths to defaults with = [paths])

When this change introduces the engine can advertise some factories in an its own railtie, i.e.:

module ModelCore
  class Engine < Rails::Engine

    initializer "model_core.factories", :after => "factory_girl.set_factory_paths" do
      FactoryGirl.definition_file_paths << File.expand_path('../../../spec/factories', __FILE__) if defined?(FactoryGirl)
    end
  end
end
  • Loading branch information
cryo28 committed Dec 2, 2011
1 parent 1e47af0 commit 191ce5e
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/factory_girl_rails/railtie.rb
Expand Up @@ -14,12 +14,15 @@ class Railtie < Rails::Railtie
end
end

config.after_initialize do
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')
]
end

config.after_initialize do
FactoryGirl.find_definitions
end
end
Expand Down

0 comments on commit 191ce5e

Please sign in to comment.