<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Robby on Rails: Flash Message Conductor</title>
    <link>http://www.robbyonrails.com/articles/2008/08/29/flash-message-conductor</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>thoughts.sort_by{|t| t[:topic]}.collect </description>
    <item>
      <title>Flash Message Conductor</title>
      <description>&lt;p&gt;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 &lt;em&gt;reusable&lt;/em&gt; components tend to get out of sync when we bounce between projects. So, we&amp;#8217;re making an effort to spot these and are creating a handful of plugins so that we can keep them updated between projects. (I&amp;#8217;m sure that a lot of you do this as well)&lt;/p&gt;


	&lt;p&gt;In an effort to share some of our patterns, we&amp;#8217;ll try to release them into the wild for others to use and perhaps if you have better patterns to offer, we&amp;#8217;re always interested in improving our approach.&lt;/p&gt;


	&lt;h2&gt;Introducing Flash Message Conductor&lt;/h2&gt;


	&lt;p&gt;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.&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="ident"&gt;flash&lt;/span&gt;&lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="symbol"&gt;:message&lt;/span&gt;&lt;span class="punct"&gt;]&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;You have successfully signed in to your account.&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;What we began doing a while back is to create a few controller helper methods:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="ident"&gt;add_message&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;You have successfully signed in to your account.&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt; &lt;span class="punct"&gt;)&lt;/span&gt;
&lt;span class="ident"&gt;add_notice&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;You've Got Mail!&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt; &lt;span class="punct"&gt;)&lt;/span&gt;
&lt;span class="ident"&gt;add_error&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;Oops! Something got fucked up!&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt; &lt;span class="punct"&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Really, nothing too crazy here, just a pattern that our developers have preferred to managing our application&amp;#8217;s flash messages.&lt;/p&gt;


	&lt;p&gt;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 &lt;span class="caps"&gt;HTML&lt;/span&gt;/CSS designers to define a consistent pattern, we moved our code into a helper for rendering flash messages.&lt;/p&gt;


	&lt;p&gt;With Flash Message Conductor, we just need to pop in the following into our application layout.&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="punct"&gt;&amp;lt;%=&lt;/span&gt;&lt;span class="string"&gt; render_flash_messages %&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;If we had called &lt;code&gt;add_message&lt;/code&gt;, it&amp;#8217;d render the following:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_xml "&gt;&lt;span class="punct"&gt;&amp;lt;&lt;/span&gt;&lt;span class="tag"&gt;div&lt;/span&gt; &lt;span class="attribute"&gt;id&lt;/span&gt;&lt;span class="punct"&gt;=&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;flash_messages&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&amp;gt;&lt;/span&gt;
  &lt;span class="punct"&gt;&amp;lt;&lt;/span&gt;&lt;span class="tag"&gt;p&lt;/span&gt; &lt;span class="attribute"&gt;class&lt;/span&gt;&lt;span class="punct"&gt;=&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;message&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&amp;gt;&lt;/span&gt;You have successfully done XYZ...&lt;span class="punct"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="tag"&gt;p&lt;/span&gt;&lt;span class="punct"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="punct"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="tag"&gt;div&lt;/span&gt;&lt;span class="punct"&gt;&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Or, should you have called &lt;code&gt;add_error&lt;/code&gt;, it&amp;#8217;d render the following:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_xml "&gt;&lt;span class="punct"&gt;&amp;lt;&lt;/span&gt;&lt;span class="tag"&gt;div&lt;/span&gt; &lt;span class="attribute"&gt;id&lt;/span&gt;&lt;span class="punct"&gt;=&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;flash_messages&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&amp;gt;&lt;/span&gt;
  &lt;span class="punct"&gt;&amp;lt;&lt;/span&gt;&lt;span class="tag"&gt;p&lt;/span&gt; &lt;span class="attribute"&gt;class&lt;/span&gt;&lt;span class="punct"&gt;=&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;error&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&amp;gt;&lt;/span&gt;Oops! Something went bonkers!&lt;span class="punct"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="tag"&gt;p&lt;/span&gt;&lt;span class="punct"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="punct"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="tag"&gt;div&lt;/span&gt;&lt;span class="punct"&gt;&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

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


	&lt;h3&gt;Installing Flash Message Conductor&lt;/h3&gt;


	&lt;p&gt;Like most &lt;em&gt;modern&lt;/em&gt; Rails applications, you can install with:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
script/plugin install git://github.com/planetargon/flash-message-conductor.git
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Then all of our helper methods will be available to your application. We&amp;#8217;ve also included an example &lt;span class="caps"&gt;CSS&lt;/span&gt; file, which you&amp;#8217;ll find in the plugin directory.&lt;/p&gt;


	&lt;p&gt;Sample output:&lt;/p&gt;


&lt;div class="thumbnail"&gt;&lt;a href="http://skitch.com/robbyrussell/wuef/flash-message-area"&gt;&lt;img src="http://img.skitch.com/20080830-n8k8ikkk3i8himuxhk7pbf8tg3.preview.jpg" alt="flash message area" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;span style="font-family: Lucida Grande, Trebuchet, sans-serif, Helvetica, Arial; font-size: 10px; color: #808080"&gt;Uploaded with &lt;a href="http://plasq.com/"&gt;plasq&lt;/a&gt;&amp;#8217;s &lt;a href="http://skitch.com"&gt;Skitch&lt;/a&gt;!&lt;/span&gt;&lt;/div&gt;

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


	&lt;p&gt;For more information, visit the &lt;a href="http://github.com/planetargon/flash-message-conductor"&gt;Flash Message Conductor plugin&lt;/a&gt; on GitHub.&lt;/p&gt;


	&lt;p&gt;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&amp;#8217;s own reusable plugin. :-)&lt;/p&gt;
</description>
      <pubDate>Fri, 29 Aug 2008 16:35:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:95e800a7-f1a5-429b-94be-aed635f73036</guid>
      <author>Robby Russell</author>
      <link>http://www.robbyonrails.com/articles/2008/08/29/flash-message-conductor</link>
      <category>Ruby on Rails</category>
      <category>Ruby</category>
      <category>Programming</category>
      <category>PLANET ARGON</category>
      <category>patterns</category>
      <category>pattern</category>
      <category>rubyonrails</category>
      <category>rails</category>
      <category>development</category>
      <category>html</category>
      <category>css</category>
      <category>team</category>
      <category>planetargon</category>
    </item>
  </channel>
</rss>
