A collection of computer systems and programming tips that you may find useful.
Brought to you by Craic Computing LLC, a bioinformatics consulting company.

Friday, August 16, 2013

Testing for the presence of a loaded Gem in a Rails application

I'm migrating the search function in an application from meta_search to ransack (both from Ernie Miller)

The syntax has changed quite a bit between the two gems and I use the search functionality in more than 30 controllers / index pages. So I want a simple way to update each instance while still being able to switch back to meta_search if I need to.

So I need to test which of the two gems has been loaded in the running application.

The best way that I have found is this:

if Gem.loaded_specs['ransack']

'... do this...'


'... do that ...'


You can see the full list of loaded gems with Gem.loaded_specs.keys

No comments:

Archive of Tips