Skip to content
This repository
Browse code

Added the :rjs render option

  • Loading branch information...
commit 6923b392b740f2346326634532b40cf24a0f26ef 1 parent e773d0e
authored May 21, 2009
14  actionpack/lib/action_controller/new_base/base.rb
@@ -13,6 +13,7 @@ class Base < Http
13 13
     include ActionController::Renderer
14 14
     include ActionController::Renderers::Json
15 15
     include ActionController::Renderers::Xml
  16
+    include ActionController::Renderers::Rjs
16 17
     include ActionController::Layouts
17 18
     include ActionController::ConditionalGet
18 19
 
@@ -74,7 +75,7 @@ def self.app_loaded!
74 75
       end
75 76
     end
76 77
     
77  
-    def _normalize_options(action = nil, options = {})
  78
+    def _normalize_options(action = nil, options = {}, &blk)
78 79
       if action.is_a?(Hash)
79 80
         options, action = action, nil 
80 81
       elsif action.is_a?(String) || action.is_a?(Symbol)
@@ -93,19 +94,20 @@ def _normalize_options(action = nil, options = {})
93 94
       end
94 95
 
95 96
       if options[:status]
96  
-        options[:status] = interpret_status(options.delete(:status)).to_i
  97
+        options[:status] = interpret_status(options[:status]).to_i
97 98
       end
98 99
 
  100
+      options[:update] = blk if block_given?
99 101
       options
100 102
     end
101 103
 
102  
-    def render(action = nil, options = {})
103  
-      options = _normalize_options(action, options)
  104
+    def render(action = nil, options = {}, &blk)
  105
+      options = _normalize_options(action, options, &blk)
104 106
       super(options)
105 107
     end
106 108
 
107  
-    def render_to_string(action = nil, options = {})
108  
-      options = _normalize_options(action, options)
  109
+    def render_to_string(action = nil, options = {}, &blk)
  110
+      options = _normalize_options(action, options, &blk)
109 111
       super(options)
110 112
     end
111 113
     
11  actionpack/lib/action_controller/new_base/render_options.rb
@@ -71,5 +71,16 @@ def _render_xml(xml, options)
71 71
         self.response_body  = xml.respond_to?(:to_xml) ? xml.to_xml : xml
72 72
       end
73 73
     end
  74
+
  75
+    module Rjs
  76
+      include RenderOption
  77
+      register_renderer :update
  78
+
  79
+      def _render_update(proc, options)
  80
+        generator = ActionView::Helpers::PrototypeHelper::JavaScriptGenerator.new(_action_view, &proc)
  81
+        response.content_type = Mime::JS
  82
+        self.response_body = generator.to_s
  83
+      end
  84
+    end
74 85
   end
75 86
 end
17  actionpack/lib/action_view/base.rb
@@ -269,15 +269,16 @@ def punctuate_body!(part)
269 269
       nil
270 270
     end
271 271
 
272  
-    private
273  
-      # Evaluates the local assigns and controller ivars, pushes them to the view.
274  
-      def _evaluate_assigns_and_ivars #:nodoc:
275  
-        unless @assigns_added
276  
-          @assigns.each { |key, value| instance_variable_set("@#{key}", value) }
277  
-          _copy_ivars_from_controller
278  
-          @assigns_added = true
279  
-        end
  272
+    # Evaluates the local assigns and controller ivars, pushes them to the view.
  273
+    def _evaluate_assigns_and_ivars #:nodoc:
  274
+      unless @assigns_added
  275
+        @assigns.each { |key, value| instance_variable_set("@#{key}", value) }
  276
+        _copy_ivars_from_controller
  277
+        @assigns_added = true
280 278
       end
  279
+    end
  280
+
  281
+    private
281 282
 
282 283
       def _copy_ivars_from_controller #:nodoc:
283 284
         if @controller
2  actionpack/lib/action_view/helpers/prototype_helper.rb
... ...
@@ -1,5 +1,6 @@
1 1
 require 'set'
2 2
 require 'active_support/json'
  3
+require 'active_support/core_ext/object/extending'
3 4
 
4 5
 module ActionView
5 6
   module Helpers
@@ -572,6 +573,7 @@ def observe_form(form_id, options = {})
572 573
       # #include_helpers_from_context has nothing to overwrite.
573 574
       class JavaScriptGenerator #:nodoc:
574 575
         def initialize(context, &block) #:nodoc:
  576
+          context._evaluate_assigns_and_ivars
575 577
           @context, @lines = context, []
576 578
           include_helpers_from_context
577 579
           @context.with_output_buffer(@lines) do
1  actionpack/lib/action_view/template/handler.rb
... ...
@@ -1,4 +1,5 @@
1 1
 require "active_support/core_ext/class/inheritable_attributes"
  2
+require "action_dispatch/http/mime_type"
2 3
 
3 4
 # Legacy TemplateHandler stub
4 5
 module ActionView
8  actionpack/test/controller/render_other_test.rb
@@ -103,6 +103,14 @@ def render_alternate_default
103 103
   end  
104 104
   
105 105
 private
  106
+  def default_render
  107
+    if @alternate_default_render
  108
+      @alternate_default_render.call
  109
+    else
  110
+      super
  111
+    end
  112
+  end
  113
+
106 114
   def determine_layout
107 115
     case action_name
108 116
       when "render_js_with_explicit_template",
4  actionpack/test/template/javascript_helper_test.rb
@@ -3,6 +3,8 @@
3 3
 class JavaScriptHelperTest < ActionView::TestCase
4 4
   tests ActionView::Helpers::JavaScriptHelper
5 5
 
  6
+  def _evaluate_assigns_and_ivars() end
  7
+
6 8
   attr_accessor :formats, :output_buffer
7 9
 
8 10
   def setup
@@ -10,6 +12,8 @@ def setup
10 12
     @template = self
11 13
   end
12 14
 
  15
+  def _evaluate_assigns_and_ivars() end
  16
+
13 17
   def test_escape_javascript
14 18
     assert_equal '', escape_javascript(nil)
15 19
     assert_equal %(This \\"thing\\" is really\\n netos\\'), escape_javascript(%(This "thing" is really\n netos'))
4  actionpack/test/template/prototype_helper_test.rb
@@ -61,6 +61,8 @@ def create_generator
61 61
 end
62 62
 
63 63
 class PrototypeHelperTest < PrototypeHelperBaseTest
  64
+  def _evaluate_assigns_and_ivars() end
  65
+
64 66
   def setup
65 67
     @record = @author = Author.new
66 68
     @article = Article.new
@@ -304,6 +306,8 @@ def setup
304 306
     @generator = create_generator
305 307
   end
306 308
 
  309
+  def _evaluate_assigns_and_ivars() end
  310
+
307 311
   def test_insert_html_with_string
308 312
     assert_equal 'Element.insert("element", { top: "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E" });',
309 313
       @generator.insert_html(:top, 'element', '<p>This is a test</p>')

0 notes on commit 6923b39

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