Overriding default Rails methods

Hey guys,

What’s the best practices for overriding default Rails methods? I can’t
seem to find a guide on this topic anywhere online.

Here’s the deal - I want to override the request_forgery_protection for
forms because we don’t set session_ids for bots, and they are now
generating exceptions.

I have created a file called ‘override_request_forgery_protection.rb’,
put it in /lib, and added to application.rb the line:

require ‘override_request_forgery_protection.rb’

Now in the file I have put in the following code:

module ActionController::RequestForgeryProtection
def self.included(base)
base.class_eval do
alias_method_chain authenticity_token_from_session_id,
:session_check
end
base.extend(ClassMethods)
end

module ClassMethods

def authenticity_token_from_session_id_with_session_check
  if defined? session.session_id  # We have the session ID - behave

as rails normally would
authenticity_token_from_session_id_with_session_check
else # We don’t have a session ID, instead of croaking, lets just
say the authenticity token is bad
return false
end
end

end

end

However, this code doesn’t seem to be being executed. I am setting
breakpoints in eclipse, and they’re not being hit. Any ideas please?

Thanks!

I managed to get the desired outcome through the following (small)
piece of code:

module ActionController
module RequestForgeryProtection

def authenticity_token_from_session_id_with_session_check
  if defined? session.session_id  # We have the session ID -

behave as rails normally would
authenticity_token_from_session_id_without_session_check
else # We don’t have a session ID, instead of croaking, lets
just say the authenticity token is bad
return false
end
end

alias_method_chain :authenticity_token_from_session_id, :session_check
end

end