Read my latest article: Launching Rails projects, an open call for lessons learned (posted Tue, 23 Jun 2009 17:33:00 GMT)

Flash Message Conductor

Posted by Robby Russell Fri, 29 Aug 2008 21:35:00 GMT

15 comments Latest by Ogredude Tue, 20 Jan 2009 18:41:13 GMT

Do you find yourself copying and pasting the same code from Rails application-to-application as new projects start? Our team has a handful of projects in development right now and we notice that some of these reusable components tend to get out of sync when we bounce between projects. So, we’re making an effort to spot these and are creating a handful of plugins so that we can keep them updated between projects. (I’m sure that a lot of you do this as well)

In an effort to share some of our patterns, we’ll try to release them into the wild for others to use and perhaps if you have better patterns to offer, we’re always interested in improving our approach.

Introducing Flash Message Conductor

Over the years, our designers and developers have approached the management of flash messages several different ways. In Rails, the default way to add something to a flash message is to do something like this in your controller.

flash[:message] = "You have successfully signed in to your account."

What we began doing a while back is to create a few controller helper methods:

add_message( "You have successfully signed in to your account." )
add_notice( "You've Got Mail!" )
add_error( "Oops! Something got fucked up!" )

Really, nothing too crazy here, just a pattern that our developers have preferred to managing our application’s flash messages.

Okay, so now for the part of the puzzle that we aimed to make consistent across our projects. Rendering flash messages would usually result in several lines of conditionals in our application layout to check if the flash had any values assigned to it. As we worked with our HTML/CSS designers to define a consistent pattern, we moved our code into a helper for rendering flash messages.

With Flash Message Conductor, we just need to pop in the following into our application layout.

<%= render_flash_messages %>

If we had called add_message, it’d render the following:

<div id="flash_messages">
  <p class="message">You have successfully done XYZ...</p>
</div>

Or, should you have called add_error, it’d render the following:

<div id="flash_messages">
  <p class="error">Oops! Something went bonkers!</p>
</div>

What we’ve done here is defined a consistent pattern for our designers and developers to follow. We’ll always have a div container that will use a p tag to display the flash messages with a CSS class value that maps to the type of flash message that we’re displaying. This makes it easier for us to reuse the same flash message styling (and tweak if necessary), but we know that it’ll produce the same HTML across our applications.

Installing Flash Message Conductor

Like most modern Rails applications, you can install with:


script/plugin install git://github.com/planetargon/flash-message-conductor.git

Then all of our helper methods will be available to your application. We’ve also included an example CSS file, which you’ll find in the plugin directory.

Sample output:

flash message area
Uploaded with plasq’s Skitch!

Anyhow, we’ve posted the plugin up on GitHub for you all to use, if you’d like to adopt a similar approach. If you have any alternative patterns that has helped your team, do share and I’m looking forward to sharing some more of ours in the near future.

For more information, visit the Flash Message Conductor plugin on GitHub.

If anything, hopefully this will inspire those of you who find yourself copying/pasting artifacts from application-to-application to extract that code into it’s own reusable plugin. :-)

Subscribe to my RSS feed Enjoying the content? Be sure to subscribe to my RSS feed.
Comments

Leave a response

  1. Avatar
    Jason Fri, 29 Aug 2008 23:18:46 GMT

    While not the most advanced plugin that I have seen, it does save time when working across several projects. I’d guesstimate that our team copy & pastes about 10-15 various components from projects with one of them being flash helpers similar to yours. You’re right, it’s a good idea to consider breaking things down into smaller pieces and making them easy to reuse.

    Thanks for the idea!

  2. Avatar
    Lar Van Der Jagt Sat, 30 Aug 2008 01:41:25 GMT

    I have a personal plugin where I store things like this as well. I’ve taken a slightly different approach that attempts to combine the flash with error_messages_for. I’m not sure I like how it works anymore, but I agree that simple plugins to store this kind of code is a good way to go.

    You can also use multiple class definitions on the parent div, so the class would look something like class=”flash message” or class=”flash error”. That lets you style all flash messages one way, with slight differences for each type of message.

  3. Avatar
    James Chan Sat, 30 Aug 2008 04:53:52 GMT

    It’s a great plugin. It would be even better if it outputs nothing instead of an empty div tag when there’s no error or message set.

  4. Avatar
    Robby Russell Sat, 30 Aug 2008 05:30:58 GMT Recommend me on Working with Rails

    James,

    Doh! I thought that I covered that. Just added a patch for that. :-)

  5. Avatar
    Robby Russell Sat, 30 Aug 2008 05:32:47 GMT Recommend me on Working with Rails

    @Lar Van Der Jagt,

    We actually specify the class on the paragraph tag because we sometimes have multiple types of flash messages to render. For example, we might show a notice and a message at the same time (with different colors to highlight them individually). It’s fairly rare that we have this happen… but it has.

  6. Avatar
    Korepetycje Sat, 30 Aug 2008 11:43:36 GMT

    usefull plugin! Thank you!

  7. Avatar
    Nikolay Kolev Mon, 01 Sep 2008 00:51:02 GMT

    Why are you using P tags instead of DIV or UL with LI ones?

  8. Avatar
    grosser Mon, 01 Sep 2008 11:21:11 GMT

    nice plugin, simple but useful, will definetly go into my next project

  9. Avatar
    Brett Tue, 02 Sep 2008 02:47:22 GMT

    Robby,

    I like the simplicity here and honestly, this is much better than what our team is currently using. I’d really like to see more simple plugins get released.

  10. Avatar
    Brett Tue, 02 Sep 2008 02:49:43 GMT

    Nikolay Kolev,

    I’m not sure that a list element would be the best to render flash messages. I suppose it depends on how you’re showing them, but these are messages and I’d argue that a paragraph tag is very appropriate given the content. Unless you’re confusing this with the output of error_messages_for, which this plugin doesn’t address and I’m sure many of us would be interested in hearing how Planet Argon handles those in their views.

  11. Avatar
    George Tue, 02 Sep 2008 07:21:26 GMT

    Thanks Robby, really useful yet simple.

  12. Avatar
    Allison Beckwith Tue, 02 Sep 2008 18:38:04 GMT

    Hi Nikolay,

    We are using p tags because it is the appropriate element for the type of content it contains. A paragraph is a block level element for holding text, like the sentences of a flash message. The paragraphs are contained inside a div, which is a “general purpose” block level element used for dividing up content and creating structure. The general rule of thumb here at PA is that p tags are used for text elements, and divs are used for structural elements.

  13. Avatar
    ActsAsFlinn Fri, 19 Sep 2008 00:59:05 GMT

    Nice stuff here. I’ve done something like this for a while, keying the flash message and classing the flash to show message context. Here’s a simple example:

    http://snippy.actsasflinn.com/snippets/14

    In the past I’ve even shown context icons along with the message.

  14. Avatar
    Jerry Cheung Sun, 14 Dec 2008 08:45:57 GMT

    This is a sweet and simple plugin. It helps organize my code, and I use it to keep fancy logic about when to show, hide, and animate the flash in my app.

    I’ve been following your blog for a while now. I really enjoy the Postgresql posts you come with. Keep up the good work!

  15. Avatar
    Ogredude Tue, 20 Jan 2009 18:41:13 GMT

    I extended the Flash Message Conductor to allow for use of the flash.new method.

    Really simple and I’m sure it could have been done prettier, but here it is.

    http://pastie.org/365857

Share your thoughts... (really...I want to hear them)

Comments