I’m not working

Yes that’s right, despite the fact that I’m employed by Unboxed I’m not working anymore. I’m going to work every day, programming some stuff but it’s not work for me anymore. Really.

I have had this feeling since I start working with Rails and this feeling became stronger when I came to London and joined Unboxed. I had heard about Agile and Scrum and I had tried to practise TDD and BDD before, but it’s really different here and now.

I joined the real world of Agile. I was so excited (and scared) when I had my first standup and my first retrospective. I’m still excited, because everyday is different. One day we have to do standup without our Product Owner (PO) because he is stuck at the tube. On another day, I was alone since everyone else was on holiday. Is it still a standup if only one developer and the PO are present? I think that it is, because the important thing is that I can tell him what is going on and another day of excitement can start.

Of course there are days when I’m tired, but you can be tired even after sitting on the beach with a Mojito in one hand and great book in the other. At the end of a tiring day at the office I look back and discover that I’m tired because I experienced so many new and interesting things.

So what am I actually doing if I’m not working? I’m a little bit scared that I might be dreaming. I’m scared I will wake up in my cubicle with PHP code (without tests) on my screen and without any slight hint of Agile or Scrum and Waterfall knocking on my door. So please don’t wake me; I like it as it is!

Working for unboxed

I start working for Unboxed Consulting at May 2010. Because we have got company blog I’m going to write more of my articles there, but this will be place where you can see the articles before official release date.

Proper error messages on rails

In order to have proper validation error messages on the web page I want to specify what the message will look like. Problem is that if you set validation message in your Person model like this:

validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :message => ‘invalid e-mail address’

you get on you page error message like this:

1 error prohibited this person from being saved

There were problems with the following fields:

‘Email invalid e-mail address’

but customer wants something more human like., for example:

Oh dear there were problem

That doesn’t look like a valid e-mail address. Please try again.

I found a solution and here is how to do that. First we change the Header error message and delete the submessage.

In your view where you are displaying errors use proper options for error_messages_for

error_messages_for(:person, :header_message => “Oh dear there were problem”, :message => “”)

Than the interesting part, error messages are in this format [attribute_name, message] but we need only the message part so without any hacking or parsing we can specify format of the message directly:

validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :message => :”email.format”

The part :”email.format” says that you have specified message text at your i18n file (usually config/locale/en.yml) .

          format: "That doesn't look like a valid e-mail address. Please try again."

And that’s it :) Feel free to comment.

Hiring as a game

I saw an interview with Amy Jo Kim at Mixergy.com about game mechanics,recently. Today I saw Slideshare called Designing a Game Changer by Philip Fierlinger from Xero. At the and of the presentation is short notice that Xero is hiring.
Than I just got an idea about making from hiring a game.
Here is example situation:
You are working for big company as a HR person or something like that and you need hire few programmers. As a clever HR person you come out with game called “win a macbook” or some catchy. Rules of the game is simple.
Write some algorithms or complete some tasks. If you want to be more precise you will write more levels of this game and provide some kind of feedback.
The winner of course win the price and you can offer him and other best solvers the job.
If you are lucky some solvers accept your offer. In the end you will have great programmer and save some money for personal agency.
This principle is applicable in larger scale and has of course some difficulties but is just an idea. :)

jQuery bug or just strange behavior ?

Today I discovered some strange behavior of jQuery library, when I added some new functionality to our old adminstration.
So I’ve got Form element with some inputs and submit button. On inputs are binded click event. That mean that only if you click on input element Event will fire, right ? But I found that even if you press return key, Event fires too. I know that if you press return key and you are inside form element, Form will fire submit Event. But it fires click Event which is strange. jQuery documentation says:
The click event fires when the pointing device button is clicked over an element. A click is defined as a mousedown and mouseup over the same screen location. The sequence of these events is:

* mousedown
* mouseup
* click
Do you see any keyPress event or something, I don’t. I just don’t know if it is a bug or if it’s purpose. I also test if it’s because that keyPress Event or because that submit. It seems to me that if you press return key jQuery call submit Event and somehow call click Event too. Here is a example. What do you think ?

Solution for prevent this behavior is bind click Event only to inputs whith type text.

Btw: It works same way at all browsers.

HTML5 storage

I recently started write pomodoro aplication to practice my skills and try out new technologies.
One of new technology that I tried is HTML5 storage. Storage is actually database but I prefer the word storage.
I show you some examples here. I also
must tell you that Html5 storage works only at Safari. I tried it at Firefox 3.5 and Chrome
but didn’t work even after installation WebKit Nightly.

Fist step is change doctype definition and put manifest at html tag:

1: <!doctype html>
2: <html manifest="PomodoroTimer.manifest">

Ar row 2 you can see defintion of manifest file which is like cache. You can named it
as you like.

Step two is javascript. Because you need somehow dynamicaly add and remove or create records at your hmtl5 storage,
javascript is the right choice. So here is how you can create storage.

if (window.openDatabase){
  storage = openDatabase("pomodoro_timer", "1.0",
   "HTML5 Database for PomodoroTimer", 200000);
  if (!storage)
    alert("Failed to open the database on disk.");
  } else {
    alert("Couldn't open the database.);

At first line you can see how to check if window object support starage. At line 2 I create instance
of a storage: storage name, a storage version, a display name, and an estimated size, in bytes, of the data to be stored in the storage/database.
Rest of lines didn’t need an explanation.

So we created a database next step is create some table.

storage.transaction(function(request) {
  request.executeSql("SELECT COUNT(*) FROM tasks", [], function(result) {},
  function(request, error) {
    request.executeSql("CREATE TABLE tasks 
         number_of_pomodoros INT, number_of_interuptions INT)", [],
      function(result, error) {           

At line one we create an anonymous function for storage transaction. Official definition of transaction method:
“The transaction method takes one to three arguments:
a transaction callback, an error callback, and a success callback. The transaction callback gets passed a SQL transaction object on
which you can use the executeSQL() method.This method takes from one to four arguments: a SQL statement, arguments, a SQL statement callback,
and a SQL statement error callback. The SQL statement callback gets passed the transaction object and a SQL statement result object which gives access to the rows, last inserted ID, et cetera.

Last step is insert some data to the table.

storage.transaction(function(request) {
  request.executeSql("INSERT INTO tasks 
     (name, number_of_pomodoros, number_of_interuptions) VALUES (?, ?, ?)", 
    [task.name, task.number_of_pomodoros, task.number_of_interuptions],
     function(result) {}, 
       function(request, error) {

I inserted some data from my object Task. You can see complete code
My sources was this example aplication and w3c documentation.

Have fun.
ps: Select from storage is asynchronous so my pomodoro aplication didn’t use html storage until I resolve this issue.

Pair programming

I just finished pair programming with my friend Laco who live at Barcelona. We used Skype, Collabedit and gitHub for code synchronization. I’m really tired after six hours, but I’ve good feeling about this. I hope we will continue on that soon. You can see our work here.

Quick example of testing login form with mechanize and Test::Unit

I started testing with Ruby. So i first use Test::Unit and then i want to test some functionality on web. I read some examples at mechanize documentation, which is here or at your ruby/gems directory. So let’s look at the code.
require ‘mechanize’
require ‘test/unit’

class LoginTest < Test::Unit::TestCase
def test_login

#creating mechanize object
agent = WWW::Mechanize.new

#setting url
page = agent.get(‘http://zaparka.cz/wp-login.php’)

# get first html form on page, u can use page.form(‘form_name’)
login_form = page.forms.first

#fill value <input type=”text” name=”log” /> with login name
login_form.log = ‘name’

#fill value <input name=”pwd” type=”password” /> with password
login_form.pwd = ‘secrets’

#submiting form and saving result page to value page
page = google_form.submit()

# test if login was succesflull
assert_equal ‘Dashboard ‹ Petr Zaparka — WordPress’, page.title

It’s very easy and simply. However you can’t test javasript functionality. Next time i post testing with WEBRAT.