Hi, I'm Benedikt Deicke, and I'm a freelance web and software developer. I'm mainly building user focused web applications using Ruby on Rails and JavaScript. Additionally I'm currently studying for my master's degree and enjoying photography in my spare time. Feel free to get in touch with me, I'm available for hire!

February 14th, 2008
Extended: Forms with widgets

There is a great article by Jason Long on Vitamin called ‘Streamline your forms with widgets’. In his article Jason Long describes a nice looking way to clean up a lot of checkboxes into multiple dropdown widgets. What I really liked was the idea to have some summary about the selected items next to the title of the dropdown widgets. Unfortunately he didn’t implement the updating function but just mentioned it as a possible extension. In addition he pointed out that “Any Fuel” would be much easier to read than “Fuel (Gasoline, Diesel, Alternative)”. As I was in the right mood for some Javascript I implemented the missing functionality.

At first I added four little lines to the top of the toggleDropdown() function. This little snippet checks weather the panel is currently visible (so the function was toggled to hide the panel again) and calls the updateTitle() function.

   1  function toggleDropdown(panel) 
   2  {
   3    panel = $(panel);
   4    if(panel.visible()) {
   5      updateTitle(panel);
   6    }
   7  
   8    // toggle the requested one
   9    Element.toggle(panel);
  10       
  11    // then hide all of the others so that only
  12    // one (at most) is open at a time
  13    $$('body .dd_option_panel').each(function(node){
  14      if (node != $(panel)) {
  15        Element.hide(node);
  16      }
  17    });  
  18  }

To do the actual updating of the title I used Prototype’s nifty little DOM traversal toolkit. It provides methods such as Element#select(), Element#up(), Element#down(), Element#previous() and Element#next(). All of can be supplied with a selector, similar to those in CSS.

First I select all child elements of type “input” and check their value. If the value matches “on” the checkbox is selected and I travel down to the next “label”-element to push it’s content into an array. Afterwards I check the size of this array and decide how to change the title. If none of the checkboxes are selected I add “No ” to the title and don’t display the list of the selected values. Accordingly if all are selected I prepend “Any ”. Otherwise I leave the title as it is and update the list behind it by joining the elements of the array into a string. To prevent titles such as “Any Any No Any Fuel” I clean up the title by removing either Any or No in front of the title using String#gsub().

   1  function updateTitle(panel)
   2  {
   3    selected = new Array();
   4    panel.select('input').each(function(box){
   5      if(box.getValue() == 'on') {
   6        selected.push(box.next('label').innerHTML);
   7      }
   8    });
   9       
  10    inner = panel.previous(".inner");
  11    title = inner.innerHTML.gsub(/^([Any|No])/, '');
  12    
  13    if(selected.length == 0) {
  14      inner.update('No ' + title);
  15      inner.down(".light").update('');
  16    } else if(selected.length == panel.select('label').length) {
  17      inner.update('Any ' + title);
  18      inner.down('.light').update('');
  19    } else {
  20      inner.update(title);
  21      inner.down('.light').update('(' + selected.join(', ') + ')');
  22    }
  23  }

In order to get correct titles right from the beginning I update all titles after the page loaded.

   1  Event.observe(window, 'load', function(event) {
   2    $$('body .dd_option_panel').each(function(panel) {
   3      updateTitle(panel);
   4    });
   5  });

That’s it! Click here to see the changed example.

Of course all the credits for the original implementation, graphics, and idea go to Jason Long.

Posted by benediktFiled in Articles, Javascript

September 21st, 2007
ActiveRecord: Using the "type"-column

By default it’s not possible to use a database-column called “type” for anything else than single table inheritance. To change this, simply use set_inheritance_column() and read_attribute():

   1  class TypeTest < ActiveRecord::Base
   2    set_inheritance_column(:something_else)
   3  
   4    def type()
   5      read_attribute(:type)
   6    end
   7  
   8    def type=(value)
   9      write_attribute(:type, value)
  10    end
  11  end
Posted by benediktFiled in Articles, Ruby on Rails

July 10th, 2007
Getting a class' subclasses

I needed a way to get a list of the subclasses that inherit a specific. Unfortunately there is no method like Class.subclasses (there is Class.superclass, though) so I had to look for another way to achieve this. Let’s say, we want to have an array containing all subclasses as a class variable of our superclass Strategy. In order to fill the array we’ll overwrite the inherited class method of Class. (Already confused by all the classes? ;-))

   1  class Strategy
   2    @@subclasses = Array.new
   3    class << self
   4      def inherited(klass)
   5         @@subclasses << klass
   6      end
   7  
   8      def subclasses
   9        @@subclasses.join(', ')
  10      end
  11    end
  12  end

Now, every time a class extends Strategy our new inherited method is called and adds the class to our array.

   1  class StrategyA < Strategy; end
   2  class StrategyB < Strategy; end
   3  class StrategyC < Strategy; end
   4  
   5  # Let's get the current list of subclasses
   6  puts Strategy.subclasses # will output StrategyA, StrategyB, StrategyC

What happens if a class inherits any of our subclasses? Well, as long as you don’t overwrite the inherited method again it’s also added to the array.

Posted by benediktFiled in Articles, Ruby

May 22nd, 2007
HowTo: Setting up Lighttpd + fastcgi

This short tutorial is going to show how to set up Lighttpd with fastcgi on Debian 4.0 (Etch). Actually it’s nothing special but I hope it demonstrates how easy it is. ;-)

What is Lighttpd?

Lighttpd is a light (I bet you already guessed that … ;)), fast and secure high performance web server. It perfectly fits the needs for high load websites but it’s not the best choice for servers hosting multiple independant websites. There are some popular sites running on lighttpd (eg. Youtube).

Installation

If you’ve been running apache, the first step is to stop it:
   1  apache2ctl stop
Afterwards you can savely install the lighttpd package:
   1  aptitude install lighttpd

It should install smoothly and start itself when finished.

Basic Configuration

Lighttpds default configuration on Debian file is /etc/lighttpd/lighttpd.conf. The default settings might be just fine for your needs, but it’s a bit messy for my taste. So let’s simply write our own. Open the configuration with your favorite editor.

The first thing we need to do is loading the required modules. This is what we’ll use for this tutorial:
   1  # Modules
   2  server.modules = (
   3    "mod_access",
   4    "mod_alias",
   5    "mod_accesslog",
   6    "mod_rewrite",
   7    "mod_compress",
   8    "mod_evhost"
   9  )
Of course there are a lot more modules. Just take a look at the list provided in the Lighttpd documentation wiki. We’ll use mod_evhost to simulate multiple virtual hosts. More on this later. Next we’ve to set some required values in order to get the server working. This should be self explaining:
   1  # Server settings
   2  server.port               = 80
   3  server.pid-file           = "/var/run/lighttpd.pid"
   4  
   5  server.username           = "www-data"
   6  server.groupname          = "www-data"
   7  
   8  # Document root
   9  server.document-root      = "/var/www/default"
In order to use logging we also need to set the following two paths:
   1  # Logs
   2  server.errorlog           = "/var/log/lighttpd/error.log"
   3  accesslog.filename        = "/var/log/lighttpd/access.log"
You also might want to set index file names and enable directory listing. Do this by adding this to your configuration file:
   1  # Index files
   2  index-file.names          = (
   3    "index.php",
   4    "index.html",
   5    "index.htm"
   6  )
   7  
   8  # Directory listing
   9  server.dir-listing        = "enable"
To finish the basic configuration we’ve to add two more lines. They’re used to load more configuration files located in /etc/lighttpd/conf-enabled/ and to load the mime-types:
   1  # External configuration files
   2  include_shell "/usr/share/lighttpd/create-mime.assign.pl"
   3  include_shell "/usr/share/lighttpd/include-conf-enabled.pl"

Don’t forget to save! ;-)

Enabling fastcgi support

In order to use ruby or php on our server we need to activate lighty’s fastcgi module. This can be done by running: true You’ll see a list of available modules and asked to enter the module in question (fastcgi). Afterwards open up /etc/lighttpd/conf-enabled/10-fastcgi.conf to see the fastcgi configuration. By default php4 support is enabled. In order to use it you need to install php4-cgi: true In order to use the newly created configuration file we need to restart lighttpd: true That’s it! Now go and place a file like this called info.php in /var/www/default:
   1  <?php phpinfo(); ?>
View it’s output by visiting http://www.your-domain.com/info.php. If everything worked you should now see php’s details.
Posted by benediktFiled in Articles, Linux