unbound imagination - a blog about programming

Posts tagged with "tips"

«

Capistrano, meet Twitter: Keep your users updated through Twitter

June 04, 2008

I recently deployed a beta of an application I'm working on and I thought it'd be nice to keep users updated about any new features or updates through twitter. So I wrote a spiffy Rails plugin that just adds a capistrano recipe for updating twitter statuses when you deploy.

capistrano_twitter just prompts you for a status update when you deploy and then updates the twitter status for the specified user (in config/deploy.rb). Of course, if you leave the status blank, no update will be made.

Check it out for yourself: http://github.com/arya/capistrano_twitter

Javascript Dependency Manager and Google AJAX Libraries

May 29, 2008

On Tuesday, Google released a new service where they host popular javascript libraries such as prototype and jQuery. This allows you to link to their copy of the libraries instead of your own so you can reduce the load on your server and speed up the client's experience.

They plan on archiving all stable versions of each library and allowing you to choose which you want to link to. They also provide compressed versions of all the libraries except scriptaculous and prototype (I'm not sure why only those two are left out).

Anyway, I've added support for using the Google copy of these libraries into my javascript dependency manager plugin. To use it, upgrade to the newest version of the plugin and create an initializer with the following code:

   1  JavascriptHelper::JavascriptHelperConfig.use_google_ajax_libs = true

Now when you include prototype, jQuery or any of the other supported libraries using the javascript dependency manager plugin, it will refer to the Google hosted file. By default the Google hosted libraries are not used, so users who want to stick to their own ways can continue doing so without any changes.

You can specify library versions and preference for compressed versions in the initializer as well, view the readme for details on how to do that.

ActiveRecord AssociationTypeMismatch: User expected, got User

May 13, 2008

Look familiar? ActiveRecord, fortunately, tells you when you try to assign an object of the wrong class to an association so you'd get this error if you tried to assign a String as the Comment for a Post (which is the correct usage). But sometimes you'll get the error in this case:

   1  @comment = @question.comments.build(params[:comment])
   2  @comment.user = current_user # raises "User expected, got User"

Now just earlier today, I got this "User expected, got User" error message and it stumped for me a few minutes. I then realized it's because I changed the User class, which usually isn't a problem since Rails reloads your models in the development environment. But since the Comment class is in a plugin (actsascommentable) and plugins aren't reloaded every request, the Comment class was expecting the User class as-it-was when mongrel was started (and the plugin was loaded).

Sooo.... to save developers between 5 seconds to 5 hours, I added this message to the exception raised (only if the two classes have the same name):

   1  (did you change the User class? try restarting mongrel)

I wrote a patch for the changes to add the helpful message. It's super-simple, but hey, I gotta start somewhere. I also made a ticket on the Rails LightHouse project, hopefully it'll get pulled into Rails.

Update: I found this guide on contributing to rails and see that I'm not supposed to fork Rails in GitHub unless its for something big. So I deleted my fork.

Hiding e-mails from bots but not users

May 04, 2008

As most of you have probably seen somewhere on the Internet, web sites have started showing e-mails with the period and at-sign replaced with "[dot] and [at]" or "(dot) and (at)" or some derivation of that.

Anyway, I recently came up with a compromise that both protects e-mails from bot scraping but still shows the e-mail normally to the user. Using the Behaviour javascript library and PrototypeJS, I simply replace any span marked with the email class with its appropriate human usable link.

Imagine we had this in our source:

   1  <span class="email">joesmith <em>[at]</em> gmail <em>[dot></em> com</span>

and we used this Javascript to change it to something more user-friendly:

   1  var email_rules = {
   2    'span.email': function(el) {
   3      var email = new String(el.innerHTML).stripTags().strip();
   4      var hasPeriod = false;
   5      if (email.endsWith(".")) { hasPeriod = true; }
   6      // change this expression to match your e-mail hiding scheme
   7      email = email.sub(" \\[at\\] ", "@").sub(" \\[dot\\] ", ".").sub("\\.$", "");
   8      el.innerHTML = '<a href="mailto:' + email + '">' + email + "</a>" + (hasPeriod ? ".":"");
   9    }
  10  };
  11  
  12  Behaviour.register(email_rules);

Which will change what the user sees to

   1  <a href="mailto:joesmith@gmail.com">joesmith@gmail.com</a>

And that's it! Bots see the original and users with javascript enabled see the real email as a link.

Update: Considering bots have added things like [at] and [dot] to their list of things to search for, this trick doesn't really work. But nonetheless, using the Behaviour library to add usability/functionality to your UI is a cool trick that comes up handy quite often.