Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

flash.use should return values which it marked as (un)used

#1792

Motivation

I found myself repeating


flash_content = flash[:whatever]
# ... 
flash.discard(:whatever)

in some of my applications.

One good example would be setting e.g. flash[:info] = "Foobar successfully deleted" in a destroy action which normally redirects to an index action. We'd normally want to display that message in index.erb.html.

However, when responding to an AJAX request, we might want to render destroy.rjs which could e.g. alert(flash[:info]). Without discarding the flash, it would still be present in the next action, which in this case is undesired. Currently a show-and-discard needs to be implemented as outlined above, the patch makes this easier:

What's in the patch?

The patch changes ActionController::Flash::FlashHash.use which is called by FlashHash.discard and FlashHash.keep. After applying the patch, all three functions will return either the single value corresponding to the provided key or itself (the FlashHash).

Any side effects?

I could not find a single line in the codebase in which the return value of either of these three functions is evaluated and all tests pass. So I'd say there aren't any!

Are tests included?

Yes. The included test does properly test the feature but I'm nevertheless not yet sure whether I like it. Feedback welcome!

Let me know what you think. The diff has been generated against the latest commit in master.

Reported by Niels Ganser · January 23rd, 2009 @ 08:18 PM

State: resolved
Milestone: 3.0.2
Assigned to: josh josh
Importance: none

Activity

  1. Niels Ganser
    Niels Ganser

    Here's an alternative, i.e. more extensive, test:

    
      def test_keep_and_discard_return_values
        flash = ActionController::Flash::FlashHash.new
        flash.update(:foo => :foo_indeed, :bar => :bar_indeed)
        
        [:discard, :keep].each do |method|
          assert_equal(:foo_indeed, flash.send(method, :foo)) # valid key passed
          assert_nil flash.send(method, :unknown) # non existant key passed
          assert_equal({:foo => :foo_indeed, :bar => :bar_indeed}, flash.send(method)) # nothing passed
          assert_equal({:foo => :foo_indeed, :bar => :bar_indeed}, flash.send(method, nil)) # nothing passed      
        end
      end
    

    January 23rd, 2009 @ 09:25 PM

  2. Niels Ganser
    Niels Ganser

    Right.. Sorry for the soliloquy. Please review the attached updated patch which features more extensive tests.

    I wonder if the test should go someplace else though as it doesn't really fit in with the rest of the file. Any thoughts on this?

    January 23rd, 2009 @ 09:46 PM

  3. Pratik
    Pratik
    • Assigned user set to josh

    May 18th, 2009 @ 07:50 PM

  4. josh
    josh
    • State changed from new to open
    • Milestone cleared.

    May 25th, 2009 @ 05:28 PM

  5. Repository
    Repository
    • State changed from open to resolved

    (from [bb8e5843f32b13e759da51a4713a31b23ebcac6d]) ActionController::Flash::FlashHash.use now returns either the value corresponding to the passed key or itself when no key is passed [#1792 state:resolved]

    Signed-off-by: Joshua Peek josh@joshpeek.com
    http://github.com/rails/rails/commit/bb8e5843f32b13e759da51a4713a31...

    May 27th, 2009 @ 09:02 PM

  6. Repository
    Repository

    (from [c01014ac1c98a05fe68e652f978dfa9d43f0fc2b]) Modify the behavior of radio_button_tag to use sanitize_to_id for consistency [#1792 status:resolved]

    Signed-off-by: José Valim jose.valim@gmail.com
    http://github.com/rails/rails/commit/c01014ac1c98a05fe68e652f978dfa...

    February 2nd, 2010 @ 10:33 AM

  7. Jeremy Kemper
    Jeremy Kemper
    • Milestone set to 3.0.2
    • Importance changed from to

    October 15th, 2010 @ 11:01 PM