Overriding the gem module method in Rails

The stiffness of will_paginate is broken in my version of Oracle. By default, the paginate_by_sql method in the WillPaginate module inserts an extra "AS" into the request and causes it to fail.

The code itself is easy to commit, but I'm not sure Rails will be able to get my changes.

I do not want to change the code in the stone itself, as this will leave my code broken on other machines.

I tried to create a lib / test.rb file containing:

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

and demanding it from environment.rb, but it doesn't pick my changes. I also tried to require it from the /application.rb controllers, but again, without raising my changes.

Temporarily, I got it to work by overriding the method inside the specific model itself, but it’s a little hack and means that I can’t use it on any of the other models in this project.

I'm sure there is an easy way to do this, but I have not been able to track it using Google.

+43
ruby ruby-on-rails gem will-paginate
Feb 24 '09 at 3:04
source share
3 answers

What you do will work, but your code should 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 to finder.rb, delete everything except the module headers, and the method you want to override, then save the file in lib and include it in environment.rb. Voila, an instant monkey patch!

+27
Feb 24 '09 at 4:44
source share

More concise solution:

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

Put the code in the initialization file in config / initializers. This is the right place to put the code that needs to be run when the environment boots. It also organizes your code better, making each file more understandable, so errors will be easier to track. Do not clutter environment.rb!

+60
Dec 05 '09 at 15:14
source share

Well, I’ll just make it easier for people like me who come and still struggle a bit by reading the other answers.

First, find the code you want to change in the github registry by searching for a line of code (you can easily find it with pry ) that you want to change in the stone, and then select Code on the left instead of Issues

enter image description here

enter image description here

Next, copy the contents of the module you want to modify and place it in the exactly named .rb file inside the config / initializers folder. Here is an example:

 module Forem module TopicsHelper def link_to_latest_post(post) text = "#{time_ago_in_words(post.created_at)} #{t("ago_by")} #{post.user}" link_to text, forum_topic_path(post.topic.forum, post.topic, :anchor => "post-#{post.id}") end end end 

Now change it to:

 Forem::TopicsHelper.module_eval do def link_to_latest_post(post) text = "#{time_ago_in_words(post.created_at)} #{t("ago_by")} #{post.user}" link_to text, forum_topic_path(post.topic.forum, post.topic, :anchor => "post-#{post.id}") end end 

Now make additional changes to the code and restart the server.

Go away!

+37
Jan 30 '14 at 1:10
source share



All Articles