unbound imagination - a blog about programming

Posts tagged with "rails"

«

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.

Rails Plugin: Simple Nav

May 04, 2008

So I've been on kind of a plugin creating binge in the past few weeks, but I promise this is the last one in this stretch. Basically, I took my blog and some other projects I've been working recently and extract the things that I felt were plugin worthy because either they are generic enough to be used on almost every site (this navigation helper and the javascript dependency helper) or it was not super specific and could be used by others the TextMate Syntax Highlighting plugin.

Anyway, this plugin is super simple to configure. It aims to centralize the navigation related logic such as which links a logged in/out user sees and when a certain link gets marked as "selected". It's broken down into 3 components for configuration:

  1. The Links that your site has for navigation (all of them, regardless if they are going to show up on every request)
  2. The order to display them for this request. This order can be any subset of the above list.
  3. The process to select the tab that is currently marked with the "selected" css class. (this part is broken into a basic and advanced version)

There is extensive documentation to explain all of the configuration in the README and the nav_helper that you can generate using script/generate.

It's hosted on GitHub at http://github.com/arya/simple_nav. As always, contributions and suggestions are highly encouraged.