validate all database records rails task

posted by alexis reigel on march 25, 2014

Sometimes you need to validate all your active record entries in the database. This may happen when you update validations in your models and you want to check which models need an update. Or you have a legacy system that has invalid records. Or some other system wrote to the database (without the rails application’s validation).

Here’s a rake task that validates all database rows for all the ActiveRecord models in your rails application.

namespace :db do
  namespace :data do
    desc 'Validates all records in the database'
    task :validate => :environment do
      original_log_level = ActiveRecord::Base.logger.level
      ActiveRecord::Base.logger.level = 1

      puts 'Validate database (this will take some time)...'

      Dir["#{Rails.root}/app/models/**/*.rb"].each { |f| require "#{ f }" }

        reject { |type| type.to_s.include? '::' }. # subclassed classes are not our own models
        each do |type|
            type.find_each do |record|
              unless record.valid?
                puts "#<#{ type } id: #{ }, errors: #{ record.errors.full_messages }>"
          rescue Exception => e
            puts "An exception occurred: #{ e.message }"

      ActiveRecord::Base.logger.level = original_log_level

And here’s the whole thing as a gist.

fuzzy file matching in vim

posted by alexis reigel on december 13, 2013

Until recently I used command-t as the mechanism for opening files. It was always working great for me, and apart from the installation, which is a bit of a hassle because it’s written in ruby, I was very happy with it. There was only one feature missing: Word boundary matching by using uppercase characters.

I was playing with the thought of contributing that feature to command-t, but then Gary Bernhardt came along and put selecta out there:

A fuzzy text selector for files and anything else you need to select.

He even provides a simple script that provides fuzzy file matching in vim.

So… let’s extend that really nice and simple script with my desired word boundary matching, I thought. And I did exactly that.

What my extension does is this:

Uppercase characters in the query must match a word boundary. Word boundaries are pascal and camel cased words and the characters ‘/’, ‘\’, ‘_’, ‘-’, ‘.’ and whitespace. (“FooB” will match “foo/bar” and “fooBar” but not “foobar”)

For the use case of fuzzy file matching in vim the change allows to search very quickly for specific paths. If we want to search e.g. for a rails model named foo, we could enter “AMFoo”, which would match the path “app/models/foo.rb”, but not “app/controllers/myfoos_controller”. If we searched just for “amfoo”, the latter would be matched as well.

linux and skype and misery and happiness

posted by alexis reigel on september 28, 2013

(Preamble: this guide applies to ubuntu)

Ok so there was a time when the appeal of Skype’s linux version lied in the lack of features and fancy ui. It was stable and my life was awesome.

Then new versions were released and new features were added. Some day Microsoft joined the party (not necessarily concluding that Skype’s change for the worse has something to do with them). Somewhere between then and now Skype became a real imposition for my life. Random crashes on chat, permanent crashes on calls. I gave up on it, using solely my android’s version for calls.

Once upon a time, which is now, I gave it another shot, crawled the internet for solutions and found the following:

$ sudo apt-get remove --purge skype
$ sudo add-apt-repository "deb $(lsb_release -sc) partner"
$ sudo apt-get install skype

Now the ugly task indicator is back and everything is stable again.

git bash prompt

posted by alexis reigel on july 05, 2013


I tried many different bash git prompt solutions, all of which were either too complicated, too buggy or too ugly. So…

I didn’t want to invent too much myself, so I used the git prompt provided by git as a basis. This script does all the magic already, so the only thing left to do was to make it look nicer. This is the whole section in my .bash_profile:

# bash git prompt

git_current_branch_name="\$(__git_ps1 '%s' | sed 's/ .\+//' | sed -e 's/[\\\\/&]/\\\\\\\\&/g')"
    "s/$git_current_branch_name//;" # remove branch temporarily
    "s/u//;" # upstream
    "s/+\([0-9]\+\)/▴\1/;" # outgoing
    "s/-\([0-9]\+\)/▾\1/;" # incoming
    "s/%/?/;" # untracked
    "s/+/✓/;" # staged
    "s/*/✕/;" # unstaged
    "s/\(.\+\)/($git_current_branch_name\1)/;" # insert branch again
git_status_command="\$(__git_ps1 '%s'| sed \"${git_status_substitutes[@]}\")"

if [ "$color_prompt" = yes ]; then
PS1="${debian_chroot:+($debian_chroot)}\[\033[0;37m\] \w \[\033[34m\]$git_status_command\[\033[37m\]\$\[\033[00m\] "
PS1="${debian_chroot:+($debian_chroot)} \w $git_status_command\$ "
unset git_status_substitutes git_status_command git_current_branch_name

And now, step by step:

git_current_branch_name="\$(__git_ps1 '%s' | sed 's/ .\+//;s/(//')"
    "s/$git_current_branch_name //;" # remove branch temporarily
    "s/u//;" # upstream
    "s/+\([0-9]\+\)/▴\1/;" # outgoing
    "s/-\([0-9]\+\)/▾\1/;" # incoming
    "s/%/?/;" # untracked
    "s/+/✓/;" # staged
    "s/*/✕/;" # unstaged
    "s/\(.\+\)/($git_current_branch_name \1)/;" # insert branch again
git_status_command="\$(__git_ps1 '%s'| sed \"${git_status_substitutes[@]}\")"
if [ "$color_prompt" = yes ]; then
    PS1="${debian_chroot:+($debian_chroot)}\[\033[0;37m\] \w \[\033[34m\]$git_status_command\[\033[37m\]\$\[\033[00m\] "
    PS1="${debian_chroot:+($debian_chroot)} \w $git_status_command\$ "

The final output looks like this:

~/src/dotfiles (master ✕✓? ▴1▾5)$

The original output of __git_ps1 looks like this, which is much more cryptic:

~/src/dotfiles (master *+% u+1-5)$

The meaning of the symbols:

Unstaged changes
Staged changes
? Untracked files
▴1 One changeset ahead of remote
▾5 Five changesets behind remote
= No difference to remote

You can see the whole thing in action in my .bash_profile.

string variable interpolation from file in ruby

posted by alexis reigel on december 17, 2012

Sometimes you want to interpolate your variables from a dynamic string, e.g. some settings from a yaml file. The top three options are:

I’ll show the third option here as:

Ruby has string formatting support and a very handy % operator. This enables us to replace certain parts of our config at runtime with the actual value, like this:

"foo = %{foo}" % { :foo => 'bar' }

Metaflop serves once again as a sample application. The following shows the perl command to generate some parts of our fonts (simplified):

font_otf: perl --family=%{fontface} --nofixedpitch --fullname="%{fontface} %{font_hash}"

Assuming we have a “config” variable that holds all the values defined in yaml we can do:

config.font_otf % {
  :fontface => 'Bespoke',
  :font_hash => '234sdof23nsf'}

# => perl --family=Bespoke --nofixedpitch --fullname="Bespoke 234sdof23nsf"

The operator accepts literals and arrays as arguments as well, but I like the named parameters style best. This way, there is an obvious connection between the placeholder and its value.

how to generate a webfont kit with open source tools

posted by alexis reigel on august 06, 2012

There are some web based services that convert fonts to a webfont package, the most popular of which is the fontsquirrel font-face generator. For our metaflop project I was looking for a service api that i could call from within our application. Since i didn’t find one I was attempting to build the whole webfont generation by collecting tools that could do the job.

Font types

To create a webfont kit that works on all browsers we need to generate several different font types:

Tool chain

The following tools are required to generate all the needed font types. I assume that you have your font as otf. If you have a ttf, just switch ttf with otf in the following statements:

The very simple FontForge script ( looks like this:
Generate($1:r + ".ttf")
Generate($1:r + ".svg")
# outputs font.ttf, font.svg
$ fontforge -script font.otf
# outputs font.woff
$ sfnt2woff font.otf
$ ttf2eot font.ttf > font.eot


The css declaration is based on Fontspring’s bulletproof @font-face syntax and is the same syntax as used by fontsquirrel.

@font-face {
    font-family: 'YourFont';
    src: url('YourFont.eot'); /* IE 9 Compatibility Mode */
    src: url('YourFont.eot?#iefix') format('embedded-opentype'), /* IE < 9 */
         url('YourFont.woff') format('woff'), /* Firefox >= 3.6, any other modern browser */
         url('YourFont.ttf') format('truetype'), /* Safari, Android, iOS */
         url('YourFont.svg#YourFont') format('svg'); /* Chrome < 4, Legacy iOS */

That’s it. Remember though that you should only convert fonts are legally eligible for web embedding.

reminder. git. awesome.

posted by alexis reigel on july 19, 2012

This is just a quick reminder of how awesome git actually is. The story is short:

My disk crashed and I lost a couple of local commits that I hadn’t pushed (to github) yet. Luckily, I deployed them to the staging server already (actually, this is a reminder of how awesome git and capistrano actually are). Because capistrano is awesome and deploys the git repo to the server, and git is awesome too, I could restore my local repo from the staging server:

local:  $ ssh staging_server
server: $ # some `cd` action
server: $ tar czf asdf.tgz www_folder
server: $ # ctrl + D
local:  $ cd /tmp
local:  $ scp user@staging_server:/tmp/asdf.tgz .
local:  $ tar xf asdf.tgz
local:  $ cd my_local_repo
local:  $ git pull /tmp/asdf dev

A simple git pull from the copied repo and I’m all set again.

How awesome is your dev stack?

sinatra with bourbon

posted by alexis reigel on march 24, 2012

If you search the web for “bourbon sinatra” you get a whole lot about Frank Sinatra and his drinking habits. But that’s not what this post is about.

How to use bourbon scss mixins with sinatra is not documented on their project page. So here’s my short setup.

The following is standard, from their documentation.

$ gem install bourbon # or better, add to use Gemfile and use bundler
$ cd views/scss
$ bourbon install

Next, import bourbon in your main scss file (as first statement).

@import 'bourbon/bourbon';

The following is the important and the undocumented part, specifically for sinatra. You need to require the ruby libraries in your scss route.

get '/assets/css/:name.scss' do |name|
  require './views/scss/bourbon/lib/bourbon.rb'
  content_type :css 
  scss name.to_sym, :layout => false

static vs dynamic vs strong vs weak vs duck typing

posted by alexis reigel on march 19, 2012

Here’s my take at clearing up the confusion about the most common type systems used in programming.

I often hear how static and strong typing are used as synonyms. These two are however quite different systems. The same applies to dynamic and weak typing. For instance, a programming language can be both dynamically and strongly typed, but not dynamically and statically. Ruby is an example of a dynamically and strongly typed language.
Ok, I guess now it’s the time to elaborate.

static vs dynamic

  // example in c#

  string s = "asdf";
  # example in ruby

  s = "asdf"

strong vs weak

  # example in ruby

  "1" + 1 # TypeError
  // example in javascript

  "1" + 1 // "11"

duck typing

Duck typing is a style of dynamic typing.With duck typing, the set of methods and properties determine the valid semantics of a class, instead of inheritance from a specific class or implementation of an interface.
“When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.”
This means that we actually don’t care if the class we’re using is actually of a certain type, as long as it provides the methods or properties we’re interested in.
The term duck typing comes from python, but ruby is also very known for this behaviour.

sinatra on capistrano

posted by alexis reigel on january 31, 2012

Capistrano is really great for deployment. The documentation however is somewhat scattered. The capistrano wiki has a more or less useful From The Beginning instruction and a Getting Started that doesn’t quite get you started.
However, the instructions on github are actually a very good starting point. Neverthelesse I want to show my sample here that has two additions to the one in the github help.

  1. Multistage support (i.e. staging and production server)
  2. Custom task to upload a config file

For further reading I recommend the Capistrano Handbook.

I assume that you’ve setup capistrano as described in the github help. The one thing I suggest is to ignore the part where the password is stored in the file. I highly advise to use ssh agent forwarding instead.

ssh_options[:forward_agent] = true # use local ssh key
set :user, "deployer"

Let’s setup multistage:

require 'capistrano/ext/multistage'
set :stages, %w(production staging)
set :default_stage, "staging"

Also, on most deployment server you may not have rights to sudo:

set :use_sudo, false

As I’m assuming you’re using passenger mod_rails:

namespace :deploy do
  task :start do ; end
  task :stop do ; end
  task :restart, :roles => :app, :except => { :no_release => true } do
    run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"

The last part uploads our configuration file to the server. This configuration contains user and password for the database login. We don’t store that in the source code repository.

namespace :config do
  task :db do
    upload('db.yml', "#{deploy_to}/current/db.yml")

before "deploy:restart", "config:db"

The whole capistrano configuration file is on github.

swimming in zurich: "züribad"

posted by alexis reigel on april 25, 2011

My first android application is a utility app about all public swimming baths in Zurich. It shows the current water temperature for each bath and additional information is available too, such as address and routing info (how to get there by public transportation). Google map integration provides a geographical overview of the baths.

The app is freely available in the android market. The source code is hosted on Bitbucket.

share on minus from windows explorer context menu

posted by alexis reigel on february 19, 2011

There’s a new tool for the file sharing service that makes it really easy to share files. The tool provides a context menu entry in the windows explorer and a simple user interface to upload the files and provide you with the share url.

The tool is on github.

windows console: spawn new process

posted by alexis reigel on january 19, 2011

Under linux spawning a new process is as easy as appending an & to the command. In windows it’s not (obviously). I pretty quickly found out about the start command. Not as elegant as the linux version, but still pretty intuitive. Or, wait, not? It took me half a decade to find out how to use it properly.

First i had a look at the help output, which can be invoked by the switch /?. I’m not 100% sure, but i have in mind that this switch isn’t really standardized, i think I also had to use /h or /help in the past. Anyway, that’s not the sad part yet.

console start

Reading through that, all you have to do is call start my_command. Easy huh? No. It’s not.

After digging around this article lead me to the solution: “title: Text for the CMD window title bar (required)”. Title! Required!
Let me say that again. Title! Required!
First thought: Why? Why is a title required? A title is clearly something optional, especially when you’re starting a GUI application.
Second thought: Why? Why is that “required” not stated as such in the help output? Not the slightest hint in the help output. And furthermore, isn’t it a convention that required parameters are NOT surrounded by brackets? Don’t brackets mean “optional”? Apparently, in the windows tooling environment all this does not apply.

I know, windows is a GUI centered OS, and therefore the console didn’t evolve as nicely as it did in other OS’s. Nevertheless, every time I have to work on a console or write a batch file it gives me the creeps. Everything is so unbelievably clumsy. At least I found out about the Console project, which is a pretty decent replacement for the windows console. Copy/paste works as desired with keyboard shortcuts and you can resize the window. Resize the window! How cool is that! But that’s a different story…

DataGridView with an object data source

posted by alexis reigel on september 04, 2010

For my Batch Replacer I am using a DataGridView with an object data source, meaning that I have a collection class that holds the data to be displayed. At first that class inherited from a usual List<T>, which led to the problem that the data binding wasn’t two-way. As soon as I bound data to the DataGridView, it wasn’t possible to add any new rows to it in my user interface. After some research I found out that instead of using a normal List&lt;T&gt; I was supposed to use a BindingList<T>. Using that the binding is two-way, and the user interface is normally editable and new rows can be added. Also, of course, all modifications done in the user interface are reflected in the object (meaning e.g. that when a new row is added, the object data source contains a corresponding entry).

replace as in batch replace

posted by alexis reigel on september 03, 2010

So I once again needed to replace several patterns in multiple files with something new… Windows lacks a nice terminal with all the pipeable tools. Instead of doing this all in my favorite text editor by using repeatedly find/replace, I decided that something more handy was needed. Of course I could have done all that replace stuff with xslt too, for even more awesomeness (you can’t see my face, that what sarcasm).

The result of my disapproval of the above mentioned options is the Batch Replacer. As always it is released under the gpl. I also used git:" for the first time and published the source code on github".