How to detect touch-capable browsers

The easiest way to detect touch-capable browsers is to check for the presence of touch events. It is no 100% solution, but has by far the best cost-benefit ratio. (Know that this does not detect touch devices, but browsers.)

Javascript

var isTouchDevice = 'ontouchstart' in window

Coffeescript

isTouchDevice = 'ontouchstart' of window

On the difference between the Javascript and the Coffeescript version, see [Beware: Coffeescript "in" is not the Javascript "in"](https://makandracards.com/makandra/31073-beware-c...

Block formatting contexts

TL;DR Block formatting contexts establish an isolating container. float
and clear only apply to elements within such a container.

About

Block formatting contexts (BFCs) are important for the positioning and clearing
of floats. The rules for positioning and clearing of floats apply only to
things within the same block formatting context.

Floats do not affect the layout of things in other block formatting contexts,
and clear only clears past floats in the same block formatting context.

How to create a new block form...

LibreOffice Writer: Prevent a table row from being split across pages

  • Right-click on the table
  • Select Table...
  • Select Text flow
  • Uncheck Allow row to break across pages and columns

Ruby: Find a hash key given it's value

To find a hash key by it's value, i.e. reverse lookup, one can use Hash#key. It's available in Ruby 1.9+.

Hash#key(value) → key
# => Returns the key of the first occurrence of a given value.
     If the value is not found, returns nil.

hash = { "a" => 100, "b" => 200, "c" => 300, "d" => 300 }
hash.key(200)   #=> "b"
hash.key(300)   #=> "c"
hash.key(999)   #=> nil

Git: How to get a useful diff when renaming files

tldr; Use git diff -M or git diff --find-renames when you've moved a few files around.

Usage

$ git diff --help
  Options:
    -M[<n>], --find-renames[=<n>]
      Detect renames. If n is specified, it is a threshold on the similarity index
       (i.e. amount of addition/deletions compared to the file’s size). For example,
       -M90% means Git should consider a delete/add pair to be a rename if more than
       90% of the file hasn’t changed. Without a % sign, the number is to be read as
       a fraction, with a decimal point...

Angular + ui-router: Make links work in a new tab

If your angular app is not served on /, but on a different url (say /admin), links generated with ui-router will not work when you open them in a new tab.

Fix this by adding this tag in your <head>:

<base href='/admin#/'>

Helper method:

def base_tag
  tag(:base, href: request.path_info + "#/")
end

greckout - a bash script to grep and checkout your git branches

greckout query

This will list all branches matching your query as input options for git checkout

  greckout ar
  
  1) ar/cache-api-keys-1098
  2) ar/add-categories-object-to-tv-show-1382
  3) ...

How to load an SQL dump from a migration

If you want to load an SQL dump from an ActiveRecord migration, you might find this to be harder than you thought. While you can call ActiveRecord::Base.connection.execute(sql) to execute arbitrary SQL commands, the MySQL connection is configured to only accept a single statement per query. If you try to feed it multiple statements, it will die with You have an error in your SQL syntax.

You can work around this by opening a second MySQL connection that does accept multiple statements per call.

Below is an example for a migration that l...

Debugging "INTERNAL ERROR!!! wrong argument type StringIO (expected File)"

If you're getting this strange error message when setting debugging breakpoints, probably HAML is the culprit.

Cause

As far as I could find out, the error is the result of setting a breakpoint (debugger) in a helper method that's called from a haml partial.

Suggestions

Try putting the breakpoint into the HAML view.

Cucumber factory 1.10.0 released

I've pushed an update to Cucumber factory that simplifies working with FactoryGirl factories.

Say you define a factory with the class: option:

factory :admin, class: User
  email
  admin true
end

In the past, you had to write

Given there is a user (admin)

Now you can simply write

Given there is an admin

The class is inferred from the factory.

Capybara - The missing API

The Capybara API is somewhat hard for parse for a list of methods you can call on a Capybara node. Below you can find such a list. It's all copied from the Capybara docs, so all credit goes to the Capybara committers.

When you talk to Capybara from a Cucumber step definition, you always have page as the document root node, or whatever you scoped to by saying within(selector) { ... }. You can select child notes by calling page.find(selector) or page.all(selector). You can call the same ...

Ubtuntu: "FATAL: Could not load /lib/modules/...-generic/modules.dep: No such file or directory"

If you get this error (probably because you want to load some modules):

# modprobe xt_comment
FATAL: Could not load /lib/modules/3.2.0-40-generic/modules.dep: No such file or directory

The reason could be that apt-get autoremove already removed /lib/modules/.../modules.dep even if you still using this kernel version:

# uname -r
3.2.0-40-generic
# ls -l /lib/modules/
total 24
drwxr-xr-x 4 root root 4096 Oct 22 17:05 3.2.0-68-generic
drwxr-xr-x 4 root root 4096 Jan  7 16:38 3.2.0-69-generic
drwxr-...

One cause for "iptables: No chain/target/match by that name" on Ubuntu

I couldn't successfully execute a simple iptables command and got this error on an Ubuntu server:

# /sbin/iptables -I INPUT ....
iptables: No chain/target/match by that name

The cause was that two modules weren't loaded: xt_multiport and xt_comment. Normally Ubuntu loads them automatically, but not this time due to another problem.

How to set up database_cleaner for Rails with Cucumber and RSpec

Add gem 'database_cleaner' to your Gemfile. Then:

Cucumber & Rails 3+

# features/support/database_cleaner.rb

DatabaseCleaner.clean_with(:deletion) # clean once, now
DatabaseCleaner.strategy = :transaction
Cucumber::Rails::Database.javascript_strategy = :deletion

Cucumber & Rails 2

The latest available cucumber-rails for Rails 2 automatically uses database_cleaner when cucumber/rails/active_record is required -- but only if transactional fixtures are off. To have database_cleaner work correctly:

  1. Add the at...

Github: How to find the Readme for a certain version of a gem

When a gem author releases a new version to Rubygems, usually a tag with the version number (e.g. v1.2.0) is created an pushed to Github, so everyone can check out or take a look at the source code at this point version release at a later time.

If you'd like to take a look at the Readme of a specific Gem version, you can easily switch to that git tag on Github.

Github: Checkout Git Tag

Unobtrusive jQuery to toggle visibility with selects and checkboxes

Use this if you want to show or hide part of a form if certain options are selected or boxes are checked.

The triggering input gets an data-selects-visibility attribute with a selector for the elements to show or hide, like

<%= form.select :advancedness, [['basic', 'basic'], ['advanced', 'advanced'], ['very advanced', 'very_advanced]], {}, :"data-selects-visibility" => ".sub_form" %>

The elements that are shown/hidden look like

<div class="sub_form" data-show-for="basic"> 
  only shown for advancedness = basic 
</div>

...