Rails 3.0引擎 - 在ActionController中执行代码

Top*_*gio 5 ruby-on-rails rails-engines applicationcontroller ruby-on-rails-3

我正在升级我的Rails插件,使其成为最新3.0RC1版本的引擎,我在找出最佳(也是最正确的)扩展方法时遇到了一些麻烦ActionController.我在DHH 看过这篇文章,这个问题在这里,但我的问题更多是关于如何正确调用代码ActionController.

例如,我需要在我的引擎控制器中调用以下内容:

class ApplicationController < ActionController::Base
  helper :all

  before_filter :require_one_user
  after_filter :store_location

  private
    def require_one_user
      # Code goes here
    end

    def store_location
      # Code goes here
    end
end
Run Code Online (Sandbox Code Playgroud)

我知道如何正确地包含我的两个私有函数,但我找不到让它正确调用的方法helper,before_filter并且after_filter.

我非常感谢一些链接或一种方法来使这项工作.我已经尝试过命名我的控制器以外的东西ApplicationController并且真正ApplicationController扩展它,但这似乎也不起作用.我真的很想找到能让发动机用户的生活变得尽可能简单的任何解决方案.理想情况下,他们不必扩展我的课程,但他们拥有自己的所有功能ApplicationController.

joh*_*ley 10

您可能还想查看引擎子类中的初始化器,因此您不必在控制器类中包含视图助手.这将使您可以控制这些模块的加载顺序.

这是我一直在使用的:


module MyEngine  
  class Engine < Rails::Engine  
    initializer 'my_engine.helper' do |app|  
      ActionView::Base.send :include, MyEngineHelper  
    end  

    initializer 'my_engine.controller' do |app|  
      ActiveSupport.on_load(:action_controller) do  
         include MyEngineActionControllerExtension  
      end
    end
  end
end

此外,动作控制器扩展的另一个选项是使用mixin模块.这将让你使用before_filter,after_filter等.


module MyEngineActionControllerExtension
  def self.included(base)
    base.send(:include, InstanceMethods) 
    base.before_filter :my_method_1
    base.after_filter :my_method_2
  end

  module InstanceMethods
   #...........
  end
end

另一件事......如果你在gem的顶层创建默认的rails目录,你不必担心需要帮助器或控制器.您的引擎子类可以访问它们.所以我在这里添加我的应用程序控制器和应用程序助手扩展

/myengine/app/helpers/myengine_application_helper_extension.rb
/myengine/app/controllers/my_engine_action_controller_extension.rb

我喜欢这个设置,因为它看起来类似于rails应用程序中的application_controller和application_helper.同样,这只是个人偏好,但我尝试保留任何直接与rails相关的内容,例如/ my_engine/app中的控制器,帮助器和模型以及与/ my_engine/lib中的插件相关的任何内容

有关初始化器的更多信息,请查看Jose Valim的本教程:https://gist.github.com/e139fa787aa882c0aa9c (engine_name现已弃用,但此大部分文档似乎是最新的)