Dica ao usar Background-fu

No meu projeto atual eu preciso de alguma coisa que me permita rodar processos em background, liberando a aplicação para o usuário. Entre as soluções atuais, a mais atraente é um plugin chamado Background-fu.

Ok, plugin instalado, exemplos rodados e tudo perfeito. Mas na hora de codificar o meu processo, ele não funcionava. Mexi em tudo, testei para não ver se era erro do código mesmo, mas nada.

Primeiro erro: Silenciei a exception gerada. Depois de consertar isso, o log me mostrou a exception linda, do jeito que eu queria.

Segundo erro: Passei um model como parâmetro, direto do controller pro agendador dos jobs. Mesmo sendo tudo código ruby, ele serializa e deserializa depois, o que fez o objeto ficar estranho e parar de responder aos métodos.

Solução: Salve o model na base, passe só o id para o agendador do Background-fu e, uma vez dentro do job, dê um find do model e chame os métodos que você quiser.

Rolling with Rails 2.1 - The Translation

Just translated Fabio Akita’s Rolling with Rails 2.1 - The First Full Tutorial - Part 1 to Portuguese.

The original is here. I will update the post with the link for the translation as soon as he launches it in his blog.

UPDATE:  Here is the translation in his blog.

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.

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!