Archive for April, 2009

Searching across all opened buffers in emacs with moccur

Posted in Uncategorized on April 30th, 2009 by Marcelo de Moraes Serpa – Be the first to comment

You probably went through this situation if you use emacs: Zillions of buffers opened and you need to check out a specific code that you know is in a file that is opened in a buffer somewhere. You have two options: 1) Remember the buffer name and switch to it; 2) rgrep through your files and, find the file so you can switch to the buffer (not optimal). Well, you have a third option actually — you can use the moccur extension and search across your current jungle of buffers.

Here’s the link to the elisp script: http://www.bookshelf.jp/elc/moccur.el.

To install, save it in a place that is visible to emacs (added to the load-path variable), add the following line to the end of moccur.el:

    (provide 'moccur)

Then, in your .emacs, add this line:

    (require 'moccur)

To use it, type M-x moccur, enter the search string and be delighted :)

You can use C-h f moccur to get more help.

Adapting ActionMailer to use a custom home-made templating system

Posted in Uncategorized on April 30th, 2009 by Marcelo de Moraes Serpa – Be the first to comment

We required a simple templaing system for our emails and a way to associate these templates with mailer actions. At first, I though getting the name of the action in Mailer (ActionMailer::Base), but it is not possible — ActionMailer is one of a kind, it’s not a controller and does not have the functionalities of ActionController::Base.

The templating system actually needs to allow the user to access attributes from models of the application and also be able to insert html “widgets” (pieces of hmtl data), both using tags. What we did was to create a EmailParser helper class (which could be a module, but this seemed simpler) and some singleton methods to parse the tags. For each tag, EmailParser checks in the passed in model instance and calls it’s content string as a method, to get the attribute. There is a security risk here,  but since this is available only in the backend of the application, we can set up to solve this in a later iteration. Then you can also pass a “view helper”, which is supposed to be a class or module with methods that return complex html structures. We plan on extending this to make it more flexible and robust, or maybe use redcloth or another existing solution, but this does the job for now.

We have several points in the application where we send emails. We need a way to identify each of these points. The first alternative is obvious – stick to ActionMailer and use the action defs. And this is what I did, I created a MailerAction model that has a name of an action def from the main application mailer (app/models/mailer.rb). This model belongs_to EmailTemplate, meaning that each action is associated to a template. An EmailTemplate, in its turn, may be associated to many actions, so EmailTEmplate has_many :mailer_actions.

So here’s a sample of how a mailer action def would look like:

def mailer_action(model_instance,  subject, sender)

message = MailerAction.find_by_name('mailer_action').email_template.template_for_object(model_instance)

from sender
recipients account.email
subject subject
body  :model => model_instance, :message => message
template 'message' #explicitly  specify the template to use here!

end

Note the template method from the DSL, it says tells ActionMailer to use a template with a name different from the action name. This is crucial for this system to work.

The bad thing here is that I have to repeat this logic for every mailer action. Of course, I could encapsulate and abstract some of it in a module (helper) or a simple private method, but I don’t think the cost-benefict would worth it in the current iteration (since there are not that many email actions using the new templating system right now).

Now, we use only one erb template that then renders the message for us:

app/views/mailer/message.html.erb

<%= @message %>

Not the most elegant solution, but works :)

In my next post I will explain how the EmailParser class works, so you can get the missing piece to understand how this particular feature works.

Emacs backup files and Rails woes

Posted in Uncategorized, rails on April 27th, 2009 by Marcelo de Moraes Serpa – 2 Comments

Today I was modifying an erb view and no matter what I did, the changes wouldn’t be reflected in the browser. First I thougtht it was a cache issue, checked the cache_classes configuration option in config/environments/development.rb. No, set to false, so the views were supposed to be refreshed without the need to restart the server.

“It might be the browser cache” — I quickly thought. Cleared the cache and the view still didn’t reflect the changes.

What the hell? I was starting to feel my head hot. Well, the thing was that Rails was loading an old version of the view file, specifically a file that emacs creates as a backup file, with the extension .~n~ where n is a number. The solution? Delete the file! I don’t know why it happens, didn’t have time to dig into that yet, but here’s this post in the hope it will save’s someone else’s precious time.

By the way, I’m using Rails 2.3.2. Might not happen in an older version, or might be fixed in a next release.

Fighting inertia – My first post!

Posted in café on April 27th, 2009 by Marcelo de Moraes Serpa – Be the first to comment

Well, if you stumbled upon this blog, congratulations! You won’t find anything… yet. This is my first post, and I needed to do that to kill the inertia — the hardest part of starting a blog is well… starting the blog. But don’t worry, I wi (hopefully) be blogging about lots of interesting things here, since I have a particular passion a computer science, graphics design, color theory, music, women and alcohol, you’ll surely find something that fits your particular tastes.

I’m currently professionally working with Ruby and Ruby on Rails on a big e-commerce website, so, no doubt I will be blogging a lot about the sweet Ruby language and also Rails. Also, emacs, Linux and how to feed your lazyness by automating more of your environment will be one of the focus of this blog.

Anyway, hope you come back in a few days and check it out regularly, I look forward to sharing some interesting stuff here!