Skip to content
This repository
Browse code

Make ActionController::Flash work with new_base

  • Loading branch information...
commit 8e7a87d299483fce6af3be89e50deae43055a96f 1 parent 205cfe2
Pratik authored May 21, 2009
2  actionpack/Rakefile
@@ -61,7 +61,7 @@ Rake::TestTask.new(:test_new_base_on_old_tests) do |t|
61 61
   # content_type mime_responds layout
62 62
   t.test_files = %w(
63 63
     addresses_render base benchmark caching capture dispatcher record_identifier
64  
-    redirect render rescue url_rewriter webservice
  64
+    redirect render rescue url_rewriter webservice flash
65 65
   ).map { |name| "test/controller/#{name}_test.rb" }
66 66
 end
67 67
 
74  actionpack/lib/action_controller/base/chained/flash.rb
@@ -26,9 +26,18 @@ module ActionController #:nodoc:
26 26
   #
27 27
   # See docs on the FlashHash class for more details about the flash.
28 28
   module Flash
29  
-    def self.included(base)
30  
-      base.class_eval do
31  
-        include InstanceMethods
  29
+    extend ActiveSupport::DependencyModule
  30
+
  31
+    # TODO : Remove the defined? check when new base is the main base
  32
+    depends_on Session if defined?(ActionController::Http)
  33
+    
  34
+    included do
  35
+      # TODO : Remove the defined? check when new base is the main base
  36
+      if defined?(ActionController::Http)
  37
+        include InstanceMethodsForNewBase
  38
+      else
  39
+        include InstanceMethodsForBase
  40
+
32 41
         alias_method_chain :perform_action, :flash
33 42
         alias_method_chain :reset_session,  :flash
34 43
       end
@@ -135,29 +144,50 @@ def use(k=nil, v=true)
135 144
         end
136 145
     end
137 146
 
138  
-    module InstanceMethods #:nodoc:
  147
+    module InstanceMethodsForBase #:nodoc:
139 148
       protected
140  
-        def perform_action_with_flash
141  
-          perform_action_without_flash
142  
-          remove_instance_variable(:@_flash) if defined? @_flash
143  
-        end
144 149
 
145  
-        def reset_session_with_flash
146  
-          reset_session_without_flash
147  
-          remove_instance_variable(:@_flash) if defined? @_flash
148  
-        end
  150
+      def perform_action_with_flash
  151
+        perform_action_without_flash
  152
+        remove_instance_variable(:@_flash) if defined?(@_flash)
  153
+      end
149 154
 
150  
-        # Access the contents of the flash. Use <tt>flash["notice"]</tt> to
151  
-        # read a notice you put there or <tt>flash["notice"] = "hello"</tt>
152  
-        # to put a new one.
153  
-        def flash #:doc:
154  
-          unless defined? @_flash
155  
-            @_flash = session["flash"] ||= FlashHash.new
156  
-            @_flash.sweep
157  
-          end
  155
+      def reset_session_with_flash
  156
+        reset_session_without_flash
  157
+        remove_instance_variable(:@_flash) if defined?(@_flash)
  158
+      end
  159
+    end
158 160
 
159  
-          @_flash
160  
-        end
  161
+    module InstanceMethodsForNewBase #:nodoc:
  162
+      protected
  163
+
  164
+      def reset_session
  165
+        super
  166
+        remove_flash_instance_variable
  167
+      end
  168
+
  169
+      def process_action(method_name)
  170
+        super
  171
+        remove_flash_instance_variable
  172
+      end
  173
+      
  174
+      def remove_flash_instance_variable
  175
+        remove_instance_variable(:@_flash) if defined?(@_flash)
  176
+      end
  177
+    end
  178
+
  179
+    protected
  180
+
  181
+    # Access the contents of the flash. Use <tt>flash["notice"]</tt> to
  182
+    # read a notice you put there or <tt>flash["notice"] = "hello"</tt>
  183
+    # to put a new one.
  184
+    def flash #:doc:
  185
+      unless defined?(@_flash)
  186
+        @_flash = session["flash"] ||= FlashHash.new
  187
+        @_flash.sweep
  188
+      end
  189
+
  190
+      @_flash
161 191
     end
162 192
   end
163 193
 end
5  actionpack/lib/action_controller/new_base.rb
@@ -10,7 +10,8 @@ module ActionController
10 10
   autoload :Rescue,               "action_controller/new_base/rescuable"
11 11
   autoload :Testing,              "action_controller/new_base/testing"
12 12
   autoload :UrlFor,               "action_controller/new_base/url_for"
13  
-  
  13
+  autoload :Session,              "action_controller/new_base/session"
  14
+
14 15
   # Ported modules
15 16
   # require 'action_controller/routing'
16 17
   autoload :Caching,           'action_controller/caching'
@@ -23,6 +24,8 @@ module ActionController
23 24
   autoload :TestCase,          'action_controller/testing/test_case'
24 25
   autoload :UrlRewriter,       'action_controller/routing/generation/url_rewriter'
25 26
   autoload :UrlWriter,         'action_controller/routing/generation/url_rewriter'
  27
+
  28
+  autoload :Flash, 'action_controller/base/chained/flash'
26 29
   
27 30
   require 'action_controller/routing'
28 31
 end
3  actionpack/lib/action_controller/new_base/base.rb
@@ -14,6 +14,9 @@ class Base < Http
14 14
     include ActionController::Layouts
15 15
     include ActionController::ConditionalGet
16 16
 
  17
+    include ActionController::Session
  18
+    include ActionController::Flash
  19
+
17 20
     # Legacy modules
18 21
     include SessionManagement
19 22
     include ActionDispatch::StatusCodes
2  actionpack/lib/action_controller/new_base/http.rb
@@ -37,7 +37,7 @@ def self.call(env)
37 37
     end
38 38
     
39 39
     delegate :headers, :to => "@_response"
40  
-    
  40
+
41 41
     def params
42 42
       @_params ||= @_request.parameters
43 43
     end
11  actionpack/lib/action_controller/new_base/session.rb
... ...
@@ -0,0 +1,11 @@
  1
+module ActionController
  2
+  module Session
  3
+    def session
  4
+      @_request.session
  5
+    end
  6
+
  7
+    def reset_session
  8
+      @_request.reset_session
  9
+    end
  10
+  end
  11
+end
49  actionpack/test/controller/flash_test.rb
@@ -60,6 +60,7 @@ def rescue_action(e)
60 60
 
61 61
     def std_action
62 62
       @flash_copy = {}.update(flash)
  63
+      render :nothing => true
63 64
     end
64 65
 
65 66
     def filter_halting_action
@@ -79,64 +80,64 @@ def test_flash
79 80
     get :set_flash
80 81
 
81 82
     get :use_flash
82  
-    assert_equal "hello", @controller.template.assigns["flash_copy"]["that"]
83  
-    assert_equal "hello", @controller.template.assigns["flashy"]
  83
+    assert_equal "hello", assigns["flash_copy"]["that"]
  84
+    assert_equal "hello", assigns["flashy"]
84 85
 
85 86
     get :use_flash
86  
-    assert_nil @controller.template.assigns["flash_copy"]["that"], "On second flash"
  87
+    assert_nil assigns["flash_copy"]["that"], "On second flash"
87 88
   end
88 89
 
89 90
   def test_keep_flash
90 91
     get :set_flash
91 92
     
92 93
     get :use_flash_and_keep_it
93  
-    assert_equal "hello", @controller.template.assigns["flash_copy"]["that"]
94  
-    assert_equal "hello", @controller.template.assigns["flashy"]
  94
+    assert_equal "hello", assigns["flash_copy"]["that"]
  95
+    assert_equal "hello", assigns["flashy"]
95 96
 
96 97
     get :use_flash
97  
-    assert_equal "hello", @controller.template.assigns["flash_copy"]["that"], "On second flash"
  98
+    assert_equal "hello", assigns["flash_copy"]["that"], "On second flash"
98 99
 
99 100
     get :use_flash
100  
-    assert_nil @controller.template.assigns["flash_copy"]["that"], "On third flash"
  101
+    assert_nil assigns["flash_copy"]["that"], "On third flash"
101 102
   end
102 103
   
103 104
   def test_flash_now
104 105
     get :set_flash_now
105  
-    assert_equal "hello", @controller.template.assigns["flash_copy"]["that"]
106  
-    assert_equal "bar"  , @controller.template.assigns["flash_copy"]["foo"]
107  
-    assert_equal "hello", @controller.template.assigns["flashy"]
  106
+    assert_equal "hello", assigns["flash_copy"]["that"]
  107
+    assert_equal "bar"  , assigns["flash_copy"]["foo"]
  108
+    assert_equal "hello", assigns["flashy"]
108 109
 
109 110
     get :attempt_to_use_flash_now
110  
-    assert_nil @controller.template.assigns["flash_copy"]["that"]
111  
-    assert_nil @controller.template.assigns["flash_copy"]["foo"]
112  
-    assert_nil @controller.template.assigns["flashy"]
  111
+    assert_nil assigns["flash_copy"]["that"]
  112
+    assert_nil assigns["flash_copy"]["foo"]
  113
+    assert_nil assigns["flashy"]
113 114
   end 
114 115
   
115 116
   def test_update_flash
116 117
     get :set_flash
117 118
     get :use_flash_and_update_it
118  
-    assert_equal "hello",       @controller.template.assigns["flash_copy"]["that"]
119  
-    assert_equal "hello again", @controller.template.assigns["flash_copy"]["this"]
  119
+    assert_equal "hello",       assigns["flash_copy"]["that"]
  120
+    assert_equal "hello again", assigns["flash_copy"]["this"]
120 121
     get :use_flash
121  
-    assert_nil                  @controller.template.assigns["flash_copy"]["that"], "On second flash"
122  
-    assert_equal "hello again", @controller.template.assigns["flash_copy"]["this"], "On second flash"
  122
+    assert_nil                  assigns["flash_copy"]["that"], "On second flash"
  123
+    assert_equal "hello again", assigns["flash_copy"]["this"], "On second flash"
123 124
   end
124 125
   
125 126
   def test_flash_after_reset_session
126 127
     get :use_flash_after_reset_session
127  
-    assert_equal "hello",    @controller.template.assigns["flashy_that"]
128  
-    assert_equal "good-bye", @controller.template.assigns["flashy_this"]
129  
-    assert_nil   @controller.template.assigns["flashy_that_reset"]
  128
+    assert_equal "hello",    assigns["flashy_that"]
  129
+    assert_equal "good-bye", assigns["flashy_this"]
  130
+    assert_nil   assigns["flashy_that_reset"]
130 131
   end 
131 132
 
132 133
   def test_sweep_after_halted_filter_chain
133 134
     get :std_action
134  
-    assert_nil @controller.template.assigns["flash_copy"]["foo"]
  135
+    assert_nil assigns["flash_copy"]["foo"]
135 136
     get :filter_halting_action
136  
-    assert_equal "bar", @controller.template.assigns["flash_copy"]["foo"]
  137
+    assert_equal "bar", assigns["flash_copy"]["foo"]
137 138
     get :std_action # follow redirection
138  
-    assert_equal "bar", @controller.template.assigns["flash_copy"]["foo"]
  139
+    assert_equal "bar", assigns["flash_copy"]["foo"]
139 140
     get :std_action
140  
-    assert_nil @controller.template.assigns["flash_copy"]["foo"]
  141
+    assert_nil assigns["flash_copy"]["foo"]
141 142
   end
142 143
 end

0 notes on commit 8e7a87d

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