The scenario: Its 3AM. You've been coding all day and youre just about ready to launch this sweet social networking app for steel workers who love to dance. One last thing - you need to replace all the crappy copy you stuck in to your 'flash' messages with real informative text. "sucesfil post" might make sense to you, but wont make sense to the blue collar guy doing the electric slide all over your server. You go to replace these warnings and feedback messages but theyre all over your code. You think: If Rails is so DRY and the business and display logic are all supposed to be separate, then why am I putting messages that will end up in my views all through my controllers?!
The get-the-view-out-of-the-controller-so-you-can-rest plugin.
Instead of all-up-in your controller files, messages are stored in a single YAML file:
# in app/views/shared/flash.yml all_is_good: This is great this_failed: This failed using_erb: you are an idiot, <%= @email %> subsection: I'm nested message: I'm soaking wet
At its most unobtrusive:
# in your controller # . . . booring part where record gets updated flash[:warning] = flashdance(:this_failed) #=> This failed end
A little nicer:
# . . . flash(:message => 'all_is_good') # YAML entries can be referred to by strings or symbols # equivalent to flash[:message] = "This is great" end
ERB is evaluated in the context of the controller so you can do fun things like:
# . . . @email = 'email@example.com' flash(:warning => :using_erb) # equivalent to flash[:warning] = "you are an idiot, firstname.lastname@example.org" end
So what if your app is huge and you use these flash thingies all over the place? Your YAML file might get big and unreadable. Well, thats why you can nest! Nested sections are refered to by arrays.
# . . . flash(:nest => [:section,:subsection]) # equivalent to flash[:nest] = "I'm nested" end
Flashdance automatically checks for entries nested in the controller/action path where you call it from. And heres where you get splashed with that big bucket of water:
./script/plugin install -x svn://svn.quirkey.com/quirkey_tools/trunk/flashdance
or Download: flashdance.tar.gz
You can email me at aaron at this domain for bug reports/comments/suggestions - and I'm looking to set up Trac or something else soon.
Photo credit: http://www.flickr.com/photos/tzofia/185003069/