I've taken over porting a large application from rails 2 to rails 3. The new application uses Datamapper as its ORM (decision taken prior to me coming to the project, so I can't change this).
The old application used the activerecord "after_find" callback quite extensively;
class Foo < ActiveRecord::Base
after_find :bar
def bar
#This would be called after an object was "found"
end
end
I need to implement this in Datamapper, but I can't just monkey patch the find methods because datamapper uses composition rather than inheritence;
class Foo
include Datamapper::Resource
end
so if I monkey patched the find methods, there would be no way of me calling the original method! Could anyone point me in the right direction?
== EDIT ==
I have tried including the following:
module ClassMethods
def after_find(meth)
class << self
alias_method :orig_first, :first
def first(*args, &block)
puts "XXX FOUND THE FIRST"
orig_first args, &block
end
end
end
end
But this just sends the code into an infinite loop (constantly printing "XXX FOUND THE FIRST")