Arnold Roa
Arnold Roa

Reputation: 7738

Override method (monkey patch?) on rails core

On this file:

...gems/actionpack-2.3.18/lib/action_controller/rescue.rb

Exists a method called local_request? inside module Rescue, that is contained in module ActionController, so its like this:

module ActionController
    ...
    module Rescue
       ...
       protected
       ...
       # True if the request came from localhost, 127.0.0.1. Override this
       # method if you wish to redefine the meaning of a local request to
       # include remote IP addresses or other criteria.
       def local_request?
         ....

I want to override that method in order to handle all requests as non-local, I tried with this (what I think is a monkey patch, not sure if the term is wrong)

module ActionController
  module Rescue
    protected
      def local_request? #:doc:
        false
      end
  end
end

It seems to work (that method return false), but when making a request I receive an error:

undefined method `call_with_exception' for ApplicationController:Class

That method exists in

module ActionController > module Rescue > module ClassMethods

1) If i'm overriding only one method why that got undefined? im deleting the other methods/modules inside the one i'm modifying?

2) How to and whats the right way to do this?

Upvotes: 0

Views: 870

Answers (1)

Frederick Cheung
Frederick Cheung

Reputation: 84132

In rails 2.3.x Rails uses ruby's autoload method to load the modules that make up ActionController (see here: the file defining each module is only loaded when the constant is first accessed.

Because you are defining a constant of the same name, autoload will never get triggered (since as far as ruby is concerned it doesn't need to), so the rails provided code never gets loaded. You could instead do

ActionController::Rescue.module_eval do
   def local_request?
      false
   end
end 

Although as pointed out in the comments you can just define this on your controller - no need to do it this way.

Upvotes: 1

Related Questions