Helper Gem
Ruby on Rails Gem Software Development
May 23, 2022 - Robert C.

The Start of the Bootstrap Helpers Gem for Ruby on Rails Development

One of my favorite things about software development is distilling the core of the code we write down into smaller parts that we can make into units of reusable work. In Ruby on Rails this means gems. In this post, I’m going to explain how to kick off thea bootstrap helpers gem.

But first, What is a gem in Ruby on Rails?

Gems, not unique to Ruby on Rails, are external libraries that developers can use to add functionality to a software project quickly. It allows developers to become extremely time-efficient when coding components and features that show up frequently in their projects.

There are gems for almost everything and, as you’ll see in this post, you can build your own to make YOUR coding more efficient.

The Goal of the Bootstrap Helpers Gem

This gem aims to take all the repeated markup that bootstrap requires and build a set of helpers that can be reused across projects to speed things up.

The danger to be avoided is inserting too much opinion about how bootstrap should be used. Bootstrap is an amazing tool that really helps get applications off the ground. It’s flexible and reusable. Many, many websites use it, in many different ways. This gem should not get in the way of that, but should instead make it a bit easier to use.

To that end, one thing this gem will not do is require a bootstrap asset gem. There are many many ways to include bootstrap into your project. This gem should not dictate the “correct” way to go about it. You should be able to use this gem if you’re using Bootstrap in any capacity, it’s your responsibility to get the css and javascript into your project. This gem is just concerned with the markup.

Building the bootstrap helpers gem in Ruby on Rails

Now for the fun stuff. We are going to look at building the gem and adding features to it. For our example application, we are going to use the Todo app.

In this first post, we are just going to set up a basic example.

Step 1: Generate The gem

bundle gem bootstrap_helpers

Step 2: Edit the Gemspec bootstrap_helpers.gemspec

 spec.summary       = %q{A set of helpers to generate bootstrap markup}
 spec.homepage      = "https://github.com/entrision/bootsteap_helpers"

Step 3: Commit to git, and push to github.

git add .
git commit -m "Add data to Gemspec"
git push

From here on out I won’t be showing the git commands. I just wanted to show it here because of the next step.

Step 4: Include the gem in the project

We need to include the gem in our example project, but pushing and downloading and all that is really a pain when you are just testing things. So we will use a bundler trick:

First add a the gem normally to the project:

gem 'bootstrap_helpers', github: 'entrision/bootsteap_helpers', branch: 'master'

Then we tell bundler to ignore all that and use our local version.

bundle config --local local.bootstrap_helpers /home/coteyr/Projects/bootstrap_helpers

And finally, we run bundle in our example project.

bundle
...
Using bootstrap_helpers 0.1.0 from https://github.com/entrision/bootsteap_helpers.git (at /home/coteyr/Projects/bootstrap_helpers@7cdbd93)
...

If we ever want to pull from github we can always remove the override:

bundle config --delete local.bootstrap_helpers

That it. Our gem has its skeleton, it’s included in our example project, and we removed the pain of always having to push and download just to see if we got the response we wanted.