I’m trying to monkey patch an existing ActiveRecord Observer; specifically, IssueObserver from the code base of Redmine 1.0.4.
In a plugin’s init.rb, I have include the *patch.rb file:
require File.join(File.dirname(__FILE__), 'lib/issue_observer_patch.rb')
IssueObserver.send :include, IssueObserverPatch
This is done outside the Redmine::Plugin.register block.
The module does something like:
module IssueObserverPatch
def self.included(base)
base.send :alias_method_chain, :after_create, :audit
end
def after_create_with_audit(issue)
after_create_without_audit(issue)
issue.logger.info('***'*50)
# Insert a new Audit instance.
end
end
However, when I run the server (using script/server, which is using Mongrel), the patch seems to do nothing. The ***..*** string does not get logged when I create a new issue.
By changing the IssueObserver and including a sentence to log the after_create event, and then running the server and creating an issue; I can see this log, but not the log on after_create_with_audit method.
Is there any proper way to patch an ActiveRecord Observer?
It seems that Rails VM are loaded and unloaded on demand. So, changing the code on the init.rb to the following solves the problem:
This makes this block of code to be execute per VM on production and per request on development.