8 steps for fixing other people's code

Guide how to make fixes in other people's GitHub repositories. It's basically "Open Source Development 101".

Way back in mid-2007, when Rails 1.2 was the new hotness and GitHub was still a year away from crawling out of the primordial internet soup, prolific open source contributor Dr Nic wrote an article titled “8 steps for fixing other people’s code”. (...)

Here in the fantastical future world of 2012, while we still don’t have hoverboards or household nuclear fusion, we do have some great tools that make fixing other people’s code...

Capistrano: Bundler stalls and asks for "Username"

Given you use Capistrano together with bundler to automatically install your gems when deploying.

I recently had the problem that Capistrano stalled like this:

[err :: host.name.tld] Username:

It turned out that I this originated from GitHub. We had a gem in our Gemfile that explicitly pointed to a GitHub URL like that:

gem 'foogem', :git => 'https://github.com/blubb/foogem.git'

The URL was returning a 404 which caused the problems. You have to get another gem or point to a fork on GitHub.

MongoMapper for Rails 2 on Ruby 1.9

MongoMapper is a MongoDB adapter for Ruby. We've forked it so it works for Rails 2.3.x applications running on Ruby 1.9. [1]

makandra/mongomapper is based on the "official" rails2 branch [2] which contains commits that were added after 0.8.6 was released. Tests are fully passing on our fork for Ruby 1.8.7, REE, and Ruby 1.9.3.

To use it, add this to your Gemfile:

gem 'mongo_mapper', :git => 'git://github.com/makandra/mongomapper.git', :branch => 'rails2'

...

How to silence UTF-8 warnings on Rails 2.3 with Ruby 1.9

Rails 2.3.16+ on Ruby 1.9 causes warnings like this:

.../gems/activesupport-2.3.17/lib/active_support/core_ext/string/output_safety.rb:22: warning: regexp match /.../n against to UTF-8 string

Many thanks to grosser for supplying a monkey-patch for Rails 2.3 (Commit f93e3f0ec3 fixed it for Rails 3). Just put it into config/initializers/ to make those warnings go away.

Since we're using RSpec on mos...

Capturing signatures on a touch device

If you need to capture signatures on an IPad or similar device, you can use Thomas J Bradley's excellent Signature Pad plugin for jQuery.

To implement, just follow the steps on the Github page.

The form

If you have a model Signature with name: string, signature: text, you can use it with regular rails form like this:

- form_for @signature, :html => { :class => 'signature_form' } do |form|
  %dl
    %dt
      = form...

Git: How to check out branches that exist on multiple remotes

So you're using multiple remotes that offer the same branch?

$ git branch -a | grep my-branch
  remotes/something/my-branch
  remotes/origin/my-branch

And when trying to check out that remote branch, it fails for you with an error like this?

$ git checkout my-branch
error: pathspec 'my-branch' did not match any file(s) known to git.

Git usually guesses the remote branch to check out, but when using more than one remote, it seems like it no longer can do that.
Even if the branch is the same on both remotes, you nee...

rsl/stringex · GitHub

Stringex is a gem that offers some extensions to Ruby's String class. Ruby 1.9 compatible, and knows its way around unicode and fancy characters.

Examples for stringex's String#to_url method:

# A simple prelude
"simple English".to_url => "simple-english"
"it's nothing at all".to_url => "its-nothing-at-all"
"rock & roll".to_url => "rock-and-roll"

# Let's show off
"$12 worth of Ruby power".to_url => "12-dollars-worth-of-ruby-power"
"10% off if you act now".to_url => "10-percent-off-if-you-act-now"

# You do...

lang/unicode_utils · GitHub

UnicodeUtils implements Unicode algorithms for case conversion, normalization, text segmentation and more in pure Ruby code.

If you don't need the ton of features that UnicodeUtils offers, try stringex.

git "fatal: bad config file line" after checking out branch

If git gives you an error message such as "fatal: bad config file line 123 in .git/config" after you tried to checkout a branch with a very long branch name, you very likely come across a bug in git version < 1.8.

You should ask someone with a newer git version (someone pushed the branch right?) to rename the branch to something shorter:

git -m old-very-very-long-branch-name new-short-branch-name

How to update a single gem conservatively

The problem

Calling bundle update GEMNAME will update a lot more gems than you think. E.g. when you do this:

bundle update cucumber-rails

... you might think this will only update cucumber-rails. But it actually updates cucumber-rails and all of its dependencies. This will explode in your face when one of these dependencies release a new version with breaking API changes. Which is all the time.

In the example above updating cucumber-rails will give you Capybara 2.0 (because capybara is a dependency of `cucumber-rail...

daylerees/colour-schemes · GitHub

Awesome color schemes for RubyMine, Sublime Text 2 and other editors.

To install the themes into your Rubymine, copy intellij-themes/*.xml from the repository to your local ~/.RubyMine40/config/colors. Then restart RubyMine.

Enjoy!

Git: How to stash with a custom message

If you say git stash, your stashed changes will be identified with an automatically generated message:

$ git stash
Saved working directory and index state WIP on master: 77af0df Merge branch 'production'

While this is okay to temporarily stash away stuff, you may want a better identifier for your changes so you can find them more easily if you stash often.
Of course, there is a way to do it with git:

$ git stash save doing crazy things
Saved working directory and index state On master: doing crazy things

Note that you n...

Git blame: How to ignore white-space modifications

When doing a git blame, git will blame the person who added or removed white space in a line (e.g. by indenting), not the person who originally wrote the code.

Say git blame -w to ignore such white-space changes. You want this. \
Note that you can also use it when diffing: git diff -w.

Example

Consider this method, created by a user in commit d47bf443:

def hello
  'world'
end

^

$ git blame foo
d47bf443 (Arne Hartherz 2012-12-19 14:44:38 +0100 1) def hello
d47bf443 (Arne Hartherz 2012-12-19 14:44:38 +0100 2...

randym/axlsx · GitHub

Axlsx is an incredible gem to generate "Office Open XML" spreadsheet files (XLSX). Does not break on large spreadsheets and supports a ton of features like graphs.

API looks mature and existing code is easy to migrate when coming from the spreadsheet gem.
The documentation of some methods is a bit out of date, but you'll find your way around the gem's code.

No support for reading files, however. :( If you want to open XLSX spreadsheets (for example to confirm your output in tests), you can use [roo](h...

occ/TraceKit · GitHub

Tracekit is a JavaScript library that automatically normalizes and exposes stack traces for unhandled exceptions across the 5 major browsers: IE, Firefox, Chrome, Safari, and Opera.

Force GitHub Pull Requests to update the diff against its target branch

When you have a Pull Request on GitHub that includes commits from another Pull Request, you will still see them after the "child" PR has been merged. Unfortunately, GitHub won't automatically update the diff (or commit list).

Here is what worked for me:

  1. Check out the target branch
    1. git checkout my-target-branch
    2. Make sure you are up to date against origin (e.g. git fetch and git status). You should be 0 commits ahead or behind.
  2. Add and commit a file
    1. touch .please-update
    2. git add .please-update
    3. `gi...

Git: In an (interactive) rebase, find out which commit you are currently working on (until version < 1.7.9.5)

When you are using git rebase and are currently editing a commit (due to a conflict, for example), you may want to know the current commit. [1]\
Luckily, there is lots of useful stuff in the .git directory.

Commit hash that you are currently applying

cat .git/rebase-merge/stopped-sha

Useful if you want to inspect the original commit for its changes, for example like:

git show `cat .git/rebase-merge/stopped-sha`

Current commit's message

cat .git/rebase-merge/message

In case you forgot what the changes are suppo...

Git: Twelve Curated Tips And Workflows From The Trenches

This article contains:

  • Making ‘git diff’ wrap long lines
  • Set a global proxy
  • Clone only a specific branch
  • Diff file against remote branch
  • List all deleted files in the repository
  • Search for a string in all revisions of entire git history
  • Apply a patch from another (unrelated) local repository
  • Making a more recent branch the new master
  • Adding an initial empty commit to a branch to allow full rebase
  • Zero a branch to do something radically different
    -...

Git: Add all changes

A nice way to stage absolutely all changes (edits, additions, deletions):

git add --all

How to package a non-Ruby file into a gem

Great solution in a GitHub issue.

Git: Improve your commits by reviewing changes one-by-one

Git commits should be very deliberate, and only contain changes that you really want to be in there. In order to reduce the chance to accidentally commit something you didn't intend, review your changes before committing.

My preferred way of doing this is (only using git)

git add -N . # Add all paths, but not their contents
git add -p

Git will now show you all your changes in small chunks and ask you in an interactive mode whether you really want to add them.

The most helpful commands are

  • y: yes (add the change)
  • ...

Don't name columns like counter_cache columns in Rails pre v4.2.4

< Rails v4.2.4

ActiveRecord has a feature called counter caching where the containing record in a has_many relationship caches the number of its children. E.g. when you have House has_many :rooms, Rails can cache the number of rooms in House#rooms_count.

Mind that when a model has a column that looks to Rails like a counter-cache column, Rails will apply counter-cache logic to your model, even if you're not using counter caches.

E.g. you have a house with 12...

SearchableTrait is now a gem: Dusen

For two years we've been using SearchableTrait which gives models the ability to process Googlesque queries like this:

Contact.search('a mix of words "and phrases" and qualified:fields')

This trait used to be a huge blob of code without tests and documentation, so I made a gem out of it. Check out https://github.com/makandra/dusen for code, tests, and a huge README.

You should use the Dusen gem and delete SearchableTrait in all future projects.

Note that the syntax to define query proc...