Skip to content
This repository
Browse code

Change AV formats so they can delegate to the controller. Now users (…

…or plugins) can override details_for_render in their controllers and add appropriate additional details. Now if only they could *do* something with those details...
  • Loading branch information...
commit b85ea58eb561d0a0fd2b0a3dbae1dc7846961c2d 1 parent 8fbbdda
authored March 01, 2010
2  actionpack/lib/action_controller/base.rb
@@ -52,7 +52,7 @@ def default_render
52 52
 
53 53
       def method_for_action(action_name)
54 54
         super || begin
55  
-          if view_paths.exists?(action_name.to_s, {:formats => formats}, controller_path)
  55
+          if view_paths.exists?(action_name.to_s, details_for_render, controller_path)
56 56
             "default_render"
57 57
           end
58 58
         end
6  actionpack/lib/action_controller/metal/rendering.rb
@@ -23,10 +23,14 @@ def render(*args)
23 23
 
24 24
       def _render_partial(options)
25 25
         options[:partial] = action_name if options[:partial] == true
26  
-        options[:_details] = {:formats => formats}
  26
+        options[:_details] = details_for_render
27 27
         super
28 28
       end
29 29
 
  30
+      def details_for_render
  31
+        {:formats => formats}
  32
+      end
  33
+
30 34
       def format_for_text
31 35
         formats.first
32 36
       end
32  actionpack/lib/action_view/helpers/prototype_helper.rb
@@ -182,14 +182,12 @@ class JavaScriptGenerator #:nodoc:
182 182
         def initialize(context, &block) #:nodoc:
183 183
           context._evaluate_assigns_and_ivars
184 184
           @context, @lines = context, []
185  
-          old_formats = @context.formats
186  
-          @context.reset_formats([:js, :html]) if @context
187  
-          include_helpers_from_context
188  
-          @context.with_output_buffer(@lines) do
189  
-            @context.instance_exec(self, &block)
  185
+          @context.reset_formats([:js, :html]) do
  186
+            include_helpers_from_context
  187
+            @context.with_output_buffer(@lines) do
  188
+              @context.instance_exec(self, &block)
  189
+            end
190 190
           end
191  
-        ensure
192  
-          @context.reset_formats(old_formats) if @context
193 191
         end
194 192
 
195 193
         private
@@ -573,15 +571,19 @@ def record(line)
573 571
               end
574 572
             end
575 573
 
576  
-            def render(*options_for_render)
577  
-              old_formats = @context && @context.formats
  574
+            def render(*options)
  575
+              with_formats(:html) do
  576
+                case option = options.first
  577
+                when Hash
  578
+                  @context.render(*options)
  579
+                else
  580
+                  option.to_s
  581
+                end
  582
+              end
  583
+            end
578 584
 
579  
-              @context.reset_formats([:html]) if @context
580  
-              Hash === options_for_render.first ?
581  
-                @context.render(*options_for_render) :
582  
-                  options_for_render.first.to_s
583  
-            ensure
584  
-              @context.reset_formats(old_formats) if @context
  585
+            def with_formats(*args)
  586
+              @context ? @context.reset_formats(args) { yield } : yield
585 587
             end
586 588
 
587 589
             def javascript_object_for(object)
8  actionpack/lib/action_view/render/rendering.rb
@@ -25,7 +25,7 @@ def render(options = {}, locals = {}, &block) #:nodoc:
25 25
         end
26 26
 
27 27
         template = if options[:file]
28  
-          find(options[:file], {:formats => formats})
  28
+          find(options[:file], details_for_render)
29 29
         elsif options[:inline]
30 30
           handler = Template.handler_class_for_extension(options[:type] || "erb")
31 31
           Template.new(options[:inline], "inline template", handler, {})
@@ -34,7 +34,7 @@ def render(options = {}, locals = {}, &block) #:nodoc:
34 34
         end
35 35
 
36 36
         if template
37  
-          layout = find(layout, {:formats => formats}) if layout
  37
+          layout = find(layout, details_for_render) if layout
38 38
           _render_template(template, layout, :locals => options[:locals])
39 39
         end
40 40
       when :update
@@ -44,6 +44,10 @@ def render(options = {}, locals = {}, &block) #:nodoc:
44 44
       end
45 45
     end
46 46
 
  47
+    def details_for_render
  48
+      controller.try(:details_for_render) || {:formats => formats}
  49
+    end
  50
+
47 51
     # You can think of a layout as a method that is called with a block. _layout_for
48 52
     # returns the contents that are yielded to the layout. If the user calls yield
49 53
     # :some_name, the block, by default, returns content_for(:some_name). If the user
1  actionpack/test/template/javascript_helper_test.rb
@@ -9,6 +9,7 @@ def _evaluate_assigns_and_ivars() end
9 9
 
10 10
   def reset_formats(format)
11 11
     @format = format
  12
+    yield if block_given?
12 13
   end
13 14
 
14 15
   def setup
1  actionpack/test/template/prototype_helper_test.rb
@@ -41,6 +41,7 @@ class PrototypeHelperBaseTest < ActionView::TestCase
41 41
 
42 42
   def reset_formats(format)
43 43
     @format = format
  44
+    yield if block_given?
44 45
   end
45 46
 
46 47
   def setup

0 notes on commit b85ea58

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