1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Added the :rjs render option

This commit is contained in:
Yehuda Katz + Carl Lerche 2009-05-21 16:35:40 -07:00
parent e773d0e68a
commit 6923b392b7
8 changed files with 47 additions and 14 deletions

View file

@ -13,6 +13,7 @@ module ActionController
include ActionController::Renderer
include ActionController::Renderers::Json
include ActionController::Renderers::Xml
include ActionController::Renderers::Rjs
include ActionController::Layouts
include ActionController::ConditionalGet
@ -74,7 +75,7 @@ module ActionController
end
end
def _normalize_options(action = nil, options = {})
def _normalize_options(action = nil, options = {}, &blk)
if action.is_a?(Hash)
options, action = action, nil
elsif action.is_a?(String) || action.is_a?(Symbol)
@ -93,19 +94,20 @@ module ActionController
end
if options[:status]
options[:status] = interpret_status(options.delete(:status)).to_i
options[:status] = interpret_status(options[:status]).to_i
end
options[:update] = blk if block_given?
options
end
def render(action = nil, options = {})
options = _normalize_options(action, options)
def render(action = nil, options = {}, &blk)
options = _normalize_options(action, options, &blk)
super(options)
end
def render_to_string(action = nil, options = {})
options = _normalize_options(action, options)
def render_to_string(action = nil, options = {}, &blk)
options = _normalize_options(action, options, &blk)
super(options)
end

View file

@ -71,5 +71,16 @@ module ActionController
self.response_body = xml.respond_to?(:to_xml) ? xml.to_xml : xml
end
end
module Rjs
include RenderOption
register_renderer :update
def _render_update(proc, options)
generator = ActionView::Helpers::PrototypeHelper::JavaScriptGenerator.new(_action_view, &proc)
response.content_type = Mime::JS
self.response_body = generator.to_s
end
end
end
end

View file

@ -269,15 +269,16 @@ module ActionView #:nodoc:
nil
end
private
# Evaluates the local assigns and controller ivars, pushes them to the view.
def _evaluate_assigns_and_ivars #:nodoc:
unless @assigns_added
@assigns.each { |key, value| instance_variable_set("@#{key}", value) }
_copy_ivars_from_controller
@assigns_added = true
end
# Evaluates the local assigns and controller ivars, pushes them to the view.
def _evaluate_assigns_and_ivars #:nodoc:
unless @assigns_added
@assigns.each { |key, value| instance_variable_set("@#{key}", value) }
_copy_ivars_from_controller
@assigns_added = true
end
end
private
def _copy_ivars_from_controller #:nodoc:
if @controller

View file

@ -1,5 +1,6 @@
require 'set'
require 'active_support/json'
require 'active_support/core_ext/object/extending'
module ActionView
module Helpers
@ -572,6 +573,7 @@ module ActionView
# #include_helpers_from_context has nothing to overwrite.
class JavaScriptGenerator #:nodoc:
def initialize(context, &block) #:nodoc:
context._evaluate_assigns_and_ivars
@context, @lines = context, []
include_helpers_from_context
@context.with_output_buffer(@lines) do

View file

@ -1,4 +1,5 @@
require "active_support/core_ext/class/inheritable_attributes"
require "action_dispatch/http/mime_type"
# Legacy TemplateHandler stub
module ActionView

View file

@ -103,6 +103,14 @@ class TestController < ActionController::Base
end
private
def default_render
if @alternate_default_render
@alternate_default_render.call
else
super
end
end
def determine_layout
case action_name
when "render_js_with_explicit_template",

View file

@ -3,6 +3,8 @@ require 'abstract_unit'
class JavaScriptHelperTest < ActionView::TestCase
tests ActionView::Helpers::JavaScriptHelper
def _evaluate_assigns_and_ivars() end
attr_accessor :formats, :output_buffer
def setup
@ -10,6 +12,8 @@ class JavaScriptHelperTest < ActionView::TestCase
@template = self
end
def _evaluate_assigns_and_ivars() end
def test_escape_javascript
assert_equal '', escape_javascript(nil)
assert_equal %(This \\"thing\\" is really\\n netos\\'), escape_javascript(%(This "thing" is really\n netos'))

View file

@ -61,6 +61,8 @@ class PrototypeHelperBaseTest < ActionView::TestCase
end
class PrototypeHelperTest < PrototypeHelperBaseTest
def _evaluate_assigns_and_ivars() end
def setup
@record = @author = Author.new
@article = Article.new
@ -304,6 +306,8 @@ class JavaScriptGeneratorTest < PrototypeHelperBaseTest
@generator = create_generator
end
def _evaluate_assigns_and_ivars() end
def test_insert_html_with_string
assert_equal 'Element.insert("element", { top: "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E" });',
@generator.insert_html(:top, 'element', '<p>This is a test</p>')