Have you ever wanted to refactor something into the model ( skinny controller, fat model ) but didn’t know how to deal with your flash notifications?

Here’s a solution that I cooked up:


module FlashExceptions
  class FlashException < StandardError; end
  class FlashError < FlashException; end
  class FlashNotice < FlashException; end
  class FlashSuccess < FlashException; end
end

###########################################

class Model < ActiveRecord::Base
  include FlashExceptions

  def self.do_something! opts = {}
    raise FlashError, opts[:error] if opts[:error]
    raise FlashNotice, opts[:notice] if opts[:notice]
    raise FlashSuccess, opts[:success] if opts[:success]
  end

end

###########################################

class Controller < ApplicationController

  def action
    Model.do_something! :error => 'oops!', :notice => 'ahem.', :success => 'yay!'
  rescue FlashException => e
    flash[ e.class.underscore.split('_').last ] = e.message
  end

end

actionmailer ActionView ActiveRecord activesupport agile ajax alphadecimal audio autotest BDD blocks capistrano ssh ruby console controller css dashboard widget delegate dog puppy naming name DRM email obfuscation exceptions factories factories-and-workers factory pattern filemerge find and replace finder fink fixtures fun functional testing gem git google maps haml helper helpers imagemagick Intertrust irb javascript jquery jrails logo macro math meetup model openssl OS X patch Pioneer Electronics plugin polymorphism project management prototype.js rails rails gotcha rails,patch,validations railsconf rake rmagick RSA encryption ruby ruby on rails script scrum scrum lessons rails scrumninja shoulda subversion SyncTV TDD testing textmate tips tricks unique hashes unix shell validation view yaml zebra stripes