简体   繁体   English

Railtie Initializer未在插件中运行

[英]Railtie Initializer not running in plugin

I recently switched from the gem version of resources_controller to a plugin as the gem version relied on git . 近日笔者从创业板的版本切换resources_controllerplugin作为gem版本上依赖git

Inside the vendor/plugins/plugin/lib/plugin.rb file, the Railtie is as follows: vendor/plugins/plugin/lib/plugin.rb文件中, Railtie如下:

module Ardes
  module ResourcesController
    class Railtie < Rails::Railtie
      initializer 'ardes.resources_controller' do
        ActiveSupport.on_load(:action_controller) do
          extend Ardes::ResourcesController
          include Ardes::ResourcesController::RequestPathIntrospection
        end

        ActiveSupport.on_load(:active_record) do
          include Ardes::ActiveRecord::Saved
        end
      end
    end
  end
end

I've added a require 'resources_controller' in one of my initializers and it's properly loading this file. 我在我的一个初始化程序中添加了一个require 'resources_controller' ,它正确加载了这个文件。 The problem is that although the Railtie is evaluated (a puts in the class block will hit), it never seems to actually call the initializer block itself. 问题是,虽然Railtie评估(一puts于类块会打),它似乎永远不会实际调用初始化块本身。 This is important of course as this is where it extends ActionController to include the resources_controller_for method. 这当然很重要,因为这是它扩展ActionController以包含resources_controller_for方法的地方。

This question is appears to have come up here and here . 这个问题似乎已经出现在这里这里 Though in both cases they found other ways around the problem and no direct answer was given for why the block wasn't being called. 虽然在这两种情况下他们都找到了解决问题的其他方法,但没有直接回答为什么没有调用块。

From what I can tell in the Rails docs you can name your initializer block anything you'd like and it should run. 从我在Rails文档中可以看出,您可以将初始化程序块命名为您想要的任何内容,并且它应该运行。 I don't think it matters, but I first noticed the problem when running in production rails s -e production though I believe the same problem exists in development mode. 我认为这不重要,但我首先注意到在生产rails s -e production运行rails s -e production时的问题,但我相信在开发模式中存在同样的问题。

What may be going on? 可能会发生什么?

For reference, full plugin is here: https://github.com/ianwhite/resources_controller 作为参考,完整的插件在这里: https//github.com/ianwhite/resources_controller

The problem you're having here is that you can't add new initializers once the initializer process has started. 您在这里遇到的问题是初始化过程开始后您无法添加新的初始化程序。

Here, you're requiring the code that registers the initializers during the initializer process. 在这里,您需要在初始化过程中注册初始化程序的代码。 When you use gems in the Gemfile, the initializers are registered in this code: 在Gemfile中使用gem时,初始值设定项在以下代码中注册:

if defined?(Bundler)
  # If you precompile assets before deploying to production, use this line
  Bundler.require(*Rails.groups(:assets => %w(development test)))
  # If you want your assets lazily compiled in production, use this line
  # Bundler.require(:default, :assets, Rails.env)
end

This code executes before the initializers begin. 此代码在初始化程序开始之前执行。 In contrast, you are requiring the resources_controller code in an initializer file, which runs during the initialization process. 相反,您需要在初始化程序文件中使用resources_controller代码,该文件在初始化过程中运行。 As a result, it's too late to register new initializers. 结果,注册新的初始化器为时已晚。

What complicates the situation is that the load paths inside vendor/plugins are also set up during the initialization process, so you won't be able to require resources_controller in application.rb . 使情况复杂化的是vendor/plugins内的加载路径也在初始化过程中设置,因此您将无法在application.rb需要resources_controller

The easiest solution to your problem would be to use the :path feature in bundler. 解决您问题的最简单方法是在bundler中使用:path功能。 After installing the plugin, add this line to your Gemfile: 安装插件后,将此行添加到您的Gemfile:

gem 'resources_controller', :path => "vendor/plugins/resources_controller"

You can then remove the require line from your initializer, and bundler will recognize that the plugin is a locally checked out gem and do what it would do if you used git. 然后,您可以从初始化程序中删除require行,并且bundler将识别该插件是本地检出的gem并执行使用git时将执行的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM