La Coctelera.">
La Coctelera

Modeling entity relationships in a non-relational scenario

Since I saw the presentation about Couch Potato, a library for using CouchDB with your Ruby applications I was very curious about how to map a relational schema (that is the most common way to model entities and their relationship) in a document / plain schema where each element has no relation with the others.

And at last, today I found a cool post from Google and it's equivalent for CouchDB.

Now it's time to play with CouchDB!

if params[:action] == 'post' - = render :partial => 'related_posts', :locals => {:post_id => @post.id} end -

Forcing I18n locale in a block

If you work in a project with i18n maybe you found useful this small trick for forcing the locale in a block:

 module I18n 
   class << self  
     def in_locale(new_locale, &block)
       old_locale = I18n.locale
       I18n.locale = new_locale
       yield
       I18n.locale = old_locale
     end
   end
 end
 

This is very useful, for example, when you have to send a notification to a user in his locale, not in the current locale, so you have to do it in this way:

   I18n.in_locale(@user) { UserMailer.deliver_new_comment(...) } 
 

Hope it helps.

if params[:action] == 'post' - = render :partial => 'related_posts', :locals => {:post_id => @post.id} end -

i18n_gettext

Two weekends ago we celebrated the first Rails Hackathon in Madrid: the idea was to make groups of two or the people, select a ticket from the Rails core, and create a patch for it.

Sam Lown and me, decided to work in a plugin for allowing compatibility between GetText and the new Rails i18n support. The result is our beta plugin i18n_gettext, which is still in development and we are trying in beta versions of unvlog.com and planetaki.com.

If you try it, any feedback will be wellcome.

if params[:action] == 'post' - = render :partial => 'related_posts', :locals => {:post_id => @post.id} end -

Testing

Testing Twitter notifications

In La Coctelera we offer to the users the possibility to automatically update their Twitter status with a link to the last post or the last published photos (shortly :).

The tests for notifying twitter are very easy if you use the powerful Mocha (in the beginning we tried to simulate the Twitter::Base class, but it gets a little bit more complicated):

   def test_an_activity_of_new_photos_notify_in_twitter
     User.any_instance.stubs(:premium_role?).returns(true)
     User.any_instance.stubs(:twitter_photos?).returns(true)
     User.any_instance.stubs(:twitter_username).returns('wadus')
     User.any_instance.stubs(:twitter_password).returns('wadus')
     photo = photos(:photo_blat_1)
     a = activities(:activity1)
     Twitter::Base.any_instance.expects(:update).with("He subido una nueva foto a La Coctelera: #{photo.guid}").once
     a.notify_in_twitter
   end
 

If you don't know Mocha you should learn to use it, in other case, you can ignore this post :)

Also, I recommend you to watch this old screencast about Mocha

if params[:action] == 'post' - = render :partial => 'related_posts', :locals => {:post_id => @post.id} end -

Updating Query Memcached

After a lot of time without having time I decided to update query_memcached plugin with a lot of changes that the github collaborators pushed some weeks ago.

These are the major changes:

  • there was a bug that made the plugin don't work in some situations
  • the philosophy have changed a bit and now not all is magic, and you have to indicate in each model if it has to use cache or not
  • at last I could write down some tests

It is important for you to update, specially if you where using the previous version.

Enjoy!

if params[:action] == 'post' - = render :partial => 'related_posts', :locals => {:post_id => @post.id} end -

Ruby

Program your i-Buddy with Ruby

This days I have been working on a small library for the i-Buddy device, a small gadget for MSN Messenger lovers, that you can connect to your computer through a USB port and has some functionality like light its head with different colours, light a small hearth, move the flaps and turn left or right.

The main attractive is the price (no more than 25 €) and the simplicity.

The library result of my work is rubi-buddy, and works with Ruby and ruby-usb, and its inspired in the one that developed at 11870.

The library is in progress, because I still have to implement the flap movement. Also, I want to add more examples in the project. At this moment there is only one that checks the response code from La Coctelera and turn the head light green or red.

If you are interested, I encourage you to use it and to add more examples.

if params[:action] == 'post' - = render :partial => 'related_posts', :locals => {:post_id => @post.id} end -

Snippets y Trucos

Small tip for debugging Javascript in IE

Today we had to find a small bug in La Coctelera's javascript: the bug was a hash with a colon after the last element. The problem was that, of course, it didn't work on IE 6 and don't have any debugger for Javascript installed on my Vmware.

The only information I had was the generic error (an object was expected), and the line and column where the mistake was. So I decide to understand how to find that line in all the linked javascript files.

My conclusions are that the line could be from the HTML resulted from loading the page, or that line in one of each javascript files. It is, you have to find that line in all your javascripts and in the result page (viewing the source code) and then try to deduce which of all causes the error.

If the number of line is big enough you could reject some files, but not always you'll be so lucky.

And that's what I have learnt today.

if params[:action] == 'post' - = render :partial => 'related_posts', :locals => {:post_id => @post.id} end -

Snippets y Trucos

Application servers serving static files

Since mongrel and thin appear as the people favorites application server (with all my respects to Passenger), the configuration of webservers changed in order that you have a web server plus a kind of proxy and then a lot of application servers.

The idea is that, given a request, the webserver has to decide if it is an static or a dynamic url and then serve the request itself or pass it to the proxy of application servers..

The problem is that, as thin and mongrel are also web servers, if you don't take care with the configuration your application server can be serving all the static files too and be busy when it has to attend a dynamic request, with all the problems it causes.

That's why for me is very surprising when you search in Google about Apache + Mongrel, for example, and you arrive to virtual hosts configurations that doesn't take this into account.

The rules are very easy:

 RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_URI} !/stylesheets/.* [NC]
 RewriteCond %{REQUEST_URI} !/images/.* [NC]
 RewriteCond %{REQUEST_URI} !/javascripts/.* [NC]
 RewriteCond %{REQUEST_URI} !.*\.js$ [NC]
 RewriteCond %{REQUEST_URI} !.*\.jpg$ [NC]
 RewriteCond %{REQUEST_URI} !.*\.gif$ [NC]
 RewriteCond %{REQUEST_URI} !.*\.css$ [NC]
 RewriteCond %{REQUEST_URI} !.*\.js$ [NC]
 RewriteCond %{REQUEST_URI} !.*\.png$ [NC]
 RewriteCond %{REQUEST_URI} !.*\.ico$ [NC]
 RewriteCond %{REQUEST_URI} !.*\.bmp$ [NC]
 RewriteRule ^/(.*)$ balancer://mongrel_cluster%{REQUEST_URI} [P,QSA,L]
 

And this is an example: in order to your needs you can tune this configuration including more filet ypes and static directories.

For nginx the rule is also very simple.

So, you know, don't forge to include rewrite rules for serving static files.

if params[:action] == 'post' - = render :partial => 'related_posts', :locals => {:post_id => @post.id} end -