Strategy for Building a Custom RubyGem
Building your own RubyGem may seem like a challenging task. In this guide we'll walk through how to take small, manageable steps for building out a custom Gem.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

Nice work going through that section, you should now have a much better idea on what ruby gems are and how they can be incorporated into our application. We also now have the bootstrap gem built in which is going to be the start of how we can implement much better-looking styles into our app.

In this deep dive, we're going to continue working with our application. You saw how to go and research and how to pull in ruby gems. I want to walk through how you can create your very own gem and how you can publish it to the web so other people can use it.

We're going to build a pretty base gem and it's going to allow us to have a dynamic copyright that is placed right at the bottom of our site. It will have things such as a dynamic date, your name or business name. It can be called from either the view, the controller or a view helper. I want to show you how you can create your very own gem from scratch. Then you can use that knowledge in the future to build even bigger gems

We're going to spend our time on this remaining task, which is to build a custom gem. I originally started outlining a gem that outputs your top rated skill, however, that's such a trivial thing that I really wanted to change it. Instead, I thought it would be cool to build a helper module that generates our fully formatted copyright. I would use a view helper method for this, we want to have a copyright on the bottom of each of these pages where it says

  • the year (it keeps up with the year automatically)
  • copyright
  • the ability to have a message

Usually, you will be using a view helper to do this, but I thought it would be a good way to learn about how to create your own gem.

Let's talk about the first kind of strategies that you could take when building a gem. It's usually considered a bad practice and it would be very buggy to just say "I want to create a gem from scratch" without having some kind of build already in your application. I'm going to show you exactly what my process is whenever I'm building a gem that I'm going to add to an application.

First, add a module into whatever file I want to call the gem from. In this case, I'm just going to start off with the application controller and say module. Usually, you want to have a unique name, such as what your last name or your company name, I usually like to prepend my company name or my name to the module. My gem name is going to be called DevcampViewTool.

Inside of the module, I'll create a new class, I'm going to call it Renderer. Inside of the class, I'm going to create a class method. I'm going to say self.copyright I'm doing this is because maybe down the line I want to update this and have other types of rendered items. I would be able to simply add it to the renderer class. For now, we're just going to be doing copyright.

I'm going to say that our method is going to take a name and it's going to take a message. This is where I'm going to create the HTML content. We're going to use the HTML copyright symbol, followed by copy followed by a semi-colon. Then we're going to use string interpolation (make sure that you are using double quotes whenever you're doing this, string interpolation does not work with single quotes) I'm going to say which is going to output the current year when a user accesses the site. I'm going to add the pipe to give a little separation and using the HTML tag (you can use this or you can use your own custom class) then I'm going to pass in the value of name. Lastly, I'm going to pass in the message and then we have to say

module DevcampViewTool
  class Renderer
    def self.copyright name, msg
      "&copy; #{} | <b>#{name}</b> #{msg}".html_safe

Now we have a module that we can call and it has a class of renderer and then that has a method called copyright that will generate the copyright for us.

Let's create before_action :set_copyright. I can create a method called set_copyright and inside create an instance variable called copyright that is equal to camp view tool::renderer.copyright. This is going to call the method that we created and then we pass in the two arguments. So, in this case, we pass in "Jordan Hudgens" and then the message "All rights reserved". This gives us the flexibility to name whatever we want. You could customize it in any way that you need.

def set_copyright
  @copyright = DevcampViewTool::Renderer.copyright 'Jordan Hudgens", "All rights reserved"

let's go to the main application layout file, underneath the yield, I'm going to say <%= @copyright %> and hit save.

Come back to Chrome and hit refresh, you'll see that this generates this copyright for us. It adds our formatting, it pulls in the current year, it adds my name in bold and then it says All rights reserved. You could have it say anything you want, it can be dynamic, you could put in the page title.

Whenever I'm building a new gem, I usually like to start here or maybe in the lib directory. I take this approach because I feel strongly that one of the biggest keys to success as a developer is taking very small easy to manage steps. If I had a syntax error or a mistake I wouldn't have to worry about things such as code organization.

Since I started with the code right under where it's going to be called, I know that if there is a bug with this code it's not with how I organized it or the path names or if I called it properly. I know that in these about 15-16 lines of code right here I have encapsulated all of this data and now that I know what works we can extract it out and

In the next guide that's what we're gonna do. We're going to start creating our ruby gem and we are going to take what we created right here and install it into the gem.