From 8ab37c76608d7105c47566e79b85fcf72cb11e4b Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Thu, 19 Mar 2009 13:35:39 -0700 Subject: [PATCH] Started implementing render :action --- .../action_controller/abstract/renderer.rb | 4 +- .../action_controller/new_base/renderer.rb | 52 ++++++++++--- .../happy_path/render_action/hello_world.erb | 1 + .../test/new_base/render_action_test.rb | 75 +++++++++++++++++++ .../new_base/render_implicit_action_test.rb | 16 ++++ .../test/new_base/render_template_test.rb | 56 ++++++++++++++ .../{render_test.rb => render_text_test.rb} | 53 ------------- 7 files changed, 192 insertions(+), 65 deletions(-) create mode 100644 actionpack/test/fixtures/happy_path/render_action/hello_world.erb create mode 100644 actionpack/test/new_base/render_action_test.rb create mode 100644 actionpack/test/new_base/render_implicit_action_test.rb create mode 100644 actionpack/test/new_base/render_template_test.rb rename actionpack/test/new_base/{render_test.rb => render_text_test.rb} (55%) diff --git a/actionpack/lib/action_controller/abstract/renderer.rb b/actionpack/lib/action_controller/abstract/renderer.rb index 537335aa0e..19a64b0c38 100644 --- a/actionpack/lib/action_controller/abstract/renderer.rb +++ b/actionpack/lib/action_controller/abstract/renderer.rb @@ -25,8 +25,8 @@ def render(template = action_name) self.response_body = render_to_string(template) end - def render_to_string(template = action_name) - tmp = view_paths.find_by_parts(template.to_s, formats, _prefix) + def render_to_string(template = action_name, prefix = true) + tmp = view_paths.find_by_parts(template.to_s, formats, (_prefix if prefix)) _render_template(tmp) end diff --git a/actionpack/lib/action_controller/new_base/renderer.rb b/actionpack/lib/action_controller/new_base/renderer.rb index eb3c8b808d..540924936d 100644 --- a/actionpack/lib/action_controller/new_base/renderer.rb +++ b/actionpack/lib/action_controller/new_base/renderer.rb @@ -1,7 +1,24 @@ module ActionController module Renderer - def render(options) + # def self.included(klass) + # klass.extend ClassMethods + # end + # + # module ClassMethods + # def prefix + # @prefix ||= name.underscore + # end + # end + + def render(action, options = {}) + # TODO: Move this into #render_to_string + if action.is_a?(Hash) + options, action = action, nil + else + options.merge! :action => action + end + _process_options(options) self.response_body = render_to_string(options) @@ -9,22 +26,37 @@ def render(options) def render_to_string(options) self.formats = [:html] + + unless options.is_a?(Hash) + options = {:action => options} + end if options.key?(:text) - text = options.delete(:text) - - case text - when nil then " " - else text.to_s - end + _render_text(options) elsif options.key?(:template) - template = options.delete(:template) - + template = options.delete(:template) + super(template, false) + elsif options.key?(:action) + template = options.delete(:action).to_s super(template) end end - private + private + + def _prefix + controller_path + end + + def _render_text(options) + text = options.delete(:text) + + case text + when nil then " " + else text.to_s + end + end + def _process_options(options) if status = options.delete(:status) response.status = status.to_i diff --git a/actionpack/test/fixtures/happy_path/render_action/hello_world.erb b/actionpack/test/fixtures/happy_path/render_action/hello_world.erb new file mode 100644 index 0000000000..6769dd60bd --- /dev/null +++ b/actionpack/test/fixtures/happy_path/render_action/hello_world.erb @@ -0,0 +1 @@ +Hello world! \ No newline at end of file diff --git a/actionpack/test/new_base/render_action_test.rb b/actionpack/test/new_base/render_action_test.rb new file mode 100644 index 0000000000..b6e98f82aa --- /dev/null +++ b/actionpack/test/new_base/render_action_test.rb @@ -0,0 +1,75 @@ +require File.join(File.expand_path(File.dirname(__FILE__)), "test_helper") + +module HappyPath + + class RenderActionController < ActionController::Base2 + + def render_action_hello_world + render :action => "hello_world" + end + + def render_action_hello_world_as_string + render "hello_world" + end + + def render_action_hello_world_as_string_with_options + render "hello_world", :status => 404 + end + + def render_action_hello_world_as_symbol + render :hello_world + end + + def render_action_hello_world_with_symbol + render :action => :hello_world + end + + end + + class TestRenderAction < SimpleRouteCase + + describe "Rendering an action using :action => " + + get "/happy_path/render_action/render_action_hello_world" + assert_body "Hello world!" + assert_status 200 + + end + + class TestRenderActionWithString < SimpleRouteCase + + describe "Render an action using 'hello_world'" + + get "/happy_path/render_action/render_action_hello_world_as_string" + assert_body "Hello world!" + assert_status 200 + + end + + class TestRenderActionWithStringAndOptions < SimpleRouteCase + + describe "Render an action using 'hello_world'" + + get "/happy_path/render_action/render_action_hello_world_as_string_with_options" + assert_body "Hello world!" + assert_status 404 + + end + + class TestRenderActionAsSymbol < SimpleRouteCase + describe "Render an action using :hello_world" + + get "/happy_path/render_action/render_action_hello_world_as_symbol" + assert_body "Hello world!" + assert_status 200 + end + + class TestRenderActionWithSymbol < SimpleRouteCase + describe "Render an action using :action => :hello_world" + + get "/happy_path/render_action/render_action_hello_world_with_symbol" + assert_body "Hello world!" + assert_status 200 + end + +end \ No newline at end of file diff --git a/actionpack/test/new_base/render_implicit_action_test.rb b/actionpack/test/new_base/render_implicit_action_test.rb new file mode 100644 index 0000000000..798505b539 --- /dev/null +++ b/actionpack/test/new_base/render_implicit_action_test.rb @@ -0,0 +1,16 @@ +require File.join(File.expand_path(File.dirname(__FILE__)), "test_helper") + +module HappyPath + + class RenderImplicitActionController < ActionController::Base2 + # No actions yet, they are implicit + end + + class TestRendersActionImplicitly < SimpleRouteCase + + test "renders action implicitly" do + assert true + end + + end +end \ No newline at end of file diff --git a/actionpack/test/new_base/render_template_test.rb b/actionpack/test/new_base/render_template_test.rb new file mode 100644 index 0000000000..758a206dbb --- /dev/null +++ b/actionpack/test/new_base/render_template_test.rb @@ -0,0 +1,56 @@ +require File.join(File.expand_path(File.dirname(__FILE__)), "test_helper") + +module HappyPath + + class RenderTemplateController < ActionController::Base2 + + def render_hello_world + render :template => "test/basic" + end + + def render_hello_world_with_forward_slash + render :template => "/test/basic" + end + + def render_template_in_top_directory + render :template => 'shared' + end + + def render_template_in_top_directory_with_slash + render :template => '/shared' + end + end + + class TestTemplateRender < SimpleRouteCase + describe "rendering a normal template with full path" + + get "/happy_path/render_template/render_hello_world" + assert_body "Hello from basic.html.erb" + assert_status 200 + end + + class TestTemplateRenderWithForwardSlash < SimpleRouteCase + describe "rendering a normal template with full path starting with a leading slash" + + get "/happy_path/render_template/render_hello_world_with_forward_slash" + assert_body "Hello from basic.html.erb" + assert_status 200 + end + + class TestTemplateRenderInTopDirectory < SimpleRouteCase + describe "rendering a template not in a subdirectory" + + get "/happy_path/render_template/render_template_in_top_directory" + assert_body "Elastica" + assert_status 200 + end + + class TestTemplateRenderInTopDirectoryWithSlash < SimpleRouteCase + describe "rendering a template not in a subdirectory with a leading slash" + + get "/happy_path/render_template/render_template_in_top_directory_with_slash" + assert_body "Elastica" + assert_status 200 + end + +end \ No newline at end of file diff --git a/actionpack/test/new_base/render_test.rb b/actionpack/test/new_base/render_text_test.rb similarity index 55% rename from actionpack/test/new_base/render_test.rb rename to actionpack/test/new_base/render_text_test.rb index 2f43bc1fc6..f845b4c9cc 100644 --- a/actionpack/test/new_base/render_test.rb +++ b/actionpack/test/new_base/render_text_test.rb @@ -68,57 +68,4 @@ class TestTextRenderWithFalse < SimpleRouteCase assert_body "false" assert_status 200 end - - class RenderTemplateController < ActionController::Base2 - - def render_hello_world - render :template => "test/basic" - end - - def render_hello_world_with_forward_slash - render :template => "/test/basic" - end - - def render_template_in_top_directory - render :template => 'shared' - end - - def render_template_in_top_directory_with_slash - render :template => '/shared' - end - end - - class TestTemplateRender < SimpleRouteCase - describe "rendering a normal template with full path" - - get "/happy_path/render_template/render_hello_world" - assert_body "Hello from basic.html.erb" - assert_status 200 - end - - class TestTemplateRenderWithForwardSlash < SimpleRouteCase - describe "rendering a normal template with full path starting with a leading slash" - - get "/happy_path/render_template/render_hello_world_with_forward_slash" - assert_body "Hello from basic.html.erb" - assert_status 200 - end - - class TestTemplateRenderInTopDirectory < SimpleRouteCase - describe "rendering a template not in a subdirectory" - - get "/happy_path/render_template/render_template_in_top_directory" - assert_body "Elastica" - assert_status 200 - end - - class TestTemplateRenderInTopDirectoryWithSlash < SimpleRouteCase - describe "rendering a template not in a subdirectory with a leading slash" - - get "/happy_path/render_template/render_template_in_top_directory_with_slash" - assert_body "Elastica" - assert_status 200 - end - - # TODO: Other language craziness end \ No newline at end of file