soledad penadés
repeat 4[fd 100 rt 90]

Archive for the ‘ruby on rails’ Category

20061122 Mongrel! Mongrel! Mongrel!

And once you understand what is Mongrel you'll have the strong desire to shout it loudly - as many times as possible, just like I'm feeling now! Like "how come I hadn't found this before!? where have I been for the last months?".

For those of you which do not understand my sudden explosion of joy (why is she shouting about dogs? is she now going to talk about dogs every day? etc), I'll explain you what Mongrel is and why it's so extremely useful, apart from lovely.

Developing in Rails has shiny and bright aspects. Scaffold a couple of models in front of your friends and all of them will go oooooooh aaaaaah! (unless they are django users, but that's another discussion). Show them the power of plug-in's and how to extend capabilities of objects thanks to ruby's features and you'll hear a couple of jaws hitting the floor. RJS - anybody heard of it, writing javascript code without actually writing javascript code? Database migrations, anyone? Yeah, Rails is good stuff.

And starting your app, locally, is so easy! ruby script/server and you're done! "Welcome Aboard: You're riding the rails".

Ok. Now go and install it in your favourite host. No one moves - suddenly silence dominates what was an animated and cheered crowd. Why?

Because installing a Rails application in a usual web server is a Pain In You Know Where

PHP is so popular because there are a couple of fast modules for Apache (mod_php4 and mod_php5) which make running any php script through a server extremely easy. But the ruby module for Apache (mod_ruby) seems to be the most unreliable stuff I have heard of in a long time. So that's why we were told to use another server - Lighttpd with FastCGI and the Ruby module for FastCGI. The problem with it is that it is complex. You need to configure Lighttpd, using another port, proxy it through Apache, create a cron process which takes care of the process and makes sure it will be running even if the server is restarted or the process killed, etc.

Summarising: I spent like two weeks trying to understand how to deploy an application in my hosting. At last it worked but I'm not quite sure how I did it.

But then Mongrel arrived. I had heard about it repeatedly these past months. Everybody talks a mix of capistrano-mongrel-blabla. I found out about Capistrano on the past LRUG meeting, and now I wanted to know what was that mongrel thing. And ooooohhh aaaah!! it is "like a webrick on esteroids", a fast ruby server that you can use in your favourite host, as easy as using webrick.

So, instead of doing

ruby script/server

you would do

mongrel_rails start

And voila:

** Starting Mongrel listening at 0.0.0.0:3000
** Starting Rails with development environment…
** Rails loaded.
** Loading any Rails specific GemPlugins
** Signals ready. TERM => stop. USR2 => restart. INT => stop (no restart).
** Rails signals registered.  HUP => reload (without restart).  It might not work well.
** Mongrel available at 0.0.0.0:3000
** Use CTRL-C to stop.

And you're riding the rails again :)

The best of all is that you can use it the same way in your favourite-host-with-mongrel, which luckily seems to be  the case of mine. You just need to add a cron line for making sure that the process is alive (or the application won't work!), and then you can go for as much complexity as you want, like configuring a "cluster" of mongrel's (for increasing performance), adding plugins, making apache serve images and static content, etc.

I still haven't tried it in the live server, just locally, but it is really faster than WEBrick. Reading the documentation, it even gives you support for running the applications in the same domain but different paths. Like mydomain.com/app1path/ and mydomain.com/app2path. That's very cool - I really hate setting up subdomains because there can't be anything easier than creating a new directory in your web.

And the dog pictures, aren't they lovely?

I absolutely dig Mongrel!

20061112 Bricks and ruled paper (a short story)

Two authors were discussing about their writing methods and specially, about the paper they used to write in. The first writer (let's call him Phil) used to prepare himself ruled paper sheets, because -he argued- it was the only method to know exactly how the rules were spread across each sheet, so he could control the height between lines, the amount and exact tone of the ink used to print the rules, etc. The second writer (called Rube) used to prepare the ruled paper but he gave up quickly and decided to just buy ruled paper notebooks, so that he could concentrate on the actual writing instead of the rules alignment.

At the end, Phil's home-made sheets were just quite messy and didn't look as professional and serious as Rube's one. Rube was always able to deliver the books on time, while Phil spent hours and hours improving his custom made method for drawing rules and making sure it was efficient and fast. Often the method failed and he had to restart from scratch.
No wonder Phil's father was a very weird architect which believed in making his own bricks. He took it with lots of dedication and pride but was only able to finish a couple of buildings in his life. Going up the family tree, Phil's grandfather was a mechanic which absolutely defended the need to build oneselves' wheels and tyres and re-engineer their design with every new patron which came to his little workshop.

Phil thinks Rube is a pretentious arrogant guy trying to impress women in any of the multiple social acts which he can attend (as he's got lots of free time thanks to just using conventional ruled paper when writing), and would like to be able to say this to the entire world but he's just too busy drawing lines in order to do any actual work at the end.

20060914 Pizza On Rails september 2006

As usual, I write about great events with one day of delay (as I did with Jeff Barr's web services' speech past May). And the great event, in this case, was Pizza On Rails!

After two failed attempts, it was the first POR that I managed to attend, and it was really funny, although I almost failed to attend this one: I had written down the address of the venue, but I hadn't looked at the map - and it was a bit tricky. It was Leicester Square 1, but (I don't know why) most of the buildings didn't show the number in their door. So I went in circles by Leicester Square, until I realized it was "officially" in that street but the entrance was in another one. Obviously it was thanks to a very ordered and british queue in front of the entrance, plus a couple of "Pizza On Rails" posters.

I didn't know anyone at the queue but then a guy, Jason, which had been at past London's Ruby Users Group meeting appeared and said "Hello! I remember you! You let me go the wrong way on past LRUG meeting! I got lost in the building…". And I felt terribly embarrased, hehe. But well, there we went and up! It was the 6th floor so we had a very nice view of Central London (which was specially impressive, when a massive romantic storm appeared, with strong thunders and the lightnings drawing lines all across the sky, following the fireworks around Southwark).

Thanks to the sponsors (Cominded), beer was free (um, as in beer!), as was the pizza. There were vast amounts of both, and we had very nice conversations with a Dutch guy and another mate there (can't remember the names, sorry!)… After a while, it was extremely surprising/funny to find out that Gasman was there as well! Demoscene joins Rails! Wooh! I went to say hi! and ask him if he was preparing something for Sundown, and he is! So pay attention to Sundown06 releases :)

Some time later, a very nice portuguese couple appeared. She had a very good level of spanish! (I can't help but loving the spanish when spoken by portuguese people). They were considering about moving to Barcelona, and we had an interesting chat about salaries in Spain and Portugal and London. London's figures just looked so impressive! And then suddenly someone asked me if I was a developer. I was kind of … uh? which question is it? as the most common question there was "how much Rails do you know?", and it was assumed that you were a developer when asking it. But he was curious about how many women there were actually developers. I explained him my personal theories about the lack of female developers -which basically are summarised in one: until recently, it was uncool to know about computers or machines, but now it is normal and desirable, so this attitude helps a lot towards assuming that it's a profession like any other one-.

Then he revealed himself as being Martin, one of last.fm's developers! Ohhhhh! I couldn't believe it, I mean, it's a service I use almost daily and last thing I could expect from a Rails meeting is to find someone from last.fm (since, as far as I know, they just use Java and PHP). Even more, he came accompanied by two more mates, Julian and Hannah, from the design department. Impressive, I was face to face with the people which creates the interface we use daily :) And I presume that it must be very nice to work at last.fm if all of the staff is like them.

I finally joined them when they left the party (it was 21h already, if I remember correctly), and went with them to the tube station. So we said bye and I went lonely but happy on the south bound Piccadilly Line - as Hannah said: … but does anybody live in Victoria? Hehe, yes… me!
So all in all, a very cool party, I ended almost without voice (and had a very sore throat this morning, arggh), but it was worth it. I'd like to thank the organisers and sponsors for this cool event.

20060506 Do you want to resize and crop images in RoR? RMagick is your friend

In the last iteration of my photoapp, I still wasn't able to update the pictures once updated. I also didn't know how to configure the file_column plug-in for creating different types of resized images.

So I finally decided to get rid of file_column, after having tried to find some kind of answer in forums, mailing lists, documentations, e-mailing the author (still without reply, although I had to follow a process for verifying that I wasn't an evil spambot) and even digging into the file_column code itself, and not finding anything useful for my purposes, as everybody usually gets satisfied with just the image file and a thumbnail.

I also had RMagick compiled and installed, from the previous step, so this part has been really quick. I just needed to know some key things:

  1. how to access the uploaded data from a form into my app
  2. how to resize and crop the images

Accessing the uploaded data from a form

Surprising it is for my php background how easy is to get that data. One could expect having to use some kind of $_FILES array, as in php, but although you can do it if you really need it, there's a simpler way. Just name your input type="file" as something like picture[file_data].

As the other fields are also named in that way (picture[name], picture[description]) what happens is when the controller receives the form submission it is in fact a hash, with keys (name, description, file_data, etc…) and its corresponding values. That easy! So your model can take the file_data value and do whatever you need for your case.

Resizing and cropping images

This is done with RMagick. Apart from the obvious (a magic require 'RMagick' on top of your model) there's little more left to get the resized images. Something like this will be enough:

img = Magick::Image.read(path).first
thumbnail = img.crop_resized(self.versions["thumbnail"][0], self.versions['thumbnail'][1])
thumbnail.write thumbnail_path

Done!

This RMagick thing is :-x beatiful :-x. Specially the crop_resized method. If I had known it when developing xplsv.tv I would have saved so much time…

20060501 My first application in Ruby On Rails

It's time to get serious with RoR! I finally decided that I would do some useful application to really learn how it works, and as I'm quite bored of doing applications for managing text (whether they are intranets, cms's, etc), I opted for recreating my old gallery script (which I did in php) in RoR. So I'm playing with images instead of just text.

This will give me the opportunity to test lots of extra functionalities of the framework, such as the famous integrated javascript effects, ajax, etc, as I want to improve my old gallery script, not simply redo it as it was in php.
So first thing I did was to make sure that I had a working updated version of ruby and rails. The best tutorial I have found which worked perfectly for me is this one: Building Ruby, Rails, LightTPD, and MySQL on Tiger, by Hivelogic. (Although I simply used it to update Ruby and Rails, but didn't build Lighttpd or Mysql).

With that I could be able to follow the recent tutorials and techniques, specially because I wanted to learn how to use a Login Generator, and there was something in the version of Ruby that I had which prevented me to install it (some lib error was thrown each time).

So my app has two parts, a public and an admin one (which was quite easy to anticipate). Once I managed to create the basic structure of controllers and models for the admin, I added the login functionality, as I don't want everybody to get into my app and modify pictures in there.

I used the Acts As Authenticated plugin, although I couldn't manage to get all the functionality that I expected so I took a look at the code of the Sudoku On Rails v5 tutorial at SobreRailes (sorry, spanish only!) and completed it with the missing functionality (basically, it was the activation stuff which was not generated, don't know why). It was sweet and smooth. If I had had to write it manually I would have spent at least three days.

Then I wanted to have support for uploading images. At all, this is about pictures! So after some researching I found a little plugin called FileColumn which allows to get picture uploads working quite quickly, including resizing and automatic deletion if the associated record gets deleted (no more orphan files in the filesystem because of a poor integration between the models and their associated data! hooray!). But for the resizing stuff and thumbnails generation, I needed to have RMagick installed in my system, which I didn't have. So I found a nice tutorial in the RMagick site, which explained how to compile and install RMagick and GraphicsMagick on mac os x, as well as all the libraries on which they depend, as RMagick is like a bridge between ruby and GraphicsMagick. This step took me a bit of time, specially the step of compiling ghostscript, but as I followed the instructions exactly as specified, I had everything working at the end and I could generate thumbnails and a resized version when uploading images. (Also I was also reading one of the supercool books about the tube that mr.doob gave me for my birthday so time flew fastly! :D )
If I had had to do this in php even using the best helpers that I could, I would have spent at least one week.

Then I prepared a draft version of the public interface. Now I can navigate between pictures and albums, using thumbnails and the big images as well, it shows the descriptions for the album or picture, where available. The code is so simple and beatiful that I could get it tattooed. Or maybe do a t-shirt with it, see an example for getting the data of one album:

def album
@album = Album.find(params[:id])
unless @album.nil?
@pictures = @album.pictures
end
end

No more embedded SQL in the middle of the code, no need for manually loading classes which abstract the DB information for the rest of the php code and try to provide a way of artificial modelization of the data, no need to spend time reinventing the wheel!! :D

As I specified in the models that one album has pictures (has_many : pictures), and that each picture belongs to one album (belongs_to :album), RoR is intelligent enough to deduct things from the database and save me that precious time (as it sees an album_id column in a picture row… it obviously is pointing to an album row!).

Unfortunately by the moment the front end is extremely basic, it's pure html with little css applied to it. In the next iteration I will do tasks such as find out how to redefine part of the behaviour of the FileColumn to make it update the associated images too if I upload another image when editing a picture, beautify the front end, and maybe start with those nice ajax capabilities which are supposedly quite easy to use with RoR. It will also be nice to learn how to do the fantastic unit tests with the support that this framework provides, so all in all this promises to be very exciting.

Conclussion: I absolutely love Ruby on Rails.

Stay tuned for a next release - as I already got a port for installing it on my host, it can be whenever I get a decent working version!.

UPDATE: As some people want to take a look (miguev ;) ) I have uploaded the relevant code of the current version. It just has the important things i.e. the models definition, etc. If you want to have a working application you will need to create it first with the usual rails name_of_your_app, configure the database, etc, and then use this code where appropiate. It won't work by itself :P This is just for showing how things are linked between them: cl1ck version 0.1