diff options
author | Yehuda Katz <wycats@gmail.com> | 2009-03-18 15:58:47 -0700 |
---|---|---|
committer | Yehuda Katz <wycats@gmail.com> | 2009-03-18 15:58:47 -0700 |
commit | e0447023db7152d3ecdf693bd9aa36c7daa02653 (patch) | |
tree | 6819a42c250674f0d5f48849c1c597a9b54db5f3 | |
parent | fb626ee39065512928c90c396db8b5476c5a7aeb (diff) | |
download | rails-e0447023db7152d3ecdf693bd9aa36c7daa02653.tar.gz rails-e0447023db7152d3ecdf693bd9aa36c7daa02653.tar.bz2 rails-e0447023db7152d3ecdf693bd9aa36c7daa02653.zip |
Implemented basic template rendering in AC::Base2:
* Created several macros for writing simpler specs
* Finished making Rack::Test work right
* Implemented render_to_string
* Status Codes
* render :text => nil
-rw-r--r-- | actionpack/lib/action_controller/abstract/renderer.rb | 6 | ||||
-rw-r--r-- | actionpack/lib/action_controller/new_base/renderer.rb | 27 | ||||
-rw-r--r-- | actionpack/test/fixtures/test/basic.html.erb | 1 | ||||
-rw-r--r-- | actionpack/test/new_base/base_test.rb | 88 | ||||
-rw-r--r-- | actionpack/test/new_base/render_test.rb | 124 | ||||
-rw-r--r-- | actionpack/test/new_base/test_helper.rb | 133 |
6 files changed, 289 insertions, 90 deletions
diff --git a/actionpack/lib/action_controller/abstract/renderer.rb b/actionpack/lib/action_controller/abstract/renderer.rb index dce411be92..537335aa0e 100644 --- a/actionpack/lib/action_controller/abstract/renderer.rb +++ b/actionpack/lib/action_controller/abstract/renderer.rb @@ -22,8 +22,12 @@ module AbstractController end 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) - self.response_body = _render_template(tmp) + _render_template(tmp) end def _render_template(tmp) diff --git a/actionpack/lib/action_controller/new_base/renderer.rb b/actionpack/lib/action_controller/new_base/renderer.rb index 503450c246..eb3c8b808d 100644 --- a/actionpack/lib/action_controller/new_base/renderer.rb +++ b/actionpack/lib/action_controller/new_base/renderer.rb @@ -2,10 +2,33 @@ module ActionController module Renderer def render(options) - if text = options[:text] - self.response_body = text + _process_options(options) + + self.response_body = render_to_string(options) + end + + def render_to_string(options) + self.formats = [:html] + + if options.key?(:text) + text = options.delete(:text) + + case text + when nil then " " + else text.to_s + end + elsif options.key?(:template) + template = options.delete(:template) + + super(template) end end + private + def _process_options(options) + if status = options.delete(:status) + response.status = status.to_i + end + end end end
\ No newline at end of file diff --git a/actionpack/test/fixtures/test/basic.html.erb b/actionpack/test/fixtures/test/basic.html.erb new file mode 100644 index 0000000000..ea696d7e01 --- /dev/null +++ b/actionpack/test/fixtures/test/basic.html.erb @@ -0,0 +1 @@ +Hello from basic.html.erb
\ No newline at end of file diff --git a/actionpack/test/new_base/base_test.rb b/actionpack/test/new_base/base_test.rb index 9609c11753..4f46cb6492 100644 --- a/actionpack/test/new_base/base_test.rb +++ b/actionpack/test/new_base/base_test.rb @@ -1,82 +1,4 @@ -$:.unshift(File.dirname(__FILE__) + '/../../lib') -$:.unshift(File.dirname(__FILE__) + '/../../../activesupport/lib') - -require 'test/unit' -require 'active_support' -require 'active_support/test_case' -require 'action_controller' -require 'action_view/base' - -begin - require 'ruby-debug' - Debugger.settings[:autoeval] = true - Debugger.start -rescue LoadError - # Debugging disabled. `gem install ruby-debug` to enable. -end - -require 'action_controller/abstract' -require 'action_controller/new_base' -require 'pp' # require 'pp' early to prevent hidden_methods from not picking up the pretty-print methods until too late - -require 'rubygems' -require 'rack/test' - -module ActionController - class Base2 < AbstractBase - include AbstractController::Callbacks - include AbstractController::Renderer - include AbstractController::Helpers - include AbstractController::Layouts - include AbstractController::Logger - - include ActionController::HideActions - include ActionController::UrlFor - include ActionController::Renderer - - CORE_METHODS = self.public_instance_methods - end -end - -# Temporary base class -class Rack::TestCase < ActiveSupport::TestCase - - include Rack::Test::Methods - - setup do - ActionController::Base.session_options[:key] = "abc" - ActionController::Base.session_options[:secret] = ("*" * 30) - ActionController::Routing.use_controllers! %w(happy_path/simple_dispatch) - end - - def self.get(url) - setup do |test| - test.get url - end - end - - def app - @app ||= ActionController::Dispatcher.new - end - - def assert_body(body) - assert_equal [body], last_response.body - end - - def assert_status(code) - assert_equal code, last_response.status - end - - def assert_content_type(type) - assert_equal type, last_response.headers["Content-Type"] - end - - def assert_header(name, value) - assert_equal value, last_response.headers[name] - end - -end - +require File.join(File.expand_path(File.dirname(__FILE__)), "test_helper") # Tests the controller dispatching happy path module HappyPath @@ -99,14 +21,6 @@ module HappyPath end end - class SimpleRouteCase < Rack::TestCase - setup do - ActionController::Routing::Routes.draw do |map| - map.connect ':controller/:action' - end - end - end - class TestSimpleDispatch < SimpleRouteCase get "/happy_path/simple_dispatch/index" diff --git a/actionpack/test/new_base/render_test.rb b/actionpack/test/new_base/render_test.rb new file mode 100644 index 0000000000..2f43bc1fc6 --- /dev/null +++ b/actionpack/test/new_base/render_test.rb @@ -0,0 +1,124 @@ +require File.join(File.expand_path(File.dirname(__FILE__)), "test_helper") + +module HappyPath + + class RenderTextController < ActionController::Base2 + def render_hello_world_from_variable + @person = "david" + render :text => "hello #{@person}" + end + + def render_custom_code + render :text => "hello world", :status => 404 + end + + def render_with_custom_code_as_string + render :text => "hello world", :status => "404 Not Found" + end + + def render_text_with_nil + render :text => nil + end + + def render_text_with_nil_and_status + render :text => nil, :status => 403 + end + + def render_text_with_false + render :text => false + end + end + + class TestSimpleTextRender < SimpleRouteCase + describe "Rendering text from a action with default options" + + get "/happy_path/render_text/render_hello_world_from_variable" + assert_body "hello david" + assert_status 200 + end + + class TestTextRenderWithStatus < SimpleRouteCase + describe "Rendering text, while also providing a custom status code" + + get "/happy_path/render_text/render_custom_code" + assert_body "hello world" + assert_status 404 + end + + class TestTextRenderWithNil < SimpleRouteCase + describe "Rendering text with nil returns a single space character" + + get "/happy_path/render_text/render_text_with_nil" + assert_body " " + assert_status 200 + end + + class TestTextRenderWithNilAndStatus < SimpleRouteCase + describe "Rendering text with nil and custom status code returns a single space character with the status" + + get "/happy_path/render_text/render_text_with_nil_and_status" + assert_body " " + assert_status 403 + end + + class TestTextRenderWithFalse < SimpleRouteCase + describe "Rendering text with false returns the string 'false'" + + get "/happy_path/render_text/render_text_with_false" + 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 diff --git a/actionpack/test/new_base/test_helper.rb b/actionpack/test/new_base/test_helper.rb new file mode 100644 index 0000000000..7a1adc3755 --- /dev/null +++ b/actionpack/test/new_base/test_helper.rb @@ -0,0 +1,133 @@ +$:.unshift(File.dirname(__FILE__) + '/../../lib') +$:.unshift(File.dirname(__FILE__) + '/../../../activesupport/lib') + +require 'test/unit' +require 'active_support' +require 'active_support/test_case' +require 'action_controller' +require 'action_view/base' + +begin + require 'ruby-debug' + Debugger.settings[:autoeval] = true + Debugger.start +rescue LoadError + # Debugging disabled. `gem install ruby-debug` to enable. +end + +require 'action_controller/abstract' +require 'action_controller/new_base' +require 'pp' # require 'pp' early to prevent hidden_methods from not picking up the pretty-print methods until too late + +require 'rubygems' +require 'rack/test' + +module ActionController + class Base2 < AbstractBase + include AbstractController::Callbacks + include AbstractController::Renderer + include AbstractController::Helpers + include AbstractController::Layouts + include AbstractController::Logger + + include ActionController::HideActions + include ActionController::UrlFor + include ActionController::Renderer + + def self.inherited(klass) + @subclasses ||= [] + @subclasses << klass.to_s + end + + def self.subclasses + @subclasses + end + + append_view_path File.join(File.dirname(__FILE__), '..', 'fixtures') + + CORE_METHODS = self.public_instance_methods + end +end + +# Temporary base class +class Rack::TestCase < ActiveSupport::TestCase + include Rack::Test::Methods + + setup do + ActionController::Base.session_options[:key] = "abc" + ActionController::Base.session_options[:secret] = ("*" * 30) + + controllers = ActionController::Base2.subclasses.map do |k| + k.underscore.sub(/_controller$/, '') + end + + ActionController::Routing.use_controllers!(controllers) + end + + def self.describe(text) + class_eval <<-RUBY_EVAL + def self.name + "#{text}" + end + RUBY_EVAL + end + + def app + @app ||= ActionController::Dispatcher.new + end + + def self.get(url) + setup do |test| + test.get url + end + end + + def assert_body(body) + assert_equal [body], last_response.body + end + + def self.assert_body(body) + test "body is set to '#{body}'" do + assert_body body + end + end + + def assert_status(code) + assert_equal code, last_response.status + end + + def self.assert_status(code) + test "status code is set to #{code}" do + assert_status code + end + end + + def assert_content_type(type) + assert_equal type, last_response.headers["Content-Type"] + end + + def self.assert_content_type(type) + test "content type is set to #{type}" do + assert_content_type(type) + end + end + + def assert_header(name, value) + assert_equal value, last_response.headers[name] + end + + def self.assert_header(name, value) + test "'#{name}' header is set to #{value.inspect}" do + assert_header(name, value) + end + end + +end + +class SimpleRouteCase < Rack::TestCase + setup do + ActionController::Routing::Routes.draw do |map| + map.connect ':controller/:action/:id' + end + end +end
\ No newline at end of file |