Skip to content
This repository
Browse code

Added support add_flash_types

  • Loading branch information...
commit 238a4253bf229377b686bfcecc63dda2b59cff8f 1 parent c55df93
Toshinori Kajihara authored July 07, 2012
10  actionpack/CHANGELOG.md
Source Rendered
... ...
@@ -1,5 +1,15 @@
1 1
 ## Rails 4.0.0 (unreleased) ##
2 2
 
  3
+*   Add `ActionController::Flash.add_flash_types` method to allow people to register their own flash types. e.g.:
  4
+
  5
+        class ApplicationController
  6
+          add_flash_types :error, :warning
  7
+        end
  8
+
  9
+    If you add the above code, you can use `<%= error %>` in an erb, and `rediect_to /foo, :error => 'message'` in a controller.
  10
+
  11
+    *kennyj*
  12
+
3 13
 *   Remove Active Model dependency from Action Pack. *Guillermo Iguaran*
4 14
 
5 15
 *   Support unicode characters in routes. Route will be automatically escaped, so instead of manually escaping:
31  actionpack/lib/action_controller/metal/flash.rb
@@ -3,19 +3,34 @@ module Flash
3 3
     extend ActiveSupport::Concern
4 4
 
5 5
     included do
  6
+      class_attribute :_flash_types, :instance_methods => false
  7
+      self._flash_types = []
  8
+
6 9
       delegate :flash, :to => :request
7  
-      delegate :alert, :notice, :to => "request.flash"
8  
-      helper_method :alert, :notice
  10
+      add_flash_types(:alert, :notice)
9 11
     end
10 12
 
11  
-    protected
12  
-      def redirect_to(options = {}, response_status_and_flash = {}) #:doc:
13  
-        if alert = response_status_and_flash.delete(:alert)
14  
-          flash[:alert] = alert
  13
+    module ClassMethods
  14
+      def add_flash_types(*types)
  15
+        types.each do |type|
  16
+          next if _flash_types.include?(type)
  17
+          
  18
+          define_method(type) do
  19
+            request.flash[type]
  20
+          end
  21
+          helper_method type
  22
+
  23
+          _flash_types << type
15 24
         end
  25
+      end
  26
+    end
16 27
 
17  
-        if notice = response_status_and_flash.delete(:notice)
18  
-          flash[:notice] = notice
  28
+    protected
  29
+      def redirect_to(options = {}, response_status_and_flash = {}) #:doc:
  30
+        self.class._flash_types.each do |flash_type|
  31
+          if type = response_status_and_flash.delete(flash_type)
  32
+            flash[flash_type] = type
  33
+          end
19 34
         end
20 35
 
21 36
         if other_flashes = response_status_and_flash.delete(:flash)
26  actionpack/test/controller/flash_test.rb
@@ -90,6 +90,10 @@ def render_with_flash_now_notice
90 90
     def redirect_with_other_flashes
91 91
       redirect_to '/wonderland', :flash => { :joyride => "Horses!" }
92 92
     end
  93
+
  94
+    def redirect_with_foo_flash
  95
+      redirect_to "/wonderland", :foo => 'for great justice'
  96
+    end
93 97
   end
94 98
 
95 99
   tests TestController
@@ -203,6 +207,12 @@ def test_redirect_to_with_other_flashes
203 207
     get :redirect_with_other_flashes
204 208
     assert_equal "Horses!", @controller.send(:flash)[:joyride]
205 209
   end
  210
+
  211
+  def test_redirect_to_with_adding_flash_types
  212
+    @controller.class.add_flash_types :foo
  213
+    get :redirect_with_foo_flash
  214
+    assert_equal "for great justice", @controller.send(:flash)[:foo]
  215
+  end
206 216
 end
207 217
 
208 218
 class FlashIntegrationTest < ActionDispatch::IntegrationTest
@@ -210,6 +220,9 @@ class FlashIntegrationTest < ActionDispatch::IntegrationTest
210 220
   SessionSecret = 'b3c631c314c0bbca50c1b2843150fe33'
211 221
 
212 222
   class TestController < ActionController::Base
  223
+
  224
+    add_flash_types :bar
  225
+
213 226
     def set_flash
214 227
       flash["that"] = "hello"
215 228
       head :ok
@@ -223,6 +236,11 @@ def set_flash_now
223 236
     def use_flash
224 237
       render :inline => "flash: #{flash["that"]}"
225 238
     end
  239
+
  240
+    def set_bar
  241
+      flash[:bar] = "for great justice"
  242
+      head :ok
  243
+    end
226 244
   end
227 245
 
228 246
   def test_flash
@@ -262,6 +280,14 @@ def test_setting_flash_now_does_not_raise_in_following_requests
262 280
     end
263 281
   end
264 282
 
  283
+  def test_added_flash_types_method
  284
+    with_test_route_set do
  285
+      get '/set_bar'
  286
+      assert_response :success
  287
+      assert_equal 'for great justice', @controller.bar
  288
+    end
  289
+  end
  290
+
265 291
   private
266 292
 
267 293
     # Overwrite get to send SessionSecret in env hash
12  actionpack/test/template/test_case_test.rb
@@ -68,14 +68,14 @@ class GeneralViewTest < ActionView::TestCase
68 68
       assert_nil self.class.determine_default_helper_class("String")
69 69
     end
70 70
 
71  
-    test "delegates notice to request.flash" do
72  
-      view.request.flash.expects(:notice).with("this message")
73  
-      view.notice("this message")
  71
+    test "delegates notice to request.flash[:notice]" do
  72
+      view.request.flash.expects(:[]).with(:notice)
  73
+      view.notice
74 74
     end
75 75
 
76  
-    test "delegates alert to request.flash" do
77  
-      view.request.flash.expects(:alert).with("this message")
78  
-      view.alert("this message")
  76
+    test "delegates alert to request.flash[:alert]" do
  77
+      view.request.flash.expects(:[]).with(:alert)
  78
+      view.alert
79 79
     end
80 80
 
81 81
     test "uses controller lookup context" do

1 note on commit 238a425

Jamie Hill

Loving the ability to add bespoke flash types. Wouldn't a better solution be to just assume that everything that isn't the response status is a flash? No need to register types at all in that case.

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