My scaffold generator stopped working after we updated factory girl. Here's why happened. First, my config file tries to set up certain defaults for scaffold generation, like so:
class Application < Rails::Application
config.app_generators do |g|
g.template_engine 'mizugumo:haml'
g.scaffold_controller 'mizugumo:scaffold_controller'
g.assets 'mizugumo:js_assets'
g.test_framework :lrdspec, :fixture => true
g.fixture_replacement 'lrdspec:factory'
g.fallbacks['mizugumo:haml'] = :haml
g.fallbacks[:lrdspec] = :rspec
end
...
end
Where :lrdspec is the name of my company's scaffold spec generator. However, the most recent factory_girl_rails, in its initializer, rudely forces config.generators.test_framework to 'test_unit' unless your test framework is exactly ":rspec":
module FactoryGirl
class Railtie < Rails::Railtie
initializer "factory_girl.set_fixture_replacement" do
generators = config.respond_to?(:app_generators) ? config.app_generators : config.generators
if generators.options[:rails][:test_framework] == :rspec
generators.fixture_replacement开发者_Go百科 :factory_girl, :dir => 'spec/factories'
else
generators.test_framework :test_unit, :fixture => false, :fixture_replacement => :factory_girl
end
end
What I am trying to figure out how to do, is to generate my own initializer that runs after FG's initializer, to set test_framework back to :lrdspec, as I want it.
I've tried dropping my own railtie into config/initializers, or adding a block to config.after_initialize in config/application.rb, and a number of other approaches, but haven't quite found the solution. (My knowledge of Rails' initialization sequence needs to be a bit deeper than it is' I think).
Thanks!
Okay - found a solution. Sometimes just posting a question can help in thinking it through.
The answer was to set my own initializer in the gem that holds my scaffold generator, and to pass :after => "factory_girl.set_fixture_replacement" to initialize() when I create that block. The fact that you can specify :after to the initializer isn't documented in the Rails docco, but can be deduced by discovering that Initializable uses TSort to sort its collection of initializers, researching how TSort works, and discovering that the stored :after/:before parameters are used in the methods that TSort calls back to.
So the fix was to drop this in the configuration Railtie for my own gem, the one that provides the scaffold generator:
initializer "lrd_dev_tools.set_generators", :after => 'factory_girl.set_fixture_replacement' do
generators = config.respond_to?(:app_generators) ? config.app_generators : config.generators
generators.test_framework :lrdspec, :fixture => true
generators.fixture_replacement 'lrdspec:factory'
end
精彩评论