Gmane
From: Luke Randall <luke.randall@...>
Subject: RFC: Standardising flash usage amongst Rails applications & generators
Newsgroups: gmane.comp.lang.ruby.rails
Date: 2005-11-04 15:12:15 GMT (2 years, 49 weeks, 1 day, 4 hours and 53 minutes ago)
Hi all

This is (hopefully) a more coherent version of what I posted in reply
to James' announcement of rails-engines.

With the expected increase in pluggable apps (in the form of engines),
as well as the increasing number of generators being made available, I
would suggest that the community standardise on a few basic flash
names. The utility in doing this, as far as I see, is to make new
engines entirely pluggable, as illustrated by the following.

I was playing around with the new login engine, and dropped it into an
existed application. Within this app (and indeed in all Rails apps I
develop), I check in the flash for flash[:notice] and flash[:warning]
and display them if they exist. The reason for this distinction is so
that normal status messages (:notice) are displayed in one way - an
unobtrusive green background - whilst errors and such (:warning) are
displayed on a more prominent red background. However, in the salted
login generator (and by extension the login engine), the code uses
notice and message instead. Therefore, I had to change all the
occurrences of message with warning to get the engine to play nicely
with my app. Moreover, other apps (probably - I can't cite any
examples off-hand) use other names. Likewise, other applications add
all messages to notice (login generator for example), which makes it
impossible to distinguish between different types of flash without
going through and changing the code yourself, which somewhat negates
the usefulness of these.

What I propose is that a standard be established for flash names, such
that if you check for and display the standard flash names in your
layout, any engines or generators you use in your app will
automatically fit into your layout. I certainly don't want to make
supporting this an onerous burden on the part of developers, so I
think that it would be wise to limit these to just a few key
categories. Based on my own experiences, I'd suggest something along
the lines of the following (the names are the best I can think of,
anyone is welcome to suggest better ones):

  :notice for positive feedback (action successful, etc)
  :message for neutral feedback (reminders, etc)
  :warning for negative feedback (action unsuccessful, error encountered, etc)

Then, in your controller or view you could place code such as the following:

  // FLASH_NAMES = [:notice, :warning, :message]

  <% for name in FLASH_NAMES %>
    <% if flash[name] %>
      <%= "<div id=\"#{name}\">#{flash[name]}</div>" %>
    <% end %>
  <% end %>

and thus gives you the ability to independently theme with CSS the
different flashes according to their nature. Alternately, if you don't
need (or want to) distinguish between the different flashes you can
choose to theme them the same way and thus not lose anything except a
FEW lines of extra code.

Naturally, my suggestions are influenced by how I use the flash, and
as such I might have overlooked a better way of doing things, or my
proposal might seem unnecessary. If this is the case, please someone
point me to the light!

Your comments?

Luke