Rails 3.1:如何添加在 gem 初始化程序之后运行的初始化程序?

发布于 2024-12-09 22:04:58 字数 1424 浏览 1 评论 0原文

更新工厂女孩后,我的脚手架生成器停止工作。这就是发生的原因。首先,我的配置文件尝试为脚手架生成设置某些默认值,如下所示:

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

其中 :lrdspec 是我公司脚手架规范生成器的名称。然而,最新的factory_girl_rails在其初始化程序中,粗鲁地强制config.generators.test_framework为'test_unit',除非你的测试框架恰好是“: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 :factory_girl, :dir => 'spec/factories'
    else
      generators.test_framework :test_unit, :fixture => false, :fixture_replacement => :factory_girl
    end
  end

我试图弄清楚该怎么做,是生成我自己的初始化程序,在 FG 的初始化程序之后运行,将 test_framework 设置回 :lrdspec,如我所愿。

我尝试将自己的railtie放入config/initializers中,或者在config/application.rb中的config.after_initialize中添加一个块,以及许多其他方法,但还没有找到解决方案。 (我认为我对 Rails 初始化序列的了解需要比实际情况更深入一些)。

谢谢!

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 :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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

殊姿 2024-12-16 22:04:58

好的 - 找到了解决方案。有时仅仅发布一个问题就可以帮助思考。

答案是在包含脚手架生成器的 gem 中设置我自己的初始化程序,并传递 :after =>;当我创建该块时,将“factory_girl.set_fixture_replacement”初始化()。事实上,Rails docco 中没有记录您可以在初始化器中指定 :after 的事实,但可以通过发现 Initializes 使用 TSort 对其初始化器集合进行排序、研究 TSort 的工作原理并发现存储的 :after/ 来推断:before 参数用于 TSort 回调的方法中。

因此,解决方法是将其放入我自己的 gem(提供脚手架生成器的配置 Railtie)中:

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   

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   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文