Ubuntu: Fix "An error occurred while installing pg"

If you get an error like this:

An error occurred while installing pg (0.17.1), and Bundler cannot continue.
Make sure that gem install pg -v '0.17.1' succeeds before bundling.

Then do this:

sudo apt-get install libpq-dev

... and run Bundler again.

Openstack: Create a Flavor

List flavors to show the ID and name, the amount of memory, the amount of disk space for the root partition and for the ephemeral partition, the swap, and the number of virtual CPUs for each flavor.

$ nova flavor-list

To create a flavor, specify a name, ID, RAM size, disk size, and the number of VCPUs for the flavor, as follows:

$ nova flavor-create FLAVOR_NAME FLAVOR_ID RAM_IN_MB ROOT_DISK_IN_GB NUMBER_OF_VCPUS

The ID of the new flavor should always be one higher than the highest.

Rails routes: Extracting collection actions into their own controllers

Let's say you have two screens:

  1. Show a given project
  2. Show a report for all projects

Ideally you want both screens to be handled by different controllers like this:

GET /projects/:id        => ProjectsController#show
GET /projects/report     => Projects::ReportsController#show

What seems like a simple requirement is a little awkward to configure in your routes.
Obviously the report should be a singleton resource, but how can we nest it into the Projects:: namespace?

What does not work is this:

resources :proj...

Rspec: Complex argument expectations for should_receive

Sometimes you need complex expectations on method arguments like this

SomeApi.should_receive(:find).with(:query => '*foo*', :sort => 'timestamp ASC', :limit => 100).and_return(['some result'])

This is not very flexible, and failure messages will be hard to read.

Instead, consider doing this:

SomeApi.should_receive(:find) do |params|
  params[:query].should == '*foo*'
  params[:sort].should == 'timestamp ASC'
  params[:limit].should == 100
  
  ['some result']
end

Testing drag&drop with Selenium

When using jQueryUI's Sortable plugin (either directly or via Angular's ui.sortable), you might struggle testing your nice drag&drop GUI since Selenium webdriver does not support native dragging events.

But jQueryUI uses jquery.simulate for their testing, so why shouldn't you? There is even an extension to it that makes testing drag & drop quite easy.

Here is what you need:

  1. jquery.simulate.js
  2. [`jquery.simula...

Linux: Get your public IP address from the shell

curl http://ipecho.net/plain; echo

Rails 3: Mass assignment protection and .create_with

The issue

Yesterday, Rails fixed a security issue (CVE-2014-3514) in Rails 4+. It was possible to use .where or .create_with to bypass Rails' Strong Parameters:

user.blog_posts.create_with(params[:blog_post]).create

would set all attributes on the blog post. After the fix, you have to properly whitelist the params, via `params[:blog_post].permit(:title, :bo...

jQuery: Work with text nodes and comment nodes

Nearly all jQuery traversal functions ignore elements that are not HTML tags.

To work with other type of nodes (like text, comment or CDATA sections) you need to:

  • Retrieve child nodes contents() (which behaves like children() except that it returns all types of child nodes)
  • Filter manually using either plain Javascript or jQuery's filter() method

Example

Let's write a function that takes a jQuery element and returns an array of all child nodes that are text nodes:

function selectTextNodes($container) {
  retu...

Vastly increase the usability of Thunderbird's search

These two addons will change your life:

Search as list
: This will always open search results in the list views instead of the barely usabely faceted search view.

Sort search results by date not relevance
: Does what it says.

Mind blown

Interacting with a Microsoft Exchange server from Ruby

Microsoft Exchange service administrators can enable Exchange Web Services (EWS) which is a rather accessible XML API for interacting with Exchange. This allows you to read and send e-mails, create appointments, invite meeting attendees, track responses, manage to-do tasks, check user availability and all other sorts of things that are usually only accessible from Outlook.

You can implement an EWS by hand-rolling your XML (the [docs](http://msdn.microsoft.com/en-us/...