2008-09-17

I like it when a site tells me that they’re going to do some maintenance, so I wanted to implement similar features for a web application that I’m working on. I based my announcements on Ryan Bates’ Railscasts episode 103 and updated code based on avarhirion’s post on the Rails Forum. The major changes are the added changes for cookie support, graceful degradation, and jQuery (jGrowl).

If you’d rather, check out all the files on Github.

announcement.rb:

Liquid error: No such file or directory – posix_spawnp

application.html.haml:

Liquid error: No such file or directory – posix_spawnp

routes.rb:

Liquid error: No such file or directory – posix_spawnp

Here’s the first of the important ones: javascripts_controller.rb. Note that I’m setting the session and a cookie. Also see my TODO. I’d like to set the expiration date of the cookie to the oldest expiring active announcement.

Liquid error: No such file or directory – posix_spawnp

In the application_helper.rb, we check the session first and set the announcements, otherwise, we check the cookies.

Liquid error: No such file or directory – posix_spawnp

Finally, to make it with the cool-kids, we need to add some jQuery. So I decided to use the jGrowl plugin because it’s awesome. This code goes in your application.js file (somewhere in $(function){ //here })

Liquid error: No such file or directory – posix_spawnp

Obviously, you’ll need to include jGrowl js and css files.

I like this solution because it provides a great looking way to do site-wide announcements. The solution degrades gracefully if the user does not have javascript enabled. Finally, by saving to a cookie, users can leave, close their browsers, and come back as the please and not see the same announcements over and over again.

I’d like to leave you with a couple of specs that I wrote. I attempted to do TDD/BDD and do the specs first, but it was tough. I didn’t get too far, as I was having some cookie/session problems. They’re in some serious need of help, so if you know cool stuff, help me out in the comments, thanks.

application_helper_spec.rb:

Liquid error: No such file or directory – posix_spawnp

announcement_spec.rb:

Liquid error: No such file or directory – posix_spawnp

Any feedback is appreciated.

Show more