作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想我会想出一个巧妙的方法来在 Rails 3.x gem 中扩展 ApplicationController。
在我的 gem 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
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)
lib/gem_namespace/application_controller.rb
中定义了 ApplicationController作为:
class ApplicationController < ActionController::Base
end
app/controllers/application_controller.rb
中重新定义。 , 这样扩展
ApplicationController
的应用程序中的两个 Controller 和扩展
MyNamespace::MyController
的 Controller 将直接或间接扩展
app/controllers/application_controller.rb
中定义的 ApplicationController .
ApplicationController
的 Controller 无法访问
app/controllers/application_controller.rb
中定义的方法.此外,
ApplicationHelper
(app/helpers/application_helper.rb)
模块不再被其他帮助模块加载。
ApplicationController
在我的 gem 的 Controller 中,用于定义
before_filter
和
after_filter
使用
initialize
访问类的名称以确定关联模型的类,然后它可以在其方法中存储和使用?
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
最佳答案
对于这种特定类型的功能,我建议在您的 gem 中创建一个模块并将该模块包含在您的应用程序 Controller 中
class ApplicationController < ActionController::Base
include MyCoolModule
end
def self.included(base)
base.send(:before_filter, my_method)
end
base.before_filter :my_method
哪个更干净。
关于ruby-on-rails - 如何在 gem 中扩展 ApplicationController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11348332/
我是一名优秀的程序员,十分优秀!