This post is by Robby Russell
from Robby on Rails
Click here to view on the original site: Original Post
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:
<p class="message">You have successfully done XYZ...</p>
Or, should you have called
add_error, it’d render the following:
<p class="error">Oops! Something went bonkers!</p>
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.
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. 🙂