how do I override a method in a gem?

1,345 views
Skip to first unread message

Walter Lee Davis

unread,
Jun 1, 2010, 10:30:18 AM6/1/10
to phil...@googlegroups.com
I am using mbleigh-acts-as-taggable-on-1.0.5 in an app, and discovered
that I was having odd things happening when a tag was used in both its
Title case and lowercase forms. So I unpacked the gem and changed the
clean! method in tag_list.rb from

def clean!
reject!(&:blank?)
map!(&:strip)
uniq!
end


to

def clean!
reject!(&:blank?)
map!(&:strip)
map!(&:downcase)
uniq!
end

This worked just fine, but now I'd like to do it the "right" way, as
opposed to hacking on the gem. I believe I can add a file in a
particular place in my app hierarchy that will shadow this file and
override its clean! method. Where would that particular place be? Or
am I thinking about this the wrong way?

Thanks,

Walter

Jon Bringhurst

unread,
Jun 4, 2010, 10:10:05 AM6/4/10
to Philly.rb
Just fyi, for some reason your email got filtered as spam in my gmail
acct (which is probably why you didn't get like 100 responses right
away).

That said, the "right way" is to fork the project on github, make your
changes, and push your changes back to the original author so everyone
can enjoy them.

If you just want a quick fix, you can "monkey patch" the class by
opening it. Take a look at this article for a few examples of opening
a class: <http://stackoverflow.com/questions/394144/what-does-monkey-
patching-exactly-mean-in-ruby>.

-Jon

Angel Pizarro

unread,
Jun 4, 2010, 10:32:35 AM6/4/10
to phil...@googlegroups.com
Also, it has a similar solution to your previous question RE PaperClip: put your monkey patch in the rails app's initializer dir 
-angel

--
You received this message because you are subscribed to the Google Groups "Philly.rb" group.
To post to this group, send email to phil...@googlegroups.com.
To unsubscribe from this group, send email to phillyrb+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/phillyrb?hl=en.




--
Angel

Reply all
Reply to author
Forward
0 new messages