How to: Build a "generic observer" widget for the awesome window manager

If you want a widget for awesome that runs a command regularly (every X seconds) and puts the output into your awesome panel, this is for you.

Put the code below into your ~/.config/awesome/rc.lua. It supplies two methods:

  • execute_command will run a command and return its output. Multiple lines will be joined into one line. The code is from the awesome wiki.
  • widget_with_timeout is a method that takes a command to run and a timeout in secon…

Duplicate a git repository with all branches and tags

In order to clone a git repository including all branches and tags you need to use two parameters when cloning the old and pushing to the new repository respectively:

git clone --bare
cd old-repo
git push --mirror

Of course, the URLs to your repository might look different depending on the protocol used, username required, etc.
For a user git using the git protocol, it could be

Howto: Create a self-signed certificate

As igalic commented on this gist.

openssl req -new -newkey rsa:2048 -sha1 -days 365 -nodes -x509 -keyout server.key -out server.crt


req -new
Create a new request …
… using a new key …
… of type RSA, 2048 bits long.
Make sure to use SHA1 as this certificate's hashing algorithm,
don't encrypt the key and
make it an X.509 certificate, not a Certificate Signing Request.

Mac OS: Remove app from Launchpad

Issue this command:

sqlite3 ~/Library/Application\ Support/Dock/*.db "DELETE from apps WHERE title='APP_NAME';" && killall Dock

This tells sqlite3 to remove the app called APP_NAME from the Launchpad database and then kill the process called Dock, thereby restarting it.


Good real world example for form models / presenters in Rails

We have often felt the pain where our models need to serve too many masters. E.g. we are adding a lot of logic and callbacks for a particular form screen, but then the model becomes a pain in tests, where all those callbacks just get in the way. Or we have different forms for the same model but they need to behave very differently (e.g. admin user form vs. public sign up form).

There are many approaches that promise help. They have many names: DCI, presenters, exhibits, form models, view models, etc.

Unfortunately most of these approaches …

LibreOffice Calc: Reset positions of split dividers

To reset the position of split dividers, drag all the dividers out of the screen. Then turn off splitting and turn it on again.

How to find out which type of Spec you are

When you need to find out in which kind of spec you are during run-time, it's definitely possible. It's a lot easier in RSpec2.

For example, consider this global before block where you'd want to run some code for specific specs only:

config.before do
  # stuff
  # more stuff

RSpec 2

If you want to run such a block for a specific type of specs, you can use filters:

config.before do
  # stuff
  # more stuff  ...

Quick git contributors list

git shortlog -s -n
-n, –numbered
Sort output according to the number of commits per author
-s, –summary
Suppress commit descriptions, only provide commit count

Example output for spreewald:

60  Tobias Kraze
12  Henning Koch
 7  Dominik Schöler
 6  Thomas Eisenbarth
 5  Martin Straub
 3  Minh Hemmer
 2  Alex McHale
 1  Manuel Kallenbach
 1  Andreas Robecke

Use the same translations for ActiveRecord and ActiveModel

ActiveRecord translations live in locale.activerecord. ActiveModel translations live in locale.activemodel. To use the same translations for both, use YAML references with & and *:


models: &models
user: 'Benutzer'

attributes: &attributes
email: 'E-Mail'

errors: &errors
format: ! '%{attribute} %{message}'
accepted: muss akzeptiert werden
blank: muss ausgefüllt werden
# …

errors: *errors
models: *models
attributes: *attr…

AngularJS: Access the scope for a rendered DOM element

This trick might be useful to implement more complicated directives in AngularJS. I needed it to do drag'n'drop in a hierarchical tree.

Let's say you have this $scope in your Angular controller:

$scope.tasks = [
  { 'text': 'Task 1' },
  { 'text': 'Task 2' }

And you have this template:

<ul ng-repeat="task in tasks">

Which renders this HTML:

  <li>Task 1</li>
  <li>Task 2</li>

If you'd like to access the scope bound to the second <li> you can say this in jQ…

Git: How to show only filenames for a diff

When you want to do a git diff but do not care about the full diff and just want to know which files changed, use the --name-only switch:

$ git diff --name-only

To include some brief information about changed lines, use the --stat switch:

$ git diff --stat
app/controllers/sessions_controller.rb |    8 +-
app/models/user.rb                     |   30 ++++
features/sign_in.feature               |  136 +++++++++++++++++


Inherit without Single-Table-Inheritance (STI) in ActiveRecord

You have multiple options:

  1. Just don't have a type column. All STI magic will be disabled automatically.
  2. If you have a type column but want to use it for something else (WAT?), you can set self.inheritance_column = :_non_existing_column in the class definition
  3. (Untested) In the parent class, set self.abstract_class = true

This technique is useful to implement form models / presenters, where you want all the goodness of Ac…

How to find out if you are in Cucumber or in RSpec

Sometimes you need a piece of code to do something different for specs than for features. If you don't have separate environments, you can't check your Rails.env.

I managed to distinguish between specs and features by asking Capybara.

Note that this only works when you do not use Capybara in specs.

if defined?(Capybara) and Capybara.respond_to?(:current_driver)
  # you're in a Cucumber scenario
  # you're probably in a spec

You could omit the defined?(Capybara) condition, if you are sure that Capybara

Using Thin for development (with SSL)

Note: These instructions are for a quick per-project setup and may require you to change code. If you generally need SSL for development, you probably want to use Passenger.

  1. Create a directory .ssl in your home directory. Go there and create a self-signed certificate. It is important to enter localhost.ssl as Common Name when asked. This is to mak…

JavaScript: How to generate a regular expression from a string

Getting a regular expression from a string in JavaScript is quite simple:

new RegExp('Hello Universe');
# => /Hello Universe/

You can also use special characters:

new RegExp('^(\\d+) users')
# => /^(\d+) users/

Our expression above now works only at the beginning of the matched string, looks for a number (\d+ [1]) and also captures that. Sweet.

However, mind that your input will not be magically escaped because of that:

new RegExp('')
# => /

The above expression would match "`…

How to not die with ActionView::MissingTemplate when clients request weird formats

When HTTP clients make an request they can define which response formats they can process. They do it by adding a header to the HTTP request like this:

Accept: application/json

This means the client will only understand JSON responses.

When a Rails action is done, it will try to render a template for a format that the client understand. This means when all you are HTML templates, a request that only accepts application/json will raise an error:

An ActionView::MissingTemplate occurred in pages#foo:
  Missing templa...

Deal with different ways of counting weeks and weekdays in Ruby

Depending on where you live, different rules are used to determine the number of the week and a weekday. You have no chance whatsoever to get this right globally unless you make it your life's purpose. However, when you work for clients from Europe or the US, there are two dominantish standards you should know about. Each of these has subtle differences.

ISO 8601

  • This is adhered to by most European countries.
  • Weeks start on Mon…
3474 cards