#![desc = "Random thoughts of a software engineer"]

Sharing controllers and views with polymorphic resources

A while ago I faced a problem with controllers for nested resources I wanted to reuse in different contexts (polymorphic resources). For example, I had a simple Todo model that is used to store tasks that have to be done. Having multiple users which are organized in teams, the goal was to have user-specific (personal) todos and team-specific todos. Obviously, both kind of todos could be handled exactly the same way (same controller, same views) -- except that the context differs.

So here's a way to use a single controller for a polymorphic resource.


Five tips for developing Rails applications

During the development of some web applications in the past, I found myself using various techniques again and again on different projects. Here's a collection of five things that I discovered over the time and that I found most useful.

  • #1 -- Pimp your rails console with colors, history and tab completion
  • #2 -- Using source annotations
  • #3 -- Using enum attributes with ActiveRecord
  • #4 -- Protect email addresses in views from being gathered by spammers
  • #5 -- Avoid sending messages to nil


Scramble email addresses in views to reduce spam

If you put your email address on a public web page, you can usually be sure to get tons of spam from there on, because address harvesters will sooner or later visit your page and recognize the email address.

There are different solutions to prevent harvesters recognizing an email address. I personally don't like the use of images to display email addresses or the use of feedback forms instead displaying addresses at all. These methods have a negative impact on the site usability, since a visitor wouldn't be able to easily copy an email address to his email application anymore.

Another method is to scramble email addresses in a way that harvesters cannot recognize it. Using JavaScript, the address is unscrambled and displayed to a human visitor. Even though this is not a foolproof solution, it provides the best measure between safety and usability in my opinion -- as long as you do it right.

So here's an easy way to use scrambled email addresses in Rails views.


Rails moves to GIT - no more SVN?

Yesterday, in my post about upcoming features in Rails 2.1, I wrote, that Git gets more and more attention in the Ruby on Rails scene recently -- and this morning, I stumbled across a post from David, where he annonced, that Ruby on Rails itself is moving from Svn to Git.

That was a rather welcome news to me, since I personally use Git for quite some time now and have switched all my repositories to Git a while ago. However, some people seem to be concerned about Svn being dropped by Rails. But it's not as bad as you may think, since this doesn't mean that you have to move to Git as well...


Assembling a partitionable software-RAID with mdadm: Device or resource busy?

This weekend, the error message "device or resource busy" almost drove me crazy when I was trying to assemble (activate) a software RAID array.

I was experimenting with a software RAID-1 setup (two mirrored disks) using VirtualBox before planning to deploy it to a live server. Because I never liked the idea of creating 4 partitions on each disk and mirror them with individual arrays (/dev/md0 to /dev/md3), I planned to create one partitionable array that spans the whole disks (/dev/md_d0). I.e. the one and only array /dev/md_d0 consists of two whole disks /dev/sda and /dev/sdb. The advantage: the second drive is an exact copy of the first one -- even partition table and master boot record are identically -- just like with a hardware RAID. So in emergency cases, you can even use one of the disks directly, without activating RAID drivers.


syslog-ng eating up CPU time for no particular reason

I encountered a really odd phenomenon with syslog-ng yesterday. A mail- and web-server with considerable traffic was disconnected from the net for some hours during a router failure. After the router came back online and the server was reachable again, I noticed an unusual high system CPU load. Strangely, the server itself wasn't changed in any way (it wasn't even shut down) and suddenly the CPU utilization raised to around 80%, whereas it normally stays under 20%. Strange thing was, that most CPU time seemed to be eaten up by the syslog-ng daemon.


Re-syncing a Cherry eVolution Orca wireless desktop

A few month ago, I wrote about my wireless mouse at home suddenly stopped working (losing sync to the receiver) and how to fix it.

Now it happens that I got an eVolution Orca (the successor of the Marlin) at the office. Guess what happened... A few days ago, the keyboard stopped working... The Orca doesn't have any LEDs or switches and changing betteries didn't help, so I called the support hotline again. Indeed there again is a method to reset and re-sync the keyboard: Take out the batteries and leave them out for 5 to 10 minutes. Indeed that worked for me.


Edge Rails and gettext: undefined method file_exists? (NoMethodError)

After updating to the latest edge Rails via SVN today, my local development webserver suddenly started to respond with a 500 application error for every request. The mongrel console revealed:

NoMethodError (undefined method `file_exists?' for #<ActionView::Base:0x2aaaac3eee10>):
    /usr/lib64/ruby/gems/1.8/gems/gettext-1.90.0/lib/gettext/rails.rb:280:in `render_file'
    /usr/lib64/ruby/gems/1.8/gems/gettext-1.90.0/lib/gettext/rails.rb:278:in `each'
    /usr/lib64/ruby/gems/1.8/gems/gettext-1.90.0/lib/gettext/rails.rb:278:in `render_file'
    /vendor/rails/actionpack/lib/action_controller/base.rb:1107:in `render_for_file'
    /vendor/rails/actionpack/lib/action_controller/base.rb:841:in `render_with_no_layout'

Digging deeper, I found out that the method ActionView::Base#file_exists? has been moved to a different place in Rails' Changeset 8683. Fortunately, fixing it is easy.


Switching to b2evolution

Finally, today I dumped my WordPress installation and switched to b2evolution. Playing around with b2evolution for the last few days, I am surprised how such a nice blog software could exist without me noticing it earlier.

Unlike WordPress, b2evolution has cleanly written structured code and lots of more features (and you don't get new security issues every week or so). Sure, there aren't as many plugins and themes available yet, but skins and plugins are well structured and easy to learn, so it was no problem at all to get all the features.

Migration was pretty easy. I just installed an export plugin to WordPress that exported all postings into MovableType format and imported that data with the b2evolution MovableType import function. All postings, comments, trackbacks and categories were converted and I later added tags to each posting manually.