簡體   English   中英

是否可以在Rails 2.3.11中為ActionController :: Base類定義繼承的鈎子

[英]Is it possible to define the inherited hook for the ActionController::Base class in Rails 2.3.11

我正在嘗試為ActionController :: Base類實現繼承的方法。 我的目的是在從ActionController :: Base繼承的類(例如ApplicationController)上調用方法,以使它們包括某些模塊。 在我的代碼中,看起來像這樣:

module MyModule
 def inherited(child)
 end
 def my_test_method()
 end
end
ActionController::Base.send(:extend, MyModule)

ActionController::Base.methods.include? 'my_test_method'
=> true
ActionController::Base.methods.include? 'inherited'
=> false

該代碼從插件的初始化文件中調用。

繼承的是Class的類方法。 定義子類時,可以直接覆蓋它以增加行為。 我看不到如何通過擴展模塊來做到這一點,但這應該達到相同的結果:

class ActionController::Base
  def self.inherited(child)
    puts "child created: #{child}"
  end
end

class ChildClass < ActionController::Base
end

然后您將獲得輸出:

child created: ChildClass

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM