Customizing Public Rails Gems

Dmitry Sychev
3 min readJan 24, 2021

Here’s an interesting issue I’ve ran into recently. I found a couple of amazing gems that come with all sorts of functionality that was incredibly useful to my current project. These gems were intended to be use as pre-baked templates. Just add the gem and you’re off to the races.

Now, with these gems, this functionality is locked inside the gem and components like views and controllers are not exposed. Meaning if you wanted to change the view to adhere to the rest of your apps styling you wouldn’t be able to, as the view is locked away. Same with the controllers, if you would like to tweak any of the functionality, you wouldn’t be able to.

Here’s the part where I had a major revelation. I was digging around the github for the gem and realized that the actual gem is structured just like a Rails app!

Say what? See for yourself! Here’s the root of the gem project directory.

Here’s what inside the app directory looks like.

Looks familiar, right? Here’s what inside the views directory looks like.

If the gem layout matches that of a typical Rails app, that means we can edit that, right? Absolutely!

First thing you need to do is fork the repo. Once you fork it, clone it down to your local environment. From there, since all of the functionality is exposed, edit anything you’d like and push the changes to your github repo.

Now you have a custom version of a gem. To actually use your new gem in a project, all you need to do is remove the original unedited version from your project Gemfile and run bundle. After that, you add the gem again, but this time, point it to YOUR github repo for the gem, instead of the original one and run bundle once more.

At this point, you should see any of the changes you’ve made to the gem propagate to your app.

In the interest of full disclosure, there are a couple of pitfalls in this approach. The first one is ease of use. Any changes you make to your gem repo, will have to be pushed up and you’ll need to remove the gem from your Gemfile and add it again while running bundle in order to sync your project to the gems latest commit. The second is you’ve disconnected the gem from authors repo. Meaning you’ll be behind in versions. If a new version of the gem comes out with some functionality you’d like to use, you’d probably need to fork the original repo again and re-apply any of the edits you’ve made.

--

--