Permalink
Browse files

ActionController::Flash::FlashHash.use now returns either the value c…

…orresponding to the passed key or itself when no key is passed [#1792 state:resolved]

Signed-off-by: Joshua Peek <josh@joshpeek.com>
  • Loading branch information...
1 parent 746f386 commit bb8e5843f32b13e759da51a4713a31b23ebcac6d @niels niels committed with josh May 27, 2009
Showing with 20 additions and 6 deletions.
  1. +5 −6 actionpack/lib/action_controller/base/chained/flash.rb
  2. +15 −0 actionpack/test/controller/flash_test.rb
@@ -135,12 +135,11 @@ def sweep #:nodoc:
# 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
- keys.each{ |key| use(key, v) }
- end
+ # Returns the single value for the key you asked to be marked (un)used or the FlashHash itself
+ # if no key is passed.
+ def use(key = nil, used = true)
+ Array(key || keys).each { |k| @used[k] = used }
+ return key ? self[key] : self
end
end
@@ -140,4 +140,19 @@ def test_sweep_after_halted_filter_chain
get :std_action
assert_nil assigns["flash_copy"]["foo"]
end
+
+ def test_keep_and_discard_return_values
+ flash = ActionController::Flash::FlashHash.new
+ flash.update(:foo => :foo_indeed, :bar => :bar_indeed)
+
+ assert_equal(:foo_indeed, flash.discard(:foo)) # valid key passed
+ assert_nil flash.discard(:unknown) # non existant key passed
+ assert_equal({:foo => :foo_indeed, :bar => :bar_indeed}, flash.discard()) # nothing passed
+ assert_equal({:foo => :foo_indeed, :bar => :bar_indeed}, flash.discard(nil)) # nothing passed
+
+ assert_equal(:foo_indeed, flash.keep(:foo)) # valid key passed
+ assert_nil flash.keep(:unknown) # non existant key passed
+ assert_equal({:foo => :foo_indeed, :bar => :bar_indeed}, flash.keep()) # nothing passed
+ assert_equal({:foo => :foo_indeed, :bar => :bar_indeed}, flash.keep(nil)) # nothing passed
+ end
end

0 comments on commit bb8e584

Please sign in to comment.