Rails 3.0 Engine - Run Code in ActionController

I am updating my Rails plugin to be an engine that works with the latest version 3.0RC1, and I am having trouble finding the best (and most correct) extension method ActionController. I saw this post from DHH and this question is here on SO, but my question is more about how to properly call the code in ActionController.

For example, I need to call the following in my kernel controller:

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

I know how to enable my two private functions correctly, but I cannot find a way to call it correctly helper, before_filterand after_filter.

. - , ApplicationController, ApplicationController , . , . , , , ApplicationController.

+5
2

, . .

:


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

... , . . - :

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

, application_controller application_helper rails. , , , , , , /my _engine/app , /my _engine/lib

: https://gist.github.com/e139fa787aa882c0aa9c (_ , )

+10

, , , , , - .

lib, . myplugin/lib/extensions/action_controller_base.rb.

myplugin/lib/myplugin.rb :

require 'extensions/action_controller_base.rb'

myplugin/lib/extensions/action_controller_base.rb :

require 'action_controller'  # Make sure ActionController::Base is defined

ActionController::Base.class_eval {
  private
    def my_method_1
      # Code Goes Here
    end

    def my_method_2
      # Code Goes Here
    end
}

ActionController::Base.instance_eval {
  helper_method :my_method_1, :my_method_2

  before_filter :my_method_1
  after_filter :my_method_2
}

, myplugin/lib/helpers ( - lib, " " ) myplugin/lib/extensions/action_controller_base.rb:

require 'helpers/helper_file_1'
require 'helpers/helper_file_2'

ActionView::Base.send :include, MyHelper1
ActionView::Base.send :include, MyHelper2
+3

All Articles