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

555 Views Asked by At

I am trying to implement the inherited method for ActionController::Base class. My purpose is to call methods on classes that inherit from ActionController::Base like ApplicationController to make them include certain modules. At the momement my Code looks like this:

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

The code is called out of an init file of a plugin.

1

There are 1 best solutions below

2
On

inherited is a class method of Class. It can be overwritten directly to add behavior when a child class is defined. I don't see how you can do this by extending a module, but this should achieve the same result:

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

class ChildClass < ActionController::Base
end

and you will get the output:

child created: ChildClass