~/ Contributing to Ruby gems

I recently came across a gem for handling user authentication with sinatra - sinatra simple authentication. This gem was easy to install and use, but the log in and sign up views were hardcoded in Haml.

This meant I couldn’t take advantage of Sinatra’s layout.erb global view, and any shared markup (navigation, footer, etc.) would have to be copied and pasted into each of the views, and be written in Haml!!

So I decided to fork the gem and convert the views to use erb.

Fork and clone the repo

First, you’ll need to fork the repo on github and clone your fork:

$ git clone git@github.com:jshawl/sinatra-simple-authentication

Build the gem locally

First install dependencies:

$ bundle install

And build the gem:

$ gem build sinatra-simple-authentication.gemspec 

Require the local gem from another ruby app

In a separate directory, you’ll need a ruby application with a gemfile and an app.rb to see the changes that you make to the gem - i.e. link to the local one.

Here’s the one I’m working with - view on github

In the Gemfile, add the path to the local gem:

diff --git a/Gemfile b/Gemfile
index 418bda9..770b9f6 100644
--- a/Gemfile
+++ b/Gemfile
@@ -7,5 +7,5 @@ gem 'activerecord'
 gem 'sinatra-contrib'
 gem 'sinatra-activerecord'
 gem 'pg'
-gem 'sinatra-simple-authentication'
+gem 'sinatra-simple-authentication', :path => "~/Sites/sinatra-simple-authentication"
 gem 'rack-flash3'

Install the dependencies from this new Gemfile:

$ bundle install

And run the application:

$ bundle exec ruby app.rb

The bundle exec tripped me up a bit at first. Without it, I kept getting:

kernel_require.rb:55:in `require': cannot load such file -- sinatra/simple-authentication (LoadError)

when trying to run my application with ruby app.rb.

bundle exec is required to load the local gem we installed with bundle install. The man page says it perfectly:

Essentially, if you would normally have run something like rspec spec/my_spec.rb, and you want to use the gems specified in the Gemfile(5) and installed via bundle install(1), you should run bundle exec rspec spec/my_spec.rb.

Make changes

Now you can freely edit the files in your local gem's folder. Here are the changes I made to the sinatra simple authentication gem - view on github

Pull request?

If you push your changes back to github, others can use your fork of the gem by specifying a git url and branch name in their Gemfile:

diff --git a/Gemfile b/Gemfile
index 418bda9..39ae19b 100644
--- a/Gemfile
+++ b/Gemfile
@@ -7,5 +7,5 @@ gem 'activerecord'
 gem 'sinatra-contrib'
 gem 'sinatra-activerecord'
 gem 'pg'
-gem 'sinatra-simple-authentication'
+gem 'sinatra-simple-authentication', :git => "https://github.com/jshawl/sinatra-simple-authentication.git", :branch => "master"

If you think other users of this gem would benefit from your changes, submit a pull request on github!


~/ Posted by Jesse Shawl on 2014-12-20