What’s Up With All These Changes in Rails?

Yesterday, there was a blog post entitled “What the Hell is Happening to Rails” that stayed at the number one spot on Hacker News for quite a while. The post and many (but not most) the comments on the post reflect deep-seated concern about the recent direction of Rails. Others have addressed the core question about change in the framework, but I’d like to address questions about specific changes that came up in the post and comments.

Unpack a .tar.gz archive

You can unpack a .tar.gz file into the current directory like this:

tar -xzvf archive.tar.gz

The options used are

-x

Extract

-z

Unzip

-f

Process a file input

-v

Be verbose, i.e. print each extracted file

Side notes:

  • The dash is optional (tar xzvf ... works as well).
  • For .tar.bz2 archives, use j instead of z (tar xjvf ...`)
  • In order to create a *.tar.gz archive, use -c flag instead of -x flag.
  • The order of parameters matters.

Order in which RSpec processes .rb files

Because your examples should not change global state, you should not need to care about the order in which RSpec processes your .rb files. However, in some cases you might want to know.

RSpec 3

  • Runs .rb files in alphabetical order of their file paths by default (or when you specify --order defined).
  • You run t...

Stubbed class methods in RSpec 1 remain stubbed in other examples

I encountered a bug in RSpec 1.x where stubbed class methods ("static methods") would not be unstubbed before the next example, causing it to fail. This behavior can come and go as you edit your specs, since this can change the order in which RSpec evaluates your .rb files.

I was not able to find a fix for this behavior. Calling #rspec_reset und #unstub!(:method) on the class after the example did not help. I know for sure that stubbing static methods has not been a problem in many other projects. I encountered the bug while working o...

New Cucumber Factory makes it easier to associate records

I pushed a new version of the Cucumber Factory gem. This new release lets you refer to a previously created record by any string attribute:

Given there is a movie with the title "Before Sunrise"
And there is a movie with the title "Limitless"
And there is a movie with the prequel "Before Sunrise"

Note how we didn't have to explicitly give the prequel a name in the example above. This is still possible, but will rarely be necessary now:

Given "Before Sunrise" is a movie with...

15 criteria for evaluating software product ideas

Choosing the right product to develop is crucial. Great execution is also very important. But if you develop a product that no-one wants or no-one is prepared to pay for, then you are going to fail, no matter how well you execute it. You can often tweak a product or its marketing to make it more successful based on market feedback (‘pivot’) . But the less pivoting you have to do, the better. Below I list some of the criteria I think are important for evaluating the potential of new commercial software products.

Example .ssh/config file

Attached you can find an example ~/.ssh/config file which makes working with SSH more pleasant. It contains several tweaks:

After you download the file to ~/.ssh/config, edit the file in a text editor to tailor it to your individual needs.

A nicer way to run RSpec and/or Cucumber

geordi, our collection of awesome shell scripts, has been extended by three scripts to help you call RSpec or Cucumber:

cuc

This script runs Cucumber the way you want it:

  • Prints some line feeds to easily find your test results when you come back to the console later
  • Configures Cucumber to use cucumber_spinner if it is available in your Gemfile
  • Runs Cucumber under bundle exec
  • Uses an old version of Firefox for Selenium (Javascript) features...

Don't forget to add ":dependent => :destroy" for join models

you should always

has_many :join_models, :dependent => :destroy

Dynamically skip Capistrano hooks

When you have a hook in your Capistrano file that dumps your remote database, you might not want it to dump each time you deploy (say, you're experimenting with staging and don't want ten dumps an hour). How to skip dump creation:

Capistrano 2

In your Capistrano file:

before 'deploy:update_code', 'db:dump' unless fetch(:skip_dump, false)

The second parameter of fetch sets a default value if skip_dump is not defined.

In your terminal:

cap staging deploy -S skip_dump=true

The -S must be a capital letter t...

Mark up text in Balsamiq Mockups

When you edit text in Balsamiq Mockups, you can define inline styles like this:

| Bold | Some *bold* text |
| Italics | Some _italic_ text |
| Blue underlined hyperlink | Some [linked] text |

Note that most Balsamiq widgets display with a bold font by default, so you won't usually see a difference when a piece of text is explicitly marked up as bold.

How to: Classic scrollbars in Ubuntu 11.04

Ubuntu natty introduced new "invisible" scrollbars for GTK programs such as gEdit or Nautilus.

If you do not like them:
sudo apt-get purge liboverlay-scrollbar-*

In order to have a tool recognize the changes you have to quit all its instances. Or just log out and back in.

Files open with an error "the location is not a folder" after installing XFCE

When using gnome-open (i.e. double-clicking a file, telling to browser to open a download, etc.) on Ubuntu you may get this error:

The location is not a folder.

It's due to the exo-utils package which does not play nice with gnome-open. You can fix the problem by uninstalling it:

sudo apt-get remove exo-utils

Note that this may also uninstall XFCE tools like xfce4-terminal or thunar (XFCE's directory browser). If you want to keep them, [try anot...

Commonly used regular expressions - Cheatsheet

For Email-Patterns see most recent E-Mail-Pattern.

HOST = /\A[a-z0-9]+[a-z0-9\-\.]*[a-z0-9]+\z/i
BANK_CODE = /\A\d{8}\z/                                          # Germany
BANK_ACCOUNT_NUMBER = /\A\d{1,10}\z/                             # Germany
ZIP_CODE = /\A\d{5}\z/                                           # Germany
HEX_COLOR = /\A([a-fA-F0-9]{3}){1,2}\z/
IP_ADDRESS = /\A(?:25[0-5]|(?:2[0-4]|1\d|[1-9])?\d)(?:\.(?:25[0-5]|(?:2[0-4]|1\d|[1-9])?\d)){3}\z/

We usually put these constants in...

Fixing: no sound on front audio in Ubuntu 11.04

My front audio output would not work even though the front mic input did fine.
The Pulse Audio mixer (= Ubuntu default) does not offer any complex controls to fix the issue. Instead, run gamix (be prepared for a GUI blast) and switch "Independent HP" to OFF. The label may be different for your audio device.

This enables all available audio outputs.

Printing to PDF files on Ubuntu

This will install a printer called "PDF":

sudo apt-get install cups-pdf

Files will be put into ~/PDF/ without any questions asked.

List directories ordered by size (with human-readable output)

You know there is the du command to fetch the disk usage of a directory (“.” in this example). By default, output is sorted by directory name, not size:

du -h --max-depth=1 .
40K  ./a
2.3G ./b
3.1M ./c
500M ./d
2.8G .

To see which directories take up the most (or least) space you can order them by size like this (the -h switch does the magic of understanding humanized size formats):

du -h --max-depth=1 . | sort -h
40K  ./a
3.1M ./c
500M ./d
2.3G ./b
2.8G .

Note that you will need to...

How to test resource_controller hooks

When using the resource_controller gem you often hook onto events like this:
update.before do
do_something
end

For testing such things in your controller you should -- as always -- not trigger something that eventually calls the thing you want.\
Instead, in your specs, have resource_controller run those hooks like it does itself. Like that:

describe 'before update' do
 ...

Check apache config files' syntax

To check your apache2 config files for syntax errors run

sudo apache2ctl configtest

or shorter

sudo apache2ctl -t

Be aware that this does not guarantee a successful Apache startup. It may still fail due to permission errors, etc.

SSL: Build a Certificate signing request (CSR)

In order to request a SSL certificate from any dealer, you usually need a CSR certificate. As both the CSR as well as key are created in this step, make sure you save this certificate on a trusted, secure machine only. Usually this is your production environment.

Run this on the server (not on your machine) as root.\
Replace your-domain.tld with the domain you request the certificate for and YYYY with the current year so you will not have any conflicts when requesting a certificate next year.

openssl req -new -sha256 -out www.your-dom...

Soft-scroll to an anchor with jQuery

This snippet makes links that refer to an anchor (like "<a href="#something">...</a>") scroll softly to it.\
In this example we only do it for links that also own a data-animate attribute.

$('a[href^="#"][data-animate]').live('click', function() {
  var hash = $(this).attr('href');
  var offset = $(hash).offset();
  if (offset) {
    $('html, body').animate({ scrollTop: offset.top }, 'slow');
    location.hash = hash;
    return false;
  }
});

Note that this could basically work for any element whos...

Semantic markup standard for search engines

If you would like to enrich your website with semantic markup like contact data, places or events you should have a look at schema.org. "Search engines including Bing, Google and Yahoo! rely on this markup to improve the display of search results, making it easier for people to find the right web pages."

The following example from the schema.org documentation shows you how to describe a movie with semantic markup:

<div itemscope itemtype ="http://schema.org/Movie">
  <h1 itemp...

Alternative to #to_a and Array(...)

Note: In Rails 3+ you can use Array.wrap instead of the solution here.

In the past you could use Array(...) or #to_a to turn an object into an array, unless it already is an array:

Array(5) # => [5]
Array([5]) # => [5]

Unfortunately this idiom has issues and it also triggers deprecation warnings like this one:

warning: default `to_a' will be obsolete

For an alternative you can copy the attached file to `config/i...

Open a new tab with the current directory on a mac

I found a nice script on crazylittlehacks and modified it slightly. Put the attachment to /usr/local/bin, chmod +x and run it like this:

tab

You may also pass a command that will be run in that newly opened tab:

tab ruby script/server

Note: This does not play too nice with Visor, because Visor will always be "window 1" for AppleScript. By modifying the script and replacing in window 1 with `in window...