19

我想我会想出一个巧妙的方法来在 Rails 3.x gem 中扩展 ApplicationController。

在我的宝石中lib/my_namespace/my_controller.rb,我有:

class MyNamespace::MyController < ApplicationController

  before_filter :some_method
  after_filter :another_method

  def initialize
    # getting classname of the subclass to use for lookup of the associated model, etc.
    # and storing the model_class in an instance variable
    # ...
  end

  # define :some_method, :another_method, etc.
  # ...

private
  attr_accessor :subclass_defined_during_initialize # etc.

  # etc.
end

但是当 Gem 被加载时,app/controllers/application_controller.rb还没有加载,所以它失败了:

/path/to/rvm/gemset/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:251:
in `require': cannot load such file -- my_gem_name/application_controller (LoadError)

作为一种解决方法,我在我的 gem 中将 ApplicationController 定义lib/gem_namespace/application_controller.rb为:

class ApplicationController < ActionController::Base
end

我假设即使我在那里定义了它,它也会在我的 Rails 3 应用程序中重新定义app/controllers/application_controller.rb,这样扩展的应用程序中的控制器和扩展的ApplicationController控制器MyNamespace::MyController都会直接或间接扩展app/controllers/application_controller.rb.

但是,我们注意到加载 gem 后,扩展ApplicationController的控制器无法访问app/controllers/application_controller.rb. 此外,该ApplicationHelper (app/helpers/application_helper.rb)模块不再被其他辅助模块加载。

我如何ApplicationController在我的 gem 中的控制器中进行扩展,以便定义 abefore_filterafter_filterto 并使用它initialize来访问类的名称以确定关联模型的类,然后它可以在其方法中存储和使用该类?

2012 年 10 月 22 日更新

这是我想出的:

lib/your_gem_name/railtie.rb

module YourGemsModuleName
  class Railtie < Rails::Railtie
    initializer "your_gem_name.action_controller" do
    ActiveSupport.on_load(:action_controller) do
      puts "Extending #{self} with YourGemsModuleName::Controller"
      # ActionController::Base gets a method that allows controllers to include the new behavior
      include YourGemsModuleName::Controller # ActiveSupport::Concern
    end
  end
end

并在lib/your_gem_name/controller.rb

module YourGemsModuleName
  module Controller
    extend ActiveSupport::Concern

    # note: don't specify included or ClassMethods if unused

    included do
      # anything you would want to do in every controller, for example: add a class attribute
      class_attribute :class_attribute_available_on_every_controller, instance_writer: false
    end

    module ClassMethods
      # notice: no self.method_name here, because this is being extended because ActiveSupport::Concern was extended
      def make_this_controller_fantastic
        before_filter :some_instance_method_available_on_every_controller # to be available on every controller
        after_filter :another_instance_method_available_on_every_controller # to be available on every controller
        include FantasticStuff
      end
    end

    # instance methods to go on every controller go here
    def some_instance_method_available_on_every_controller
      puts "a method available on every controller!"
    end

    def another_instance_method_available_on_every_controller
      puts "another method available on every controller!"
    end

    module FantasticStuff
      extend ActiveSupport::Concern

      # note: don't specify included or ClassMethods if unused

      included do
        class_attribute :class_attribute_only_available_on_fantastic_controllers, instance_writer: false
      end

      module ClassMethods
        # class methods available only if make_this_controller_fantastic is specified in the controller
        def some_fanastic_class_method
          put "a fantastic class method!"
        end
      end

      # instance methods available only if make_this_controller_fantastic is specified in the controller
      def some_fantastic_instance_method
        puts "a fantastic instance method!"
      end

      def another_fantastic_instance_method
        puts "another fantastic instance method!"
      end
    end
  end
end
4

4 回答 4

9

对于这种特定类型的功能,我建议在您的 gem 中创建一个模块并将该模块包含在您的应用程序控制器中

class ApplicationController < ActionController::Base
  include MyCoolModule
end

添加前置过滤器等(将其添加到您的模块中)

def self.included(base)
  base.send(:before_filter, my_method)
end

更新:您也许可以做base.before_filter :my_method更清洁的事情。

于 2012-07-05T16:11:46.310 回答
5

这是一个要点 ,它显示了如何访问子类的类并将其存储在实例变量中,并在前后过滤器中访问它。它使用包含方法。

于 2012-07-05T23:37:44.577 回答
2

真理要简单得多,灵活得多。

添加lib/engine.rb到此:class Engine < Rails::Engine; end

然后只需使用:

ActionController::Base.class_eval do

  include SomethingFromMineGemModule

  # or:
  def hello_from_gem
    'Hey people!'
  end

end
于 2016-10-24T17:30:13.623 回答
0

我能够使用初始化回调来引用 ApplicationController。

子类/引用 ApplicationController 的 gem 代码:

class GemApplicationController < ApplicationController
  before_filter :method_to_call

  def method_to_call
    #your code here
  end
end

用于创建子类控制器的 gem 代码回调:

module GemName
  def self.load_gem_application_controller
    require "path/to/gem_application_controller"
  end
end

rails_app/config/initializers/gem_name.rb

GemName.load_gem_application_controller

然后让控制器使用此功能的子类 GemApplicationController

class SpecialCaseController < GemApplicationController
  # this will inherit from the gem's controller, 
  # which inherits from the rails_app ApplicationController
end
于 2013-06-14T19:58:21.583 回答