Skip to content
Browse files

The FlashHash and friends causes a lot of needless session storing, w…

…hen we know for a fact that there's no content in the flash. By not storing the empty hash in the session we save a lot of communication with the various session backends, while still keeping the same interface to the flash. [#2703 state:resolved]

Signed-off-by: Joshua Peek <josh@joshpeek.com>
  • Loading branch information...
1 parent 34a1ed0 commit dc94c095031036ffc0f9ac0ee98a1162e57062eb @js js committed with josh May 28, 2009
Showing with 17 additions and 4 deletions.
  1. +11 −3 actionpack/lib/action_controller/flash.rb
  2. +6 −1 actionpack/test/controller/flash_test.rb
View
14 actionpack/lib/action_controller/flash.rb
@@ -120,6 +120,11 @@ def sweep #:nodoc:
(@used.keys - keys).each{ |k| @used.delete(k) }
end
+ def store(session, key = "flash")
+ return if self.empty?
+ session[key] = self
+ end
+
private
# Used internally by the <tt>keep</tt> and <tt>discard</tt> methods
# use() # marks the entire flash as used
@@ -139,7 +144,10 @@ module InstanceMethods #:nodoc:
protected
def perform_action_with_flash
perform_action_without_flash
- remove_instance_variable(:@_flash) if defined? @_flash
+ if defined? @_flash
+ @_flash.store(session)
+ remove_instance_variable(:@_flash)
+ end
end
def reset_session_with_flash
@@ -151,8 +159,8 @@ def reset_session_with_flash
# read a notice you put there or <tt>flash["notice"] = "hello"</tt>
# to put a new one.
def flash #:doc:
- unless defined? @_flash
- @_flash = session["flash"] ||= FlashHash.new
+ if !defined?(@_flash)
+ @_flash = session["flash"] || FlashHash.new
@_flash.sweep
end
View
7 actionpack/test/controller/flash_test.rb
@@ -121,7 +121,7 @@ def test_update_flash
assert_nil @response.template.assigns["flash_copy"]["that"], "On second flash"
assert_equal "hello again", @response.template.assigns["flash_copy"]["this"], "On second flash"
end
-
+
def test_flash_after_reset_session
get :use_flash_after_reset_session
assert_equal "hello", @response.template.assigns["flashy_that"]
@@ -139,4 +139,9 @@ def test_sweep_after_halted_filter_chain
get :std_action
assert_nil @response.template.assigns["flash_copy"]["foo"]
end
+
+ def test_does_not_set_the_session_if_the_flash_is_empty
+ get :std_action
+ assert_nil session["flash"]
+ end
end

0 comments on commit dc94c09

Please sign in to comment.
Something went wrong with that request. Please try again.