61

I've found myself twice in this situation: I install a gem on my system and start using it from my Rails project. Eventually I need to make some changes to that gem. How should I proceed?

Ideally I'd like to check out the source code of that gem somewhere, like ~/third_party/gems, work on it and have my Rails project use that instead. Is that possible?

In all the cases the gems were at github so I would probably for it at github, clone it, make my chances and maintain my own branch. I suppose then I would install that branch directly with gem install on my server. Does that make sense?

Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622

2 Answers2

107

Today this is pretty easy to do with Bundler. You make a local copy of the gem and then instead of doing

gem "whatever"

in your Gemfile, you do:

gem "whatever", :path => "/home/pupeno/whatever"

After running bundle install, the gem is picked from that directory. Even if you modify something in there, all you need to do to re-load it is restart Rails.

If you need to deploy an application using your own changes of a Gem, you make a fork, on Github or similar and on the Gemfile you do:

gem "whatever", :git => "git@github.com:/pupeno/whatever.git"

and that's it. It's simple, straightforward and beautiful.

Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622
  • 2
    This is useful, because in some cases, there is a gem that is stale, or has functionality that I want to use as a staring point. Thanks. – Dustin M. Oct 27 '10 at 19:22
  • 19
    I suggest using the public git url. `git://github.com/pupeno/whatever.git` instead of the private (`git@githpb.com:pupeno/whatever.git`) in case other people use your project and don't have private access to your repo – Amiel Martin Apr 17 '12 at 23:57
  • 2
    how do you get the github address of the exact gem version? – Ben G Jul 24 '13 at 21:47
  • 1
    To use the gem at a specific version use `:ref` (or `:branch` or `:tag`). http://stackoverflow.com/questions/6119946/how-to-get-a-specific-commit-of-a-gem-from-github – Patrick Mar 15 '14 at 20:35
  • ... And to see the available versions in github visit `github.com/user-name/repo-name/releases` – Patrick Mar 15 '14 at 20:42
  • You suggest storing the gem in /home. Is it bad practice to store it inside the rails project you're working on? If not, what folder should I then use? – bo-oz Dec 10 '15 at 11:37
  • How do you then update the gem when the original gem updates, but also keep your changes? – Adzz Nov 16 '16 at 10:42
3

In all the cases the gems were at github so I would probably for it at github, clone it, make my chances and maintain my own branch. I suppose then I would install that branch directly with gem install on my server.

If you really need to hack the actual gem source then yes, that would be the way to do it. However, it should be a last resort. You don't want to maintain the actual gem if you don't have to. Why not extend classes from the gem source whose functionality you need to change and use your classes instead of the gem classes in your Rails code?

I find it rare that you actually need to hack 3rd party code directly to do what you need to do. Good software can be extended/easily augmented.

Chris
  • 1,826
  • 12
  • 15