Topic: need help for my first plugin

Hey all,

I just wrote my first plugin for ActiveResource. It works great but I need to put:

include MyPlugin

in all my models that use this plugin.

To avoid that, i did put:

ActiveResource::Base.send :include, MyPlugin

but it doesn't work (I get a NoMethodError: undefined method my_plugin_function).

Any idea what's the way to do this?

thanx in advance

Pat

Re: need help for my first plugin

Do you want all of your ARes models to inlcude this plugin? If so, you could create a file in your plugin to extend ARes:

module ActiveResource
  class Base
    include MyPlugin
  end
end

I believe that if you put that in the lib/ directory of your plugin, it will get picked up automatically when your app starts.

Re: need help for my first plugin

I usually put the code to extend whatever class in the init.rb, then it will load automatically.

Also depending on whether your doing class methods (things like User.find, that apply to the class as a whole) or instance methods (things like @user.update_attributes that apply to a single instance of a class) you will have to use extend or include respectively.

If you're using a mixture of the two this is the pattern thats generally adopted.

Alex