I'm trying to write a plugin that aliases some methods in ActiveRecord in the following fashion:
class Foo < ActiveRecord::Base
include MyOwnPlugin
acts_as_my_own_plugin :methods => [:bar]
def bar
puts 'do something'
end
end
Inside the plugin:
module MyOwnPlugin
def self.included(base)
base.class_eval do
extend ClassMethods
end
end
module ClassMethods
def acts_as_my_own_plugin(options)
options[:methods].each do |m|
self.class_eval <<-END
alias_method :origin_#{m}, :#{m}
END
end
end
end
end
This approach won't work because when #acts_as_my_own_plugin is run, Foo#bar is not defined yet because it hasn't been run.
putting acts_as_my_own_plugin :methods => [:bar] AFTER the bar function declaration will work. However this is not pretty.
I want to be able to have the acts_as_my_own_plugin placed on top of the class definition as most plugins do.
Is there an alternative approach to satisfy this condition?