Navigating through the browser history in a cucumber feature using selenium

In order to navigate through the browser history. you can manipulate the window.history object via javascript like follows:

When /^I go back in the browser history$/ do
  page.evaluate_script('window.history.back()')
end 

For further functions of the window and history objects check out this link.


An improved version of this step is now part of our gem spreewald on Github.

Fix Capistrano warnings: Missing public directories

I got these warnings while deploying a Rails 3.2 app with asset pipeline enabled:

*** [err :: host.tld] find: `/opt/www/hollyapp.com/releases/20120503115342/public/images': No such file or directory
*** [err :: host.tld] find: `/opt/www/hollyapp.com/releases/20120503115342/public/stylesheets': No such file or directory
*** [err :: host.tld] find: `/opt/www/hollyapp.com/releases/20120503115342/public/javascripts': No such file or directory

Folders like public/javascripts might not exist if you're using the asset pipeline (...

Git: Change the text editor used to enter commit messages

In your ~/.gitconfig:

[core]
  editor=nano

Git: How to configure git to push only your current branch

You can change which branches will be pushed when saying git push. Our recommendation is to set it to current.

From the git-config documentation:

push.default
Defines the action git push should take if no refspec is given on the command line, no refspec is configured in the remote, and no refspec is implied by any of the options given on the command line. Possible values are:

  • nothing - do not push anything.
  • `matchin...

Shell script to deploy changes to production and not shoot yourself in the foot

Geordi, our collection of command line tools, has been extended by another command deploy-to-production. This script encapsulates the following workflow:

  • Pull the production branch.
  • Show which commits from the master would make it to production with this deploy.
  • Ask if you want to proceed.
  • If yes, merge the master into the production branch, push and deploy with bundle exec cap production deploy:migrations

The script will ask you for the names of your master branch, production branch an...

Git: Push a new branch and track it immediately

When you create a new branch and push it to origin, you won't be tracking it. This means a git pull won't know its remote version.

You could use difficult commands to set up a branch's tracking but it's easier to just push it like this:

git push -u

From the documentation on git push:

-u, --set-upstream
For every branch that is up to date or successfully pushed, add upstream (tracking) reference, used by argument-less git-pull(1) and other commands. For more information, see branch.<nam...

Ruby: Making your regular expressions more readable with /x and alternative delimiters

The following two hints are taken from Github's Ruby style guide:

If your regular expression mentions a lot of forward slashes, you can use the alternative delimiters %r(...), %r[...] or %r{...} instead of /.../.

 %r(/blog/2011/(.*))
 %r{/blog/2011/(.*)}
 %r[/blog/2011/(.*)]

If your regular expression is growing complex, you can use the /x modifier to ignore whitespace and comments:

regexp = %r{
  start         # some text
  \s            # white space char
  (group)    ...

Hack-fix Selenium::WebDriver::Element#select is deprecated

In some older Capybara versions (e.g. 0.3.9), we're getting lots of deprecations warnings:

Selenium::WebDriver::Element#select is deprecated. Please use Selenium::WebDriver::Element#click and determine the current state with Selenium::WebDriver::Element#selected?

Hani-elabed on Github helps. Add this code to features/support/env.rb to remove them temporarily:

# June 30th, 2011
# a temporary hack to disable the annoying upstream warnings capybara > selenium-webdriver 0.2.2
# capybara folks know about this and are working on it. S...

Make your Rails console (and irb) output better readable

Pour color on your Rails console with awesome_print. Turn confusing long strings into formatted output. Have objects and classes laid out clearly whenever you need it.

Put gem 'awesome_print', :group => :development into your Gemfile. Now on the Rails console you have the command ap that will give you a colored, formatted output of whatever you pass it. See the example output of the User class below.

For customization visit the repository on Github.

![awesome_print.png](https://makan...

Fix error: rails console - no such file to load -- readline

Install libreadline:

sudo apt-get install libreadline-dev

Reinstall the ruby and tell rvm where to find readline

rvm reinstall 1.8.7 --with-readline-dir=/usr/include/readline

Make sure you get a huge cup of tea before running the command above! It will take some time.

References

Find out your SSH key's fingerprint (e.g. to authenticate on GitHub)

If you want to know your public key's fingerprint, do this:

ssh-keygen -lf ~/.ssh/my.key.pub

This may be necessary to authenticate your key on GitHub because of recent events -- you need to do that if you get an error like this when talking to them (to pull etc):

ERROR: Hi foobear, it's GitHub. We're doing an SSH key audit.
Please visit https://github.com/settings/ssh/audit/...
to approve this key so we know it's safe.
Fingerprint: ab:cd:ef:...
fatal:...

Gherkin: Error during installation

When trying to install the gherkin gem, you might encounter an error with the following lines:

ERROR:  Error installing gherkin:
	ERROR: Failed to build gem native extension.
...
checking for main() in -lc... yes
creating Makefile
...
cc1: all warnings being treated as errors
Makefile:150: recipe for target 'gherkin_lexer_ar.o' failed
make: *** [gherkin_lexer_ar.o] Error 1
...

If upgrading is not an option, configure build options for gherkin:

bundle config --local build.gherkin --with-cflags=-w

Your .bundle/config fi...

Git: Remove information on branches that were deleted on origin

When branches get deleted on origin, your local repository won't take notice of that.

You'll still have your locally cached versions of those branches (which is actually good) but git branch -a will still list them as remote branches.

You can clean up that information locally like this:

git remote prune origin

Your local copies of deleted branches are not removed by this.

The same effect is achieved by using (kudos to Julien):

git fetch --prune

You could also set that as a default.

Markdown/Commonmarker examples

This card shows you how to format a card's content using Markdown. We use the Commonmarker interpreter, so here are examples for its dialect.

Formatting

**Bold**

Bold

_Italics_

Italics

`Monospaced`

Monospaced

> Quoted text

Quoted text

Here is [a link](http://makandra.com/).

Here is a link.

![An image; this is the alt text](http:/...

Git: Diff changes in a long line efficiently

Instead of showing you two lines for each change, Git allows you to highlight changes in a line explicitly:

git diff --word-diff some_file

Hello [-world-]{+universe+}!

(The result is usually colored nicely, the removed part being red and the added text green.)

When doing a diff on a long line, this can be very helpful but you'll still get a less-like scrolling output that can be unhandy to use.\
You maybe just want the diff put into your terminal:

PAGER='' git diff --word-diff some_file

WebMock 1.8.0 does not play nice with Curb < 0.7.16

When updating WebMock, be prepared that your specs may send real requests into the depths of the internet unless you update Curb as well.\
WebMock will not complain about those requests not being stubbed.

One of the commits that made it into 1.8.0 actually breaks Curb versions below 0.7.16 while fixing it for that version (and above, hopefully).\
WebMock's hooks for Curl::Easy are sti...

Gatekeeping: Guide for developer

If your project manager wants to do gatekeeping on a project, as a developer you need to follow the following guidelines (e.g. by using something like this issue checklist template).

In order to reduce the number of rejects we get from clients, we want to review all code written before it goes to the staging server.


Note: This process is tailored to our specific needs and tools at makandra. While it will certainly not apply to all (especially larger tea...

Gatekeeping: Guide for gatekeeper

If you're responsible for gatekeeping in a projects, here is a guide, what to do.
In order to reduce the number of rejects we get from clients, we want to review all code written before it goes to the staging server.

Note: This process is tailored to our specific needs and tools at makandra. While it will certainly not apply to all (especially larger teams), we think it is a helpful starting point.


First, read the [Gatekeeping for developers](https://makandracards.com/makandra/6579-gatekeeping-guide-for...

Sunspot and Solr on Tomcat: Trouble with Umlauts

We experienced problems with Sunspot and Solr on Tomcat: Umlauts (ä, ö, ü) were not correctly handled on Tomcat while everything was okay on the local development machines (your local Sunspot service you start with the sunspot:solr:run task is based on Jetty).

We use a stemmer that reduces "Sänger" to "sang" and "Sanger" to "sang" as well.
Though, results for "Sänger" where empty on Tomcat.

This is due to a UTF-8 bug in RSolr (see Github for some discussion on that).
The bug is fixed in a ...

New cards feature: Github-style code blocks

You can now add code blocks without indentation, by using triple-backticks:

```
Code block goes here.
```

Make Capistrano use SSH Key Forwarding

When deploying code with Capistrano (depending on your configuration) at some point Capistrano tries to check out code from your repository. In order to do so, Capistrano connects to your repository server from the application server you're deploying to with SSH. For this connection you can use two SSH keys:

  • the user's ~/.ssh/id_rsa [default]
  • the very same key you used for connecting to the application server - forwarded automatically to the git repository.

If you prefer the second way, add this to deploy.rb:

ssh_options[:forwar...

Git: Retrieve a file from a different branch or commit

To access files from another branch or past commit without doing a complete checkout, you can either use

git show branch:file
git show commit:file

to display, or check out the file into your working directory with

git checkout branch -- file
git checkout commit -- file

Look up a gem's version history

Sometimes it might be helpful to have a version history for a gem, e.g. when you want to see if there is a newer Rails 2 version of your currently used gem.

At first you should search your gem at RubyGems. Example: will_paginate version history.

The "Tags" tab at GitHub might be helpful as well.

How to fix a corrupt git index

If your git index for some reason becomes invalid, no need to worry.

Your index is corrupt when you see this error running usual git commands like git pull, git status, etc.:

error: bad index file sha1 signature
fatal: index file corrupt

Though it sounds bad, your changes are still there. Fix it by first removing the index file, then resetting the branch:

rm .git/index
git reset

You should be all good now.

To be safe, make a backup of .git/index before you delete it.