Started implementing render :action

This commit is contained in:
Yehuda Katz 2009-03-19 13:35:39 -07:00
parent e0447023db
commit 8ab37c7660
7 changed files with 192 additions and 65 deletions

View File

@ -25,8 +25,8 @@ module AbstractController
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

View File

@ -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 @@ module ActionController
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

View File

@ -0,0 +1 @@
Hello world!

View File

@ -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 => <String>"
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

View File

@ -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

View File

@ -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

View File

@ -68,57 +68,4 @@ module HappyPath
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