Skip to content

Commit

Permalink
Added the :rjs render option
Browse files Browse the repository at this point in the history
  • Loading branch information
Yehuda Katz + Carl Lerche committed May 21, 2009
1 parent e773d0e commit 6923b39
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 14 deletions.
14 changes: 8 additions & 6 deletions actionpack/lib/action_controller/new_base/base.rb
Expand Up @@ -13,6 +13,7 @@ class Base < Http
include ActionController::Renderer
include ActionController::Renderers::Json
include ActionController::Renderers::Xml
include ActionController::Renderers::Rjs
include ActionController::Layouts
include ActionController::ConditionalGet

Expand Down Expand Up @@ -74,7 +75,7 @@ def self.app_loaded!
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)
Expand All @@ -93,19 +94,20 @@ def _normalize_options(action = nil, options = {})
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

Expand Down
11 changes: 11 additions & 0 deletions actionpack/lib/action_controller/new_base/render_options.rb
Expand Up @@ -71,5 +71,16 @@ def _render_xml(xml, options)
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
17 changes: 9 additions & 8 deletions actionpack/lib/action_view/base.rb
Expand Up @@ -269,15 +269,16 @@ def punctuate_body!(part)
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
Expand Down
2 changes: 2 additions & 0 deletions actionpack/lib/action_view/helpers/prototype_helper.rb
@@ -1,5 +1,6 @@
require 'set'
require 'active_support/json'
require 'active_support/core_ext/object/extending'

module ActionView
module Helpers
Expand Down Expand Up @@ -572,6 +573,7 @@ def observe_form(form_id, options = {})
# #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
Expand Down
1 change: 1 addition & 0 deletions actionpack/lib/action_view/template/handler.rb
@@ -1,4 +1,5 @@
require "active_support/core_ext/class/inheritable_attributes"
require "action_dispatch/http/mime_type"

# Legacy TemplateHandler stub
module ActionView
Expand Down
8 changes: 8 additions & 0 deletions actionpack/test/controller/render_other_test.rb
Expand Up @@ -103,6 +103,14 @@ def render_alternate_default
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",
Expand Down
4 changes: 4 additions & 0 deletions actionpack/test/template/javascript_helper_test.rb
Expand Up @@ -3,13 +3,17 @@
class JavaScriptHelperTest < ActionView::TestCase
tests ActionView::Helpers::JavaScriptHelper

def _evaluate_assigns_and_ivars() end

attr_accessor :formats, :output_buffer

def setup
super
@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'))
Expand Down
4 changes: 4 additions & 0 deletions actionpack/test/template/prototype_helper_test.rb
Expand Up @@ -61,6 +61,8 @@ def create_generator
end

class PrototypeHelperTest < PrototypeHelperBaseTest
def _evaluate_assigns_and_ivars() end

def setup
@record = @author = Author.new
@article = Article.new
Expand Down Expand Up @@ -304,6 +306,8 @@ def setup
@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>')
Expand Down

0 comments on commit 6923b39

Please sign in to comment.