From 49834e088bf8d02a4f75793a42868f2aea8749a4 Mon Sep 17 00:00:00 2001 From: Yehuda Katz + Carl Lerche Date: Wed, 29 Apr 2009 17:32:39 -0700 Subject: Support implicit render and blank render --- actionpack/lib/action_controller/abstract/base.rb | 13 ++++++++- .../lib/action_controller/abstract/renderer.rb | 4 +-- .../lib/action_controller/new_base/renderer.rb | 9 +----- .../test/new_base/render_implicit_action_test.rb | 32 +++++++++++----------- actionpack/test/new_base/render_test.rb | 18 ++++++++++++ actionpack/test/new_base/test_helper.rb | 20 ++++++++++++++ 6 files changed, 69 insertions(+), 27 deletions(-) diff --git a/actionpack/lib/action_controller/abstract/base.rb b/actionpack/lib/action_controller/abstract/base.rb index ade7719cc0..a87a490a2d 100644 --- a/actionpack/lib/action_controller/abstract/base.rb +++ b/actionpack/lib/action_controller/abstract/base.rb @@ -29,10 +29,21 @@ module AbstractController private + # It is possible for respond_to?(action_name) to be false and + # respond_to?(:action_missing) to be false if respond_to_action? + # is overridden in a subclass. For instance, ActionController::Base + # overrides it to include the case where a template matching the + # action_name is found. def process_action - respond_to?(action_name) ? send(action_name) : send(:action_missing, action_name) + if respond_to?(action_name) then send(action_name) + elsif respond_to?(:action_missing, true) then send(:action_missing, action_name) + end end + # Override this to change the conditions that will raise an + # ActionNotFound error. If you accept a difference case, + # you must handle it by also overriding process_action and + # handling the case. def respond_to_action?(action_name) respond_to?(action_name) || respond_to?(:action_missing, true) end diff --git a/actionpack/lib/action_controller/abstract/renderer.rb b/actionpack/lib/action_controller/abstract/renderer.rb index e9a7ab4004..716b213c75 100644 --- a/actionpack/lib/action_controller/abstract/renderer.rb +++ b/actionpack/lib/action_controller/abstract/renderer.rb @@ -42,9 +42,9 @@ module AbstractController def render_to_body(options = {}) name = options[:_template_name] || action_name - template = options[:_template] || view_paths.find_by_parts(name.to_s, {:formats => formats}, options[:_prefix]) + options[:_template] ||= view_paths.find_by_parts(name.to_s, {:formats => formats}, options[:_prefix]) - _render_template(template, options) + _render_template(options[:_template], options) end # Raw rendering of a template to a string. diff --git a/actionpack/lib/action_controller/new_base/renderer.rb b/actionpack/lib/action_controller/new_base/renderer.rb index df07edbd90..096a63e406 100644 --- a/actionpack/lib/action_controller/new_base/renderer.rb +++ b/actionpack/lib/action_controller/new_base/renderer.rb @@ -7,14 +7,7 @@ module ActionController super end - def render(action, options = {}) - # TODO: Move this into #render_to_body - if action.is_a?(Hash) - options, action = action, nil - else - options.merge! :action => action - end - + def render(options = {}) _process_options(options) super(options) diff --git a/actionpack/test/new_base/render_implicit_action_test.rb b/actionpack/test/new_base/render_implicit_action_test.rb index 3afa444e3e..58f5cec181 100644 --- a/actionpack/test/new_base/render_implicit_action_test.rb +++ b/actionpack/test/new_base/render_implicit_action_test.rb @@ -4,25 +4,25 @@ module RenderImplicitAction class SimpleController < ::ApplicationController self.view_paths = [ActionView::Template::FixturePath.new( "render_implicit_action/simple/hello_world.html.erb" => "Hello world!", - "render_implicit_action/simple/hyphen-ated.html.erb" => "Hello hyphen-ated" + "render_implicit_action/simple/hyphen-ated.html.erb" => "Hello hyphen-ated!" )] def hello_world() end end - # class TestImplicitRender < SimpleRouteCase - # describe "render a simple action with new explicit call to render" - # - # get "/render_implicit_action/simple/hello_world" - # assert_body "Hello world!" - # assert_status 200 - # end - # - # class TestImplicitWithSpecialCharactersRender < SimpleRouteCase - # describe "render an action with a missing method and has special characters" - # - # get "/render_implicit_action/simple/hyphen-ated" - # assert_body "Hello hyphen-ated!" - # assert_status 200 - # end + class TestImplicitRender < SimpleRouteCase + describe "render a simple action with new explicit call to render" + + get "/render_implicit_action/simple/hello_world" + assert_body "Hello world!" + assert_status 200 + end + + class TestImplicitWithSpecialCharactersRender < SimpleRouteCase + describe "render an action with a missing method and has special characters" + + get "/render_implicit_action/simple/hyphen-ated" + assert_body "Hello hyphen-ated!" + 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_test.rb index 9ad79124b8..93bc8d854c 100644 --- a/actionpack/test/new_base/render_test.rb +++ b/actionpack/test/new_base/render_test.rb @@ -1,6 +1,24 @@ require File.join(File.expand_path(File.dirname(__FILE__)), "test_helper") module Render + class BlankRenderController < ActionController::Base2 + self.view_paths = [ActionView::Template::FixturePath.new( + "render/blank_render/index.html.erb" => "Hello world!" + )] + + def index + render + end + end + + class TestBlankRender < SimpleRouteCase + describe "Render with blank" + + get "/render/blank_render" + assert_body "Hello world!" + assert_status 200 + end + class DoubleRenderController < ActionController::Base2 def index render :text => "hello" diff --git a/actionpack/test/new_base/test_helper.rb b/actionpack/test/new_base/test_helper.rb index b40cbb163f..03af5a66a6 100644 --- a/actionpack/test/new_base/test_helper.rb +++ b/actionpack/test/new_base/test_helper.rb @@ -58,11 +58,31 @@ module ActionController end end + def render(action = action_name, options = {}) + if action.is_a?(Hash) + options, action = action, nil + else + options.merge! :action => action + end + + super(options) + end + def render_to_body(options = {}) options = {:template => options} if options.is_a?(String) super end + def process_action + ret = super + render if response_body.nil? + ret + end + + def respond_to_action?(action_name) + super || view_paths.find_by_parts(action_name, {:formats => formats, :locales => [I18n.locale]}, controller_path) + end + # append_view_path File.join(File.dirname(__FILE__), '..', 'fixtures') CORE_METHODS = self.public_instance_methods -- cgit v1.2.3