Entries from April 2008 ↓

Testing = insurance?

Some people think (and they\’re right) that professional developers write tests. So, if you don’t do them, you’re an amateur. In my current job, I don’t write tests. And I’m not very happy about it, because the decision-making process of whether testing or not is beyond my control.

Something that I must say: In my pet-projects is the complete opposite. Since I’m quite new to testing, I must say it is a very addictive thing. This week for example I almost introduced a bug in the code, and was saved by my humble integration tests.

What do you think about people who do not insurance their cars? Irresponsible? I got two cars stolen on the last four years, and I must say it: You must insurance your car before taking it out of the garage. You just stop complaining about a R$150,00 monthly fee after you get a R$33.000,00 check from them and buy a new one.

[crazy_angel_investor_bait]But there\’s no company that insurances our software[/crazy_angel_investor_bait]. Meanwhile, we can “insurance” our own code by writing tests. And also set goals for it, like stop coding because all tests passed. This is useful for people who seems to spend a lot of time on trivial matters. Hey, you software do what it is supposed to do. Go on with your life.

So here is the (poor) analogy: You can’t tell when you’re gonna have a bug in your code, just like you don’t know when you’re gonna have your car stolen. You won’t have a insurance company to help your minimize your losses, but with tests, you know that your code would break some other working feature, and the number of bugs that do go public drops down heavily.

From today on, every time you go to a place where people don’t write tests, or hear someone say they don’t do it for whatever the reason, think about them as people who take the risk of losing a some tens of thousands of dollars (or brazilian reais in my case) everytime they drive their cars.

Restful authentication in rails. Quickly.

  1. Add the repository to your repository list: script/plugin source http://svn.techno-weenie.net/projects/plugins/
  2. Install the plugin in your rails app: script/plugin install restful_authentication
  3. Generate the user model and the session controller:script/generate authenticated user session --include-activation
    In this step, you can change the words user and session for anything you want. If you skip the --include-activation option, no code for the classical activation e-mail will be generated.
  4. Edit config/routes.rb, adding the following lines:
    map.activate '/activate/:activation_code', :controller => 'users', :action => 'activate'
    map.signup '/signup', :controller => 'users', :action => 'new'
    map.login '/login', :controller => 'session', :action => 'new'
    map.logout '/logout', :controller => 'session', :action => 'destroy'

    Again, changing users and session for anything you might wanna call them.

  5. Run the migration: rake db:migrate. This step is not necessary if you provide the --skip-migration option while generating the model and the controller.
  6. Move the following lines from the users and session controllers, to the Application controller:
    # Be sure to include AuthenticationSystem in Application Controller instead
    include AuthenticatedSystem
  7. You\’re done!

Some things to be said: First, it is nice (and even encouraging) to hack around the generated code and see what is happening. For some people (maybe many), some concepts are new, and the reading process might prove itself very interesting.
Second, since this is a standard way of authenticate users, it may be very easy for crackers to hack into your app, so look around and change anything you might want.

This is a summary of the restful_authentication railscast. Watch the original if you can.

Stepping stone in Mephisto 0.8

When I set up this blog some weeks ago (and in my opinion, it is still under construction), I tried to use themes like Stepping Stone, Ocadia, and others, but almost every theme I tried to upload any of them, I got error messages, like this one: Invalid theme uploaded: [ThemeError] No such file or directory - templates.

I tried to solve this by myself, so I started hacking. First, I opened the Stepping Stone and the Red Sky at Night zip files, the latter imported successfully into Mephisto. And then I saw that the Stepping Stone zip had a ’stepping_stone’ directory and nothing else in the root, while the Red Sky at Night had all the structure right from the beginning.

After I generated a new zip with a similar structure, the second try gave me this message: Invalid theme uploaded: [ThemeError] No such file or directory - javascripts. Now, looking again, the zip file didn’t have a javascripts directory in it. Directory created, nothing, inside, third try to upload: Theme imported Successfully!

I looked at the Red Sky at Night zip, and its javascripts directory was empty too, so I don’t think it will be much of a problem. If anyone somewhere reads this, and would like to have a working copy of Stepping Stone and Ocadia themes, post a comment and I will upload it.

P.S.: Thanks for the creators of the themes.

Unicode support in rails. Quickly.

  1. Create the database with Unicode support. In MySQL, for example, it would be something like this:
    create table onon_development character set utf8;
  2. Add a meta tag to your template. It can be like this one:<meta http-equiv="content-type" content="text/html; charset="UTF-8"></meta>
  3. Everytime you wanna do Unicode-friendly operations, use the method chars of the String class, instead of using it directly.
  4. Done!

Linus on Git

I just finished watching to this video. Everybody is talking about Git, and this video explained a lot of things to me, so I recommend it.

Just one comment: Besides Linus being a genius, I can\’t tell the difference whether he is making fun or just having no respect of other people.