views:

973

answers:

2

I've got a gem (will_paginate) that's broken on my version of Oracle. The default paginate_by_sql method in the WillPaginate module is inserting an extra 'AS' into a query and causing it to fail.

The code itself is easily fixed, but i'm not sure of the best way to get rails to pick up my change.

I don't want to change the code in the gem itself, as that will leave my code broken on other machines.

I've tried creating an lib/test.rb file containing

module WillPaginate
  def paginate_by_sql
    (my code goes here)
  end
end

and requiring it from environment.rb, but it's not picking up my changes. I've also tried requiring it from controllers/application.rb, but again, not picking up my changes.

Temporarily, I've got it to work by overriding the method within the specific model itself, but this is a bit of a hack, and means I can't use it on any of the other models in this project.

I'm sure there's an easy way to do this, but I'm not having any luck tracking it down on google.

Cheers Dave Smylie

+5  A: 

What you are doing will work, but your code needs to look like this:

module WillPaginate
  module Finder
    module ClassMethods
      def paginate_by_sql(sql, options)
        # your code here
      end
    end
  end
end

In other words, go into finder.rb, delete everything except the module headers and the method you want to override, then save to a file in lib and include in environment.rb. Voila, instant monkey patch!

Sarah Mei
Cool. Thanks. That did the trick =)
Dave Smylie
+3  A: 

A more concise solution:

WillPaginate::Finder::ClassMethods.module_eval do
 def paginate_by_sql sql, options
   # Your code here
 end
end

Put the the code into an initializer file in config/initializers. This is the correct place to put code that needs to be run when the environment is loaded. It also better organises your code, making each file's intent clearer, thus bugs will be easier to track down. Do not clutter up environment.rb!

Steve Graham