Live geek or die tryin'

A Question About Applications’ Design

The idea of creating a blog post to gather people’s opinions is pretty weird, but nothing ventured, nothing gained.

Hello,

I’m building an application that will have many types of multilingual dynamic content: news, about, announces, meta tags, meta description, and maybe more. The app is a Rails one, so I’ll be talking about tables and not entities.

I’ve been creating a database table for each of the content types above, and I noticed halfway that they have (almost) the same columns: title, body, lang_id. I decided then to use one single table, called text, which will have a text_type_id column, additionally to all the previous columns. type_text will only contain the type of the content (news, about, announce, etc…).

The thing is that I’m not very convinced about what I’ve done, I find it a bit dirty because many kind of data will be stored in the same place. On the other hand, if I create a table for each type, it seems to me that there will be some kind of redundancy, which is also messy in my eyes.

What do you think about this? Please leave a comment explaining what you would have done if you were in my shoes, and why.

Thanks!

Rails: Cannot Load Such File – Openssl

While setting up my Rails development environment on my fresh Kubuntu VM, I’ve encountered many errors. This one was occurring when trying to launch a WEBrick server:

/home/dinduks/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:240:in `require’: cannot load such file – openssl (LoadError)

This error means that your Ruby isn’t compiled with openssl.

Assuming that you use RVM, these are the steps to follow to fix this issue.

Install the openssl package

rvm pkg install openssl

Remove the Ruby installation you’re using

rvm remove 1.9.3

And finally recompile Ruby with openssl

rvm install 1.9.3 --with-openssl-dir=$HOME/.rvm/usr

Everything should be working now. Don’t forget to:

rvm use 1.9.3 --default

I hope my first Rails related tutorial was helpful, happy coding! :)

GitHub: Free Plans for Students and Teachers

I’ll be soon working on a school project, which led me to look for a place where to version my code.

Since I use GitHub daily, I checked its plans first. The cheapest one, called micro, gives 5 private repos for $7/month. I unfortunately only need a single private repo, and I don’t want to pay for 4 ones that I probably won’t be using.

Although I knew about Bitbucket’s free private repos, the ideas of leaving GitHub and having my repos here and there didn’t please me that much.

Luckily, I stumbled upon GitHub’s Educational offers, that are available to both teachers and students.
The teachers’ offer concerns organization accounts. And the students’ one on the other hand is simply a micro plan, and lasts 2 years, enough to get your ass a job and pay a subscription.

GitHub

To benefit from this offer, just contact GitHub from the Request an educational account page.
Make sure you are logged in with the account you want the plan for, and tell them why you need it.

Thank you GitHub!

Programming Motherfucker

Implementing a Simple Caching System

Hello,

I’m going to show you a simple way to cache a file.

To illustrate my example, I will use a script that creates a single image from multiple images. I have written that script a few months ago to generate a patchwork of my top albums in Last.fm, due to the lack of working similar scripts.

Dinduks\

First, let’s see how the script works, without the cache system:

As you can see, the image is generated whenever the script is called. This isn’t only greedy on the server’s resources, but does also take a little time ; between 4 and 6 seconds for a common patchwork.

To avoid this repetitive task, the idea is to save the generated image on a file, and, each time the script is called, check if the image doesn’t already exist before creating it again.

Create a signature of the source file

Since the image is generated from data that comes from the Last.fm Web Service, it will change according to it’s response. Let’s then create a MD5 hash of it.

$response     = file_get_contents($query);
$responseHash = md5($response);

Name the generated file

It’s time now to choose a name format for our file. I chose the following:

$fileName = "images/$user.$period.$rows.$cols.$imagesSize.$responseHash";

Note: Make sure you have the write and read permissions on images/.

Save the file

At the end of the script, just before displaying the image, save it.

imagejpeg($patchwork, $fileName);

You’ll have to use fwrite() or a similar function depending on the type of the file you want to save.

Check if the file exists

The file is well saved now.
The final step consists in checking, at the next request, if that file exists. If it’s the case, answer with it, if it is not, run the usual code ; which is the image creation.
The code below should be put right before the image creation process.

if (file_exists($fileName)) {
    header("Content-type: image/jpg");
    echo file_get_contents($fileName);
    exit;
}

You are now ready to implement your own little cache system. Have fun. :)

Symfony 2: Execute Console Commands on a Specific Entity Manager

The following isn’t an exact science, however, if you didn’t --help, it will spare you a 10min research.
This is your lifesaver: --em.

Example

In case you want to generate mapping information using the “client” entity manager:

php app/console doctrine:mapping:convert yml /src/Acme/ClientBundle/Resources/config/doctrine/metadata/orm --from-database --force --em="client

Translated from my french post Symfony2: Executer les commandes console sur un Entity Manager spécifique