2004-11-23 20:04:44 -05:00
|
|
|
module ActionController #:nodoc:
|
|
|
|
# The flash provides a way to pass temporary objects between actions. Anything you place in the flash will be exposed
|
2007-09-09 17:54:59 -04:00
|
|
|
# to the very next action and then cleared out. This is a great way of doing notices and alerts, such as a create
|
|
|
|
# action that sets <tt>flash[:notice] = "Successfully created"</tt> before redirecting to a display action that can
|
|
|
|
# then expose the flash to its template. Actually, that exposure is automatically done. Example:
|
2004-11-23 20:04:44 -05:00
|
|
|
#
|
|
|
|
# class WeblogController < ActionController::Base
|
|
|
|
# def create
|
|
|
|
# # save post
|
2005-10-16 11:42:03 -04:00
|
|
|
# flash[:notice] = "Successfully created post"
|
|
|
|
# redirect_to :action => "display", :params => { :id => post.id }
|
2004-11-23 20:04:44 -05:00
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def display
|
|
|
|
# # doesn't need to assign the flash notice to the template, that's done automatically
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
2007-02-20 17:09:12 -05:00
|
|
|
# display.erb
|
2006-08-07 02:11:56 -04:00
|
|
|
# <% if flash[:notice] %><div class="notice"><%= flash[:notice] %></div><% end %>
|
2004-11-23 20:04:44 -05:00
|
|
|
#
|
2007-09-09 17:54:59 -04:00
|
|
|
# This example just places a string in the flash, but you can put any object in there. And of course, you can put as
|
|
|
|
# many as you like at a time too. Just remember: They'll be gone by the time the next action has been performed.
|
2005-03-20 14:12:53 -05:00
|
|
|
#
|
|
|
|
# See docs on the FlashHash class for more details about the flash.
|
2004-11-23 20:04:44 -05:00
|
|
|
module Flash
|
2006-02-12 00:51:02 -05:00
|
|
|
def self.included(base)
|
|
|
|
base.class_eval do
|
2007-10-02 01:32:14 -04:00
|
|
|
include InstanceMethods
|
2006-04-29 16:20:22 -04:00
|
|
|
alias_method_chain :assign_shortcuts, :flash
|
2006-09-03 19:22:24 -04:00
|
|
|
alias_method_chain :process_cleanup, :flash
|
|
|
|
alias_method_chain :reset_session, :flash
|
2006-02-12 00:51:02 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2005-03-20 14:12:53 -05:00
|
|
|
class FlashNow #:nodoc:
|
2006-02-12 01:11:17 -05:00
|
|
|
def initialize(flash)
|
2005-03-20 14:12:53 -05:00
|
|
|
@flash = flash
|
|
|
|
end
|
|
|
|
|
|
|
|
def []=(k, v)
|
|
|
|
@flash[k] = v
|
|
|
|
@flash.discard(k)
|
|
|
|
v
|
|
|
|
end
|
2005-10-26 09:14:10 -04:00
|
|
|
|
|
|
|
def [](k)
|
|
|
|
@flash[k]
|
|
|
|
end
|
2005-03-20 14:12:53 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class FlashHash < Hash
|
|
|
|
def initialize #:nodoc:
|
|
|
|
super
|
|
|
|
@used = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def []=(k, v) #:nodoc:
|
|
|
|
keep(k)
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2006-02-12 00:51:02 -05:00
|
|
|
def update(h) #:nodoc:
|
2007-05-06 00:17:01 -04:00
|
|
|
h.keys.each { |k| keep(k) }
|
2005-03-20 14:12:53 -05:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2006-02-12 00:51:02 -05:00
|
|
|
alias :merge! :update
|
2005-03-20 14:12:53 -05:00
|
|
|
|
2006-02-12 00:51:02 -05:00
|
|
|
def replace(h) #:nodoc:
|
2005-03-20 14:12:53 -05:00
|
|
|
@used = {}
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
# Sets a flash that will not be available to the next action, only to the current.
|
|
|
|
#
|
2005-10-16 11:42:03 -04:00
|
|
|
# flash.now[:message] = "Hello current action"
|
2005-03-20 14:12:53 -05:00
|
|
|
#
|
|
|
|
# This method enables you to use the flash as a central messaging system in your app.
|
|
|
|
# When you need to pass an object to the next action, you use the standard flash assign (<tt>[]=</tt>).
|
|
|
|
# When you need to pass an object to the current action, you use <tt>now</tt>, and your object will
|
|
|
|
# vanish when the current action is done.
|
|
|
|
#
|
|
|
|
# Entries set via <tt>now</tt> are accessed the same way as standard entries: <tt>flash['my-key']</tt>.
|
|
|
|
def now
|
2007-09-09 17:54:59 -04:00
|
|
|
FlashNow.new(self)
|
2005-03-20 14:12:53 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Keeps either the entire current flash or a specific flash entry available for the next action:
|
|
|
|
#
|
|
|
|
# flash.keep # keeps the entire flash
|
2005-10-16 11:42:03 -04:00
|
|
|
# flash.keep(:notice) # keeps only the "notice" entry, the rest of the flash is discarded
|
2006-09-03 19:22:24 -04:00
|
|
|
def keep(k = nil)
|
2005-03-20 14:12:53 -05:00
|
|
|
use(k, false)
|
|
|
|
end
|
|
|
|
|
2007-08-31 15:07:42 -04:00
|
|
|
# Marks the entire flash or a single flash entry to be discarded by the end of the current action:
|
2005-03-20 14:12:53 -05:00
|
|
|
#
|
2007-05-28 22:42:31 -04:00
|
|
|
# flash.discard # discard the entire flash at the end of the current action
|
|
|
|
# flash.discard(:warning) # discard only the "warning" entry at the end of the current action
|
2006-09-03 19:22:24 -04:00
|
|
|
def discard(k = nil)
|
2005-03-20 14:12:53 -05:00
|
|
|
use(k)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Mark for removal entries that were kept, and delete unkept ones.
|
|
|
|
#
|
|
|
|
# This method is called automatically by filters, so you generally don't need to care about it.
|
|
|
|
def sweep #:nodoc:
|
|
|
|
keys.each do |k|
|
|
|
|
unless @used[k]
|
|
|
|
use(k)
|
|
|
|
else
|
|
|
|
delete(k)
|
|
|
|
@used.delete(k)
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2006-09-03 19:22:24 -04:00
|
|
|
|
2007-09-09 17:54:59 -04:00
|
|
|
# clean up after keys that could have been left over by calling reject! or shift on the flash
|
|
|
|
(@used.keys - keys).each{ |k| @used.delete(k) }
|
2005-03-20 14:12:53 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2005-04-17 07:38:01 -04:00
|
|
|
# Used internally by the <tt>keep</tt> and <tt>discard</tt> methods
|
|
|
|
# use() # marks the entire flash as used
|
|
|
|
# use('msg') # marks the "msg" entry as used
|
|
|
|
# use(nil, false) # marks the entire flash as unused (keeps it around for one more action)
|
|
|
|
# use('msg', false) # marks the "msg" entry as unused (keeps it around for one more action)
|
|
|
|
def use(k=nil, v=true)
|
|
|
|
unless k.nil?
|
|
|
|
@used[k] = v
|
|
|
|
else
|
2007-09-09 17:54:59 -04:00
|
|
|
keys.each{ |key| use(key, v) }
|
2005-04-17 07:38:01 -04:00
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2005-03-20 14:12:53 -05:00
|
|
|
end
|
2005-11-21 13:09:28 -05:00
|
|
|
|
2006-03-27 22:19:27 -05:00
|
|
|
module InstanceMethods #:nodoc:
|
2007-03-12 22:14:31 -04:00
|
|
|
protected
|
|
|
|
def reset_session_with_flash
|
|
|
|
reset_session_without_flash
|
|
|
|
remove_instance_variable(:@_flash)
|
|
|
|
flash(:refresh)
|
|
|
|
end
|
2006-02-12 00:51:02 -05:00
|
|
|
|
|
|
|
# Access the contents of the flash. Use <tt>flash["notice"]</tt> to read a notice you put there or
|
|
|
|
# <tt>flash["notice"] = "hello"</tt> to put a new one.
|
|
|
|
# Note that if sessions are disabled only flash.now will work.
|
2006-02-12 01:11:17 -05:00
|
|
|
def flash(refresh = false) #:doc:
|
2006-08-07 02:11:56 -04:00
|
|
|
if !defined?(@_flash) || refresh
|
|
|
|
@_flash =
|
|
|
|
if session.is_a?(Hash)
|
|
|
|
# don't put flash in session if disabled
|
2006-02-12 01:11:17 -05:00
|
|
|
FlashHash.new
|
|
|
|
else
|
2006-08-07 02:11:56 -04:00
|
|
|
# otherwise, session is a CGI::Session or a TestSession
|
2006-02-12 01:11:17 -05:00
|
|
|
# so make sure it gets retrieved from/saved to session storage after request processing
|
2006-08-07 02:11:56 -04:00
|
|
|
session["flash"] ||= FlashHash.new
|
2006-02-12 01:11:17 -05:00
|
|
|
end
|
|
|
|
end
|
2006-08-07 02:11:56 -04:00
|
|
|
|
|
|
|
@_flash
|
2006-02-12 00:51:02 -05:00
|
|
|
end
|
2005-11-21 13:09:28 -05:00
|
|
|
|
2007-03-12 22:14:31 -04:00
|
|
|
private
|
|
|
|
def assign_shortcuts_with_flash(request, response) #:nodoc:
|
|
|
|
assign_shortcuts_without_flash(request, response)
|
|
|
|
flash(:refresh)
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_cleanup_with_flash
|
|
|
|
flash.sweep if @_session
|
|
|
|
process_cleanup_without_flash
|
|
|
|
end
|
2006-02-12 00:51:02 -05:00
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2007-10-02 01:32:14 -04:00
|
|
|
end
|