Extending Rails plugins?

Gents,

I’m using the acts_as_taggable rails plugin (not gem), and would like to
add some additional methods to it. For example, the find_tagged_with
methods essentially does a find tag in a list (effectively an OR), while
I’d like to implement a find_tagged_with_all method that would implement
an AND (so if I specified 4 tags, it would only return items that were
tagged with all four inclusively).

Is there a proper way to extend a Rails plugin so that problems are
minimized downstream when the plugin is upgraded? Or should I just dive
right in the plugin code and hack away?

FYR, I want to add a method to the following module:

module SingletonMethods
def find_tagged_with(list)
find_by_sql([
"SELECT #{table_name}.* FROM #{table_name}, tags, taggings "
+
"WHERE #{table_name}.#{primary_key} = taggings.taggable_id "
+
"AND taggings.taggable_type = ? " +
“AND taggings.tag_id = tags.id AND tags.name IN (?)”,
acts_as_taggable_options[:taggable_type], list
])
end
end

Thanks!

B.A.

B.A. Baracus: I thought you weren’t crazy no more?
Murdock: Only on paper.

I put mine in a module, TagExtension (tag_extension.rb) in the /lib
folder. Vaguely remember there was something on rails-weenie about it
too.
HTH

On Saturday, May 06, 2006, at 7:32 PM, Chris T wrote:

I put mine in a module, TagExtension (tag_extension.rb) in the /lib
folder. Vaguely remember there was something on rails-weenie about it too.
HTH

Hmm…ok, so I’ve created my own tag_extension.rb file in /lib, and it
follows the same convention as the plugin:

module ActiveRecord
module Acts #:nodoc:
module Taggable #:nodoc:
module SingletonMethods
#my additional methods here
end
end
end
end

This operates under the assumption that Ruby modules are open, just as
classes are, which based on a few irb tests they seem to be.

Now, how can I get my code to see it? The documentation says /lib files
are loaded automagically, but this doesn’t see to be appearing in my
models that call acts_as_taggable.

Thanks very much for your help!

B.A.

On Sunday, May 07, 2006, at 7:38 PM, BA Baracus wrote:

Now, how can I get my code to see it? The documentation says /lib files
are loaded automagically, but this doesn’t see to be appearing in my
models that call acts_as_taggable.

I’ll answer myself here…turns out that /lib is added a load
directory, but the files within /lib aren’t automatically loaded. To do
this, I had to require my file in config/environment.rb:

require plugin extension

require ‘my_extension’

Thanks for the help!

B.A.


B.A. Baracus: I thought you weren’t crazy no more?
Murdock: Only on paper.