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 !
now intercepting template_exists? to decide whether to render a response for an 
action
  that has no action method defined.  (Removes somewhat mysterious behaviour of 
  empty
  actions being defined)
ianwhite (author)
Thu Oct 09 17:05:15 -0700 2008
commit  c89bc63b42f91ffcb06b34c6e9764b00e29e7979
tree    991b6657d06f57d2c7fbc958bb3d7ef1ba03dcf1
parent  b655450bed1f1b294f67a84ddb1e1faf440331bb
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
 
 
 
 
 
 
17
18
19
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
0
@@ -1,18 +1,9 @@
0
-* response_for only kicks in once ActionController has got to the stage of
0
-  performing an action.
0
-  
0
-    before_filter :bail_out
0
-  
0
-    def bail_out
0
-      respond_to do |format|
0
-        format.html { redirect_to :bail_out_place }
0
-      end
0
-    end
0
-  
0
-  The above code snippet bails out whatever the response_for, which I think is
0
-  the least surprising behaviour.
0
-  
0
-  (This means that you can ignore the last changelog message for response_for,
0
-  which has been deleted to avoid confusion)
0
+* now intercepting template_exists? to decide whether to render a response for an action
0
+  that has no action method defined.  (Removes somewhat mysterious behaviour of empty
0
+  actions being defined)
0
+
0
+* tagged v0.2.0 - API change, and major internal simplifications - see http://blog.ardes.com/response_for
0
+
0
+* tagged v0.1.0
0
   
0
 * initial release of response_for
0
\ No newline at end of file
...
165
166
167
168
 
169
170
...
165
166
167
 
168
169
170
0
@@ -165,6 +165,6 @@ StackingResponsesSpec::TheController with responses conditionally executed GET :
0
 - should redirect from second response
0
 - should NOT execute first html response
0
 
0
-Finished in 0.494693 seconds
0
+Finished in 0.49728 seconds
0
 
0
 92 examples, 0 failures
...
5
6
7
 
8
9
10
...
55
56
57
58
 
59
60
61
...
63
64
65
66
 
67
68
69
70
71
72
 
 
 
73
74
75
...
84
85
86
87
88
89
90
91
92
93
...
113
114
115
 
 
 
 
 
 
 
 
 
 
 
116
117
118
...
5
6
7
8
9
10
11
...
56
57
58
 
59
60
61
62
...
64
65
66
 
67
68
69
70
 
 
 
71
72
73
74
75
76
...
85
86
87
 
 
 
 
88
89
90
...
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
0
@@ -5,6 +5,7 @@ module Ardes #:nodoc:
0
       base.class_eval do
0
         extend ClassMethods
0
         alias_method_chain :default_render, :response_for
0
+        alias_method_chain :template_exists?, :response_for
0
       end
0
     end
0
     
0
@@ -55,7 +56,7 @@ module Ardes #:nodoc:
0
       #
0
       #   response_for :update do |format|          # this example is for a resources_controller controller
0
       #     if !(resource.new_record? || resource.changed?) # => resource.saved?
0
-      #       format.js { render(:update) {|page| page.replace(dom_id(resource), :partial => resource}}
0
+      #       format.js { render(:update) {|page| page.replace dom_id(resource), :partial => resource }}
0
       #     else
0
       #       format.js { render(:update) {|page| page.visual_effect :shake, dom_id(resource) }}
0
       #     end
0
@@ -63,13 +64,13 @@ module Ardes #:nodoc:
0
       #
0
       # === Notes
0
       #
0
-      # * If the before_filters or action renders or redirects, then response_for will not be invoked
0
+      # * If the before_filters or action renders or redirects, then response_for will not be invoked.
0
       # * you can stack up multiple response_for calls, the most recent has precedence
0
       # * the specifed block is executed within the controller instance, so you can use controller
0
       #   instance methods and instance variables (i.e. you can make it look just like a regular
0
-      #   respond_to block)
0
-      # * you can add a response_for an action that is just a public template (where there is no
0
-      #   actual action method defined)
0
+      #   respond_to block).
0
+      # * you can add a response_for an action that has no action method defined.  This is just like
0
+      #   defining a template for an action that has no action method defined.
0
       # * you can combine the :types option with a block, the block has precedence if you specify the
0
       #   same mime type in both.
0
       def response_for(*actions, &block)
0
@@ -84,10 +85,6 @@ module Ardes #:nodoc:
0
           action_responses[action] ||= []
0
           action_responses[action].unshift types_block if types_block
0
           action_responses[action].unshift block if block
0
-          
0
-          # if there's no action yet defined, create an empty one - this is so that you
0
-          # we may provide responses for templates
0
-          class_eval "def #{action}; end" unless instance_methods.include?(action)
0
         end
0
       end
0
     
0
@@ -113,6 +110,17 @@ module Ardes #:nodoc:
0
     end
0
     
0
   protected
0
+    # does a response exist for the current action?
0
+    def response_exists?
0
+      self.class.action_responses.keys.include?(action_name.to_s)
0
+    end
0
+    
0
+    # we extend template_exists? to return true if a template OR a response exists corresponding to the current action.
0
+    # This is so that a default render will be triggered when no action, but a repsonse does exist.
0
+    def template_exists_with_response_for?
0
+      response_exists? || template_exists_without_response_for?
0
+    end
0
+
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

Comments