aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
authorJoshua Peek <josh@joshpeek.com>2008-12-15 20:33:21 -0600
committerJoshua Peek <josh@joshpeek.com>2008-12-15 20:43:01 -0600
commit95c839bd2aca1576a33a6503f0dd67266d53a353 (patch)
treeb25cf2eb1e05c33449b06701b4e3a34e823507da /actionpack
parent89056885b030b981193d73ebde56cf60829a924c (diff)
downloadrails-95c839bd2aca1576a33a6503f0dd67266d53a353.tar.gz
rails-95c839bd2aca1576a33a6503f0dd67266d53a353.tar.bz2
rails-95c839bd2aca1576a33a6503f0dd67266d53a353.zip
Session objects are always a hash, so we need to ensure a flash hash is always assigned to the session
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/lib/action_controller/flash.rb55
1 files changed, 20 insertions, 35 deletions
diff --git a/actionpack/lib/action_controller/flash.rb b/actionpack/lib/action_controller/flash.rb
index 62fa381a6f..6378a7f25b 100644
--- a/actionpack/lib/action_controller/flash.rb
+++ b/actionpack/lib/action_controller/flash.rb
@@ -31,51 +31,50 @@ module ActionController #:nodoc:
alias_method_chain :reset_session, :flash
end
end
-
-
+
class FlashNow #:nodoc:
def initialize(flash)
@flash = flash
end
-
+
def []=(k, v)
@flash[k] = v
@flash.discard(k)
v
end
-
+
def [](k)
@flash[k]
end
end
-
+
class FlashHash < Hash
def initialize #:nodoc:
super
@used = {}
end
-
+
def []=(k, v) #:nodoc:
keep(k)
super
end
-
+
def update(h) #:nodoc:
h.keys.each { |k| keep(k) }
super
end
-
+
alias :merge! :update
-
+
def replace(h) #:nodoc:
@used = {}
super
end
-
+
# Sets a flash that will not be available to the next action, only to the current.
#
# flash.now[:message] = "Hello current action"
- #
+ #
# 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
@@ -85,7 +84,7 @@ module ActionController #:nodoc:
def now
FlashNow.new(self)
end
-
+
# Keeps either the entire current flash or a specific flash entry available for the next action:
#
# flash.keep # keeps the entire flash
@@ -93,7 +92,7 @@ module ActionController #:nodoc:
def keep(k = nil)
use(k, false)
end
-
+
# Marks the entire flash or a single flash entry to be discarded by the end of the current action:
#
# flash.discard # discard the entire flash at the end of the current action
@@ -101,12 +100,12 @@ module ActionController #:nodoc:
def discard(k = nil)
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|
+ keys.each do |k|
unless @used[k]
use(k)
else
@@ -118,7 +117,7 @@ module ActionController #:nodoc:
# 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) }
end
-
+
private
# Used internally by the <tt>keep</tt> and <tt>discard</tt> methods
# use() # marks the entire flash as used
@@ -139,32 +138,18 @@ module ActionController #:nodoc:
def reset_session_with_flash
reset_session_without_flash
remove_instance_variable(:@_flash)
- flash(:refresh)
end
-
- # 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.
- def flash(refresh = false) #:doc:
- if !defined?(@_flash) || refresh
- @_flash =
- if session.is_a?(Hash)
- # don't put flash in session if disabled
- FlashHash.new
- else
- # otherwise, session is a CGI::Session or a TestSession
- # so make sure it gets retrieved from/saved to session storage after request processing
- session["flash"] ||= FlashHash.new
- end
- end
- @_flash
+ # 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.
+ def flash #:doc:
+ @_flash ||= session["flash"] ||= FlashHash.new
end
private
def assign_shortcuts_with_flash(request, response) #:nodoc:
assign_shortcuts_without_flash(request, response)
- flash(:refresh)
flash.sweep if @_session
end
end