Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Working on being able to render :text with layouts

  • Loading branch information...
commit 81e814adfad6d4bba1af5f70a5a409f6d71f8f6c 1 parent 90c079a
Carl Lerche authored wycats committed
2  actionpack/lib/action_controller/abstract/renderer.rb
View
@@ -31,7 +31,7 @@ def render(name = action_name, options = {})
#
# :api: plugin
def render_to_string(name = action_name, options = {})
- template = view_paths.find_by_parts(name.to_s, formats, options[:_prefix])
+ template = options[:_template] || view_paths.find_by_parts(name.to_s, formats, options[:_prefix])
_render_template(template, options)
end
13 actionpack/lib/action_controller/new_base/renderer.rb
View
@@ -35,15 +35,16 @@ def render_to_string(options)
end
if options.key?(:text)
- _render_text(options)
+ options[:_template] = ActionView::TextTemplate.new(_text(options))
+ template = nil
elsif options.key?(:template)
- template = options.delete(:template)
- super(template)
+ template = options.delete(:template)
elsif options.key?(:action)
template = options.delete(:action).to_s
options[:_prefix] = _prefix
- super(template, options)
end
+
+ super(template, options)
end
private
@@ -52,12 +53,12 @@ def _prefix
controller_path
end
- def _render_text(options)
+ def _text(options)
text = options.delete(:text)
case text
when nil then " "
- else text.to_s
+ else text.to_s
end
end
1  actionpack/lib/action_view.rb
View
@@ -50,6 +50,7 @@ def self.load_all!
autoload :TemplateError, 'action_view/template/error'
autoload :TemplateHandler, 'action_view/template/handler'
autoload :TemplateHandlers, 'action_view/template/handlers'
+ autoload :TextTemplate, 'action_view/template/text'
autoload :Helpers, 'action_view/helpers'
end
6 actionpack/lib/action_view/render/rendering.rb
View
@@ -62,20 +62,18 @@ def _render_content_with_layout(content, layout, locals)
end
def _render_template(template, local_assigns = {})
- template.compile(local_assigns)
-
@_render_stack.push(template)
_evaluate_assigns_and_ivars
_set_controller_content_type(template.mime_type) if template.respond_to?(:mime_type)
- result = send(template.method_name(local_assigns), local_assigns) do |*names|
+ result = template.render(self, local_assigns) do |*names|
if !instance_variable_defined?(:"@content_for_#{names.first}") &&
instance_variable_defined?(:@_proc_for_layout) && (proc = @_proc_for_layout)
capture(*names, &proc)
elsif instance_variable_defined?(ivar = :"@content_for_#{names.first || :layout}")
instance_variable_get(ivar)
- end
+ end
end
@_render_stack.pop
7 actionpack/lib/action_view/template/renderable.rb
View
@@ -4,6 +4,13 @@ module ActionView
module Renderable #:nodoc:
extend ActiveSupport::Memoizable
+ def render(view, locals)
+ compile(locals)
+ view.send(method_name(locals), locals) {|*args| yield(*args) }
+ end
+
+ private
+
def filename
'compiled-template'
end
2  actionpack/lib/action_view/template/template.rb
View
@@ -206,7 +206,7 @@ def recompile?
def load!
@cached = true
- freeze
+ # freeze
end
private
9 actionpack/lib/action_view/template/text.rb
View
@@ -0,0 +1,9 @@
+module ActionView #:nodoc:
+ class TextTemplate < String #:nodoc:
+
+ def render(*) self end
+
+ def exempt_from_layout?() false end
+
+ end
+end
34 actionpack/test/new_base/render_text_test.rb
View
@@ -3,9 +3,13 @@
module HappyPath
class RenderTextController < ActionController::Base2
+ self.view_paths = [ActionView::FixtureTemplate::FixturePath.new(
+ "layouts/application.html.erb" => "<%= yield %>, I'm here!",
+ "layouts/greetings.html.erb" => "<%= yield %>, I wish thee well."
+ )]
+
def render_hello_world_from_variable
- @person = "david"
- render :text => "hello #{@person}"
+ render :text => "hello david"
end
def render_custom_code
@@ -27,10 +31,18 @@ def render_text_with_nil_and_status
def render_text_with_false
render :text => false
end
+
+ def render_text_with_layout
+ render :text => "hello world", :layout => true
+ end
+
+ def render_text_with_custom_layout
+ render :text => "hello world", :layout => "greetings"
+ end
end
class TestSimpleTextRender < SimpleRouteCase
- describe "Rendering text from a action with default options"
+ describe "Rendering text from a action with default options renders the text without the layout"
get "/happy_path/render_text/render_hello_world_from_variable"
assert_body "hello david"
@@ -68,4 +80,20 @@ class TestTextRenderWithFalse < SimpleRouteCase
assert_body "false"
assert_status 200
end
+
+ class TestTextRenderWithLayoutTrue < SimpleRouteCase
+ describe "Rendering text with :layout => true"
+
+ get "/happy_path/render_text/render_text_with_layout"
+ assert_body "hello world, I'm here!"
+ assert_status 200
+ end
+
+ class TestTextRenderWithCustomLayout < SimpleRouteCase
+ describe "Rendering text with :layout => 'greetings'"
+
+ get "/happy_path/render_text/render_text_with_custom_layout"
+ assert_body "hello world, I wish thee well."
+ assert_status 200
+ end
end

1 comment on commit 81e814a

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