Run Chrome in a specific resolution or user agent with Selenium

When you want to test how an web-application reacts in a specific resolution, you can set up a specific Selenium driver for some tests:

 Before('@chrome320x480') do
     Capybara.current_driver = :chrome320x480
 end

 After('@chrome320x480') do
    Capybara.use_default_driver 
 end

You can use either chromium or chrome beta (as of 2012.05 the Version "19.0.1084.41 beta" works), or any other member of the family. It only needs to supports the "--window-size" command-line switch. [See this list](http://peter.sh...

Manipulating the browser history - MDN

The DOM window object provides access to the browser's history through the history object. It exposes useful methods and properties that let you move back and forth through the user's history, as well as -- starting with HTML5 -- manipulate the contents of the history stack.

Fix error when embedding Bing maps

If you get this:

p_elSource.attachEvent is not a function

... you need to disable Firebug or switch to another browser.

Open Helvetica alternatives

The closest is probably Nimbus Sans L, which is released under the GPL, AFPL, LPPL licenses. However, I couldn't find a way to convert Nimbus Sans L into a web font.

I finally settled with Liberation Sans, which is awesome for some reasons:

  • Although it's available for free, it's a high quality font because its creation was thankfully sponsored ...

Convert a TrueType (.ttf) font to web font formats

  • Note that you are not allowed to embed any font in a website. You need to check the license first. Fonts from Font Squirrel are all okay to embed and use for commercial purposes, but as with many free fonts, quality differs widely, especially at medium and small font sizes.
  • You will need to embed your font in several formats since browser support is a mess.
  • You can use the [Font Squirrel @font-face generator](http://www.fontsquirrel.c...

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.

Creating a multi-resolution favicon including transparency with the GIMP

The result is a .ico file with multiple versions of your logo at different resolutions. Now, depending on the context, visitors to your site will see your nice favicon in their browser tabs, superimposed on whatever browser chrome they're using in all of its transparent glory.

How to access a dmcrypt-encrypted partition from outside your system

This is for you when you want to mount a dmcrypt encrypted partition manually, e.g. from a live CD.

First, open the dmcrypted partition (You need to provide some name. It may, but does not need to, be your LVM group name):

cryptsetup luksOpen /dev/sda5 some_name

Since your encryption container most likely contains an LVM group (root + swap for example), enable the logical volume manager (replace LVM_NAME with your volume group's name):

vgchange -ay LVM_NAME

After that, you can your access (mount, [fsck](https://makandrac...

How to check your file system's integrity with fsck

To force a check on your next reboot (here for your root partition), simply:

touch /forcefsck

To manually do this, here is how to do it for the first partition on sda, when you are using ext4:

fsck.ext4 -fn /dev/sda1
  • -f forces the check, even if everything seems okay.
  • -n is a "read only" check, where nothing is changed and every question is answered with "no".
  • When using dmcrypt, you probably want to check s...

Browser support for box-shadow

Basic box shadow support is available in all browsers today, but you need to check to which extend they are supported. Implementations differ:

  • Are multiple box shadows (separated by comma) possible?
  • Are inset shadows possible?
  • Can the spread radius be defined?
  • Can you use it without vendor prefix?

A good compatibility chart can be found here. The gist is that, except for IE and some Safaris, you can use all features ...

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 (...

Scroll a textarea to a given line with jQuery

You can use this code:

function scrollToLine($textarea, lineNumber) {
  var lineHeight = parseInt($textarea.css('line-height'));
  $textarea.scrollTop(lineNumber * lineHeight);      
}

Some caveats about this code:

  1. Your textarea needs to have a line-height in Pixels.
  2. The code will scroll to the line number in the text area, not the line number of the original text. These will differ if any lines wrap at the edge of the textarea.

Also see our solution for [scrolling a textarea to a given position with jQuery](htt...

Convert primitive Ruby structures into Javascript

Controller responses often include Javascript code that contains values from Ruby variables. E.g. you want to call a Javascript function foo(...) with the argument stored in the Ruby variable @foo. You can do this by using ERB tags (<%= ruby_expression %>) or, in Haml, interpolation syntax (#{ruby_expression}).

In any case you will take care of proper quoting and escaping of quotes, line feeds, etc. A convenient way to do this is to use Object#json, which is defined for Ruby strings, numb...

Git: Change the text editor used to enter commit messages

In your ~/.gitconfig:

[core]
  editor=nano

Uncaught exception in PassengerServer client thread exception: Cannot accept new connection: Too many open files

So you probably see the following error trace within your Passenger log file if you got here:

[ pid=123 thr=1401414 file=ext/nginx/HelperAgent.cpp:964 time=2012-04-27 10:01:49.273 ]: Uncaught exception in PassengerServer client thread:
   exception: Cannot accept new connection: Too many open files (24)
   backtrace:
     in 'Passenger::FileDescriptor Client::acceptConnection()' (HelperAgent.cpp:429)
     in 'void Client::threadMain()' (HelperAgent.cpp:953)

You can either

  1. Lower the maximum number of running Passenge...

zClip :: jQuery ZeroClipboard

zClip is a lightweight jQuery "copy to clipboard" plugin built using the popular Zero Clipboard library. This plugin uses an invisible Adobe Flash movie that is fully compatible with Flash Player 10 and below.

fnando/i18n-js

A small library to provide the Rails I18n translations in Javascript clients.

CodeMirror

CodeMirror is a JavaScript component that provides a code editor in the browser. When a mode is available for the language you are coding in, it will color your code, and optionally help with indentation.

xterm: Have a black background by default

xterm by default uses black text on white background.

To change that to something like "light gray on black", do a

vim ~/.Xresources

...and put this in there:

xterm*background: black
xterm*foreground: lightgray

Afterwards, feed these changes to your current X session:

xrdb ~/.Xresources

Any subsequent xterm will be colored white on black background.

Hint:

  • While you're at it, you can also set xterm's font in your ~/.Xresources file. Just add xterm*faceName: monospace:pixelsize=14.
  • If you don't want to chan...

How to fix Passenger "Unexpected end-of-file detected" error

This is for you if Passenger gives you the following useless error message.

Passenger encountered the following error:\
The application spawner server exited unexpectedly: Unexpected end-of-file detected.

Exception class:
PhusionPassenger::Rack::ApplicationSpawner::Error

Most often this happens because you are missing a gem. Usually Passenger would tell you about that but in some cases it can't.

To resolve this issue, run:

bundle install

If this does not do the trick for you, take a look at the Apache log files for de...

Validate attachment presence using paperclip

Make sure you call the methods in the following order and not vice versa:

has_attached_file :image
validates_attachment_presence :image

Validation with condition works fine, too:

validates_attachment_presence :image, :if => :method

This is because validates_attachment_presence is only available after saying has_attached_file.

LibreOffice Calc: How to paste cells without overwriting other cells

The default behavior of Calc is that when you paste cells from the clipboard, it overwrites whatever cells occupy the same space on the spreadsheet. If you would like to insert the cells in the clipboard instead by pushing the existing cells to the bottom or the right, you can do it like this:

  • Go to Edit / Paste Special
  • The paste dialog has a segment Shift Cells. Choose Down or Right depending on what you want to do.

Building Gem 'RedCloth' with Bundler and GCC 4.6

If you cannot install the gem 'RedCloth' via bundle install you might want to try the following.

Specifying extra build options for bundler which will only be applied when building RedCloth:

bundle config build.RedCloth --with-cflags=-w