Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Flash now on failure #26

Closed
wants to merge 3 commits into from

4 participants

Iain Hecker Brendan Spinks Didier Lafforgue José Valim
Iain Hecker
iain commented July 09, 2011

Since the usual pattern is that a page gets rerendered after failure, the proper behavior would be to set the flash directly, so that it doesn't stick around if you give up and leave the page.

This is a redone version of my last pull request, with some improvements.

added some commits July 09, 2011
Iain Hecker Set default of flash_now to :on_failure
Since the usual pattern is that a page gets rerendered after failure,
the proper behavior would be to set the flash directly, so that it
doesn't stick around if you give up and leave the page.
b706e7e
Iain Hecker Use a default for flash_now, enabling it to be set to false.
The previous behavior to never set flash.now can now be used again.
d36fc1a
Iain Hecker assert_not_flash_now should require that the flash is set
Otherwise it would return a false positive when the flash wasn't set at
all.
e98dd3c
Brendan Spinks

Seems to do the trick, merge please.

Didier Lafforgue

I assume there has to be a reason why flash_now is not set by default to :on_failure. Am I right people from plataformatec ?

In my app, I write a custom responder and I re-define the set_flash_now? method within it like that:

    def set_flash_now?
      super || has_errors?
    end
José Valim
Owner

This pull request was good. Could you please rebase? I know I am late to the party, but for some reason it seems I was not receiving notifications. :(

José Valim josevalim closed this March 20, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 3 unique commits by 1 author.

Jul 09, 2011
Iain Hecker Set default of flash_now to :on_failure
Since the usual pattern is that a page gets rerendered after failure,
the proper behavior would be to set the flash directly, so that it
doesn't stick around if you give up and leave the page.
b706e7e
Iain Hecker Use a default for flash_now, enabling it to be set to false.
The previous behavior to never set flash.now can now be used again.
d36fc1a
Iain Hecker assert_not_flash_now should require that the flash is set
Otherwise it would return a false positive when the flash wasn't set at
all.
e98dd3c
This page is out of date. Refresh to see the latest.
4  lib/responders/flash_responder.rb
@@ -85,7 +85,7 @@ def initialize(controller, resources, options={})
85 85
       @flash     = options.delete(:flash)
86 86
       @notice    = options.delete(:notice)
87 87
       @alert     = options.delete(:alert)
88  
-      @flash_now = options.delete(:flash_now)
  88
+      @flash_now = options.delete(:flash_now) { :on_failure }
89 89
     end
90 90
 
91 91
     def to_html
@@ -164,4 +164,4 @@ def flash_defaults_by_namespace(status) #:nodoc:
164 164
       defaults << ""
165 165
     end
166 166
   end
167  
-end
  167
+end
18  test/flash_responder_test.rb
@@ -30,6 +30,11 @@ def another
30 30
     respond_with(@resource, :notice => "Yes, notice this!", :alert => "Warning, warning!")
31 31
   end
32 32
 
  33
+  def flexible
  34
+    options = params[:responder_options] || {}
  35
+    respond_with(@resource, options)
  36
+  end
  37
+
33 38
   protected
34 39
 
35 40
   def interpolation_options
@@ -135,6 +140,16 @@ def test_sets_message_based_on_alert_key
135 140
     assert_equal "Warning, warning!", flash[:alert]
136 141
   end
137 142
 
  143
+  def test_sets_flash_now_on_failure_by_default
  144
+    post :another, :fail => true
  145
+    assert_flash_now :alert
  146
+  end
  147
+
  148
+  def test_never_set_flash_now
  149
+    post :flexible, :fail => true, :responder_options => { :flash_now => false, :alert => "Warning" }
  150
+    assert_not_flash_now :alert
  151
+  end
  152
+
138 153
   # If we have flash.now, it's always marked as used.
139 154
   def assert_flash_now(k)
140 155
     assert flash.instance_variable_get(:@used).include?(k.to_sym),
@@ -142,6 +157,7 @@ def assert_flash_now(k)
142 157
   end
143 158
 
144 159
   def assert_not_flash_now(k)
  160
+    assert flash.has_key?(k), "Expected #{k} to be set"
145 161
     assert !flash.instance_variable_get(:@used).include?(k.to_sym),
146 162
      "Expected #{k} to not be in flash.now, but it is."
147 163
   end
@@ -169,4 +185,4 @@ def test_fallbacks_to_non_namespaced_controller_flash_message
169 185
     delete :destroy
170 186
     assert_equal "Successfully deleted the chosen address at Ocean Avenue", flash[:notice]
171 187
   end
172  
-end
  188
+end
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.