public
Description: response for lets you decorate your actions respond_to blocks
Homepage: http://blog.ardes.com/response_for
Clone URL: git://github.com/ianwhite/response_for.git
Click here to lend your support to: response_for and make a donation at www.pledgie.com !
Added specs for stacking responses.  Added NoResponsesError which is used 
internally to handle the case where there are no responses
ianwhite (author)
Sun Sep 14 19:43:09 -0700 2008
commit  8ac908ade87b73d18340fccb10e607123806a879
tree    e486a6b328e2c28cfc3f230c28e4508c5af90401
parent  7cccbad561f75e7a3f6b3ab1b250d0e852cb47d3
...
144
145
146
147
148
149
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
144
145
146
 
 
 
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
0
@@ -144,6 +144,27 @@ RemoveResponseForSpec::TheController.remove_response_for :bar
0
 RemoveResponseForSpec::TheController.remove_response_for
0
 - should have empty action_responses
0
 
0
-Finished in 0.451715 seconds
0
-
0
-79 examples, 0 failures
0
+StackingResponsesSpec::TheController with responses conditionally executed GET :foo (no conditions)
0
+- should execute second, then first, response
0
+- should NOT execute the html response in first
0
+- should NOT execute the html response in second
0
+- should render :action => :foo (the default response)
0
+
0
+StackingResponsesSpec::TheController with responses conditionally executed GET :foo, :second => true
0
+- should execute second, then first, then in html second, response
0
+- should redirect from second response
0
+- should NOT execute first html response
0
+
0
+StackingResponsesSpec::TheController with responses conditionally executed GET :foo, :first => true
0
+- should execute second, then first, then in first html response
0
+- should redirect from first response
0
+- should NOT execute second html response
0
+
0
+StackingResponsesSpec::TheController with responses conditionally executed GET :foo, :first => true, :second => true (can't execute two html blocks)
0
+- should execute second, then first, then in second html response
0
+- should redirect from second response
0
+- should NOT execute first html response
0
+
0
+Finished in 0.729328 seconds
0
+
0
+92 examples, 0 failures
...
1
2
3
 
 
4
...
1
 
2
3
4
5
0
@@ -1,2 +1,3 @@
0
 require 'ardes/response_for'
0
-ActionController::Base.send :include, Ardes::ResponseFor
0
\ No newline at end of file
0
+ActionController::Base.send :include, Ardes::ResponseFor
0
+ActionController::MimeResponds::Responder.send :include, Ardes::ResponseFor::Responder
0
\ No newline at end of file
...
1
 
2
3
4
...
113
114
115
 
 
 
116
117
118
119
120
121
122
 
 
123
124
125
...
129
130
131
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
133
134
...
1
2
3
4
5
...
114
115
116
117
118
119
120
121
122
 
 
 
 
123
124
125
126
127
...
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
0
@@ -1,4 +1,5 @@
0
 module Ardes #:nodoc:
0
+  # included into ActionController::Base
0
   module ResponseFor
0
     def self.included(base)
0
       base.class_eval do
0
@@ -113,13 +114,14 @@ module Ardes #:nodoc:
0
     
0
   protected
0
     # if there are responses for the current action, then respond_to them
0
+    #
0
+    # we rescue the case where there were no responses, so that the default_render
0
+    # action will be performed
0
     def respond_to_action_responses
0
       if (responses = self.class.action_responses[action_name]) && responses.any?
0
         respond_to do |responder|
0
-          responses.each do |response|
0
-            instance_exec(responder, &response)
0
-          end
0
-        end
0
+          responses.each {|response| instance_exec(responder, &response) }
0
+        end rescue Responder::NoResponsesError
0
       end
0
     end
0
     
0
@@ -129,5 +131,21 @@ module Ardes #:nodoc:
0
       respond_to_action_responses
0
       default_render_without_response_for unless performed?
0
     end
0
+    
0
+    # included into ActionController::MimeResponds::Responder
0
+    module Responder
0
+      class NoResponsesError < RuntimeError; end
0
+      
0
+      def self.included(responder)
0
+        responder.class_eval do
0
+          # we make the responder raise an error if there are no responses
0
+          def respond_with_response_for
0
+            raise NoResponseError if @responses.empty?
0
+            respond_without_response_for
0
+          end
0
+          alias_method_chain :respond, :response_for
0
+        end
0
+      end
0
+    end
0
   end
0
 end
0
\ No newline at end of file

Comments