From d1157e7242d248b37546800bf7816c3035b56ce8 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Fri, 27 Feb 2009 11:42:13 -0800 Subject: AbstractController now supports layouts and rendering --- actionpack/lib/action_controller/abstract/base.rb | 9 +- .../lib/action_controller/abstract/layouts.rb | 10 ++ .../lib/action_controller/abstract/logger.rb | 7 ++ .../lib/action_controller/abstract/renderer.rb | 26 ++++-- actionpack/lib/action_view/template/template.rb | 2 +- .../abstract_controller_test.rb | 101 ++++++++++++++++++++- .../testing/me3/formatted.html.erb | 1 + .../abstract_controller/testing/me3/index.erb | 1 + .../abstract_controller/testing/me4/index.erb | 1 + .../abstract_controller/testing/me5/index.erb | 1 + .../views/action_with_ivars.erb | 1 + .../layouts/abstract_controller/testing/me4.erb | 1 + .../views/layouts/application.erb | 1 + .../abstract_controller/views/naked_render.erb | 1 + 14 files changed, 151 insertions(+), 12 deletions(-) create mode 100644 actionpack/lib/action_controller/abstract/layouts.rb create mode 100644 actionpack/lib/action_controller/abstract/logger.rb create mode 100644 actionpack/test/abstract_controller/views/abstract_controller/testing/me3/formatted.html.erb create mode 100644 actionpack/test/abstract_controller/views/abstract_controller/testing/me3/index.erb create mode 100644 actionpack/test/abstract_controller/views/abstract_controller/testing/me4/index.erb create mode 100644 actionpack/test/abstract_controller/views/abstract_controller/testing/me5/index.erb create mode 100644 actionpack/test/abstract_controller/views/action_with_ivars.erb create mode 100644 actionpack/test/abstract_controller/views/layouts/abstract_controller/testing/me4.erb create mode 100644 actionpack/test/abstract_controller/views/layouts/application.erb create mode 100644 actionpack/test/abstract_controller/views/naked_render.erb diff --git a/actionpack/lib/action_controller/abstract/base.rb b/actionpack/lib/action_controller/abstract/base.rb index c139531956..6ff4ed8dd2 100644 --- a/actionpack/lib/action_controller/abstract/base.rb +++ b/actionpack/lib/action_controller/abstract/base.rb @@ -3,8 +3,8 @@ module AbstractController attr_internal :response_body attr_internal :response_obj - cattr_accessor :logger - + attr_internal :action_name + def self.process(action) new.process(action) end @@ -13,8 +13,9 @@ module AbstractController self.response_obj = {} end - def process(action) - send(action) + def process(action_name) + @_action_name = action_name + send(action_name) self.response_obj[:body] = self.response_body self end diff --git a/actionpack/lib/action_controller/abstract/layouts.rb b/actionpack/lib/action_controller/abstract/layouts.rb new file mode 100644 index 0000000000..c8d6e77fce --- /dev/null +++ b/actionpack/lib/action_controller/abstract/layouts.rb @@ -0,0 +1,10 @@ +module AbstractController + module Layouts + def _render_template(tmp) + _action_view._render_template_with_layout(tmp, _layout) + end + + def _layout + end + end +end \ No newline at end of file diff --git a/actionpack/lib/action_controller/abstract/logger.rb b/actionpack/lib/action_controller/abstract/logger.rb new file mode 100644 index 0000000000..846d8ad040 --- /dev/null +++ b/actionpack/lib/action_controller/abstract/logger.rb @@ -0,0 +1,7 @@ +module AbstractController + module Logger + def self.included(klass) + klass.cattr_accessor :logger + end + end +end \ No newline at end of file diff --git a/actionpack/lib/action_controller/abstract/renderer.rb b/actionpack/lib/action_controller/abstract/renderer.rb index 7999bc1b70..dce411be92 100644 --- a/actionpack/lib/action_controller/abstract/renderer.rb +++ b/actionpack/lib/action_controller/abstract/renderer.rb @@ -1,21 +1,35 @@ +require "action_controller/abstract/logger" + module AbstractController module Renderer def self.included(klass) - klass.extend ClassMethods - klass.extlib_inheritable_accessor :view_paths - klass.view_paths ||= ActionView::PathSet.new + klass.class_eval do + extend ClassMethods + attr_internal :formats + + extlib_inheritable_accessor :view_paths + self.view_paths ||= ActionView::PathSet.new + include AbstractController::Logger + end end def _action_view @_action_view ||= ActionView::Base.new(self.class.view_paths, {}, self) end - def render(template) - tmp = view_paths.find_by_parts(template) - self.response_body = _action_view._render_template_with_layout(tmp) + def _prefix end + def render(template = action_name) + tmp = view_paths.find_by_parts(template.to_s, formats, _prefix) + self.response_body = _render_template(tmp) + end + + def _render_template(tmp) + _action_view._render_template_with_layout(tmp) + end + module ClassMethods def append_view_path(path) self.view_paths << path diff --git a/actionpack/lib/action_view/template/template.rb b/actionpack/lib/action_view/template/template.rb index 4b4b80d48c..1ee073c3e9 100644 --- a/actionpack/lib/action_view/template/template.rb +++ b/actionpack/lib/action_view/template/template.rb @@ -62,7 +62,7 @@ module ActionView #:nodoc: template = find_template(extensioned_path) || find_template(path) break if template end - template + template || find_template(path) end private diff --git a/actionpack/test/abstract_controller/abstract_controller_test.rb b/actionpack/test/abstract_controller/abstract_controller_test.rb index a5026fb0da..4834f8b7bb 100644 --- a/actionpack/test/abstract_controller/abstract_controller_test.rb +++ b/actionpack/test/abstract_controller/abstract_controller_test.rb @@ -5,6 +5,7 @@ require 'test/unit' require 'active_support' require 'active_support/test_case' require 'action_controller' +require 'action_view/base' begin require 'ruby-debug' @@ -16,6 +17,7 @@ end require 'action_controller/abstract/base' require 'action_controller/abstract/renderer' +require 'action_controller/abstract/layouts' module AbstractController module Testing @@ -27,7 +29,7 @@ module AbstractController def index self.response_body = "Hello world" "Something else" - end + end end class TestBasic < ActiveSupport::TestCase @@ -47,6 +49,15 @@ module AbstractController def index render "index.erb" end + + def action_with_ivars + @my_ivar = "Hello" + render "action_with_ivars.erb" + end + + def naked_render + render + end end class TestRenderer < ActiveSupport::TestCase @@ -54,6 +65,94 @@ module AbstractController result = Me2.process(:index) assert_equal "Hello from index.erb", result.response_obj[:body] end + + test "rendering passes ivars to the view" do + result = Me2.process(:action_with_ivars) + assert_equal "Hello from index_with_ivars.erb", result.response_obj[:body] + end + + test "rendering with no template name" do + result = Me2.process(:naked_render) + assert_equal "Hello from naked_render.erb", result.response_obj[:body] + end + end + + class PrefixedViews < RenderingController + private + def self.prefix + name.underscore + end + + def _prefix + self.class.prefix + end + end + + class Me3 < PrefixedViews + def index + render + end + + def formatted + self.formats = [:html] + render + end + end + + class TestPrefixedViews < ActiveSupport::TestCase + test "templates are located inside their 'prefix' folder" do + result = Me3.process(:index) + assert_equal "Hello from me3/index.erb", result.response_obj[:body] + end + + test "templates included their format" do + result = Me3.process(:formatted) + assert_equal "Hello from me3/formatted.html.erb", result.response_obj[:body] + end + end + + class WithLayouts < PrefixedViews + include Layouts + + private + def self.layout(formats) + begin + view_paths.find_by_parts(name.underscore, formats, "layouts") + rescue ActionView::MissingTemplate + begin + view_paths.find_by_parts("application", formats, "layouts") + rescue ActionView::MissingTemplate + end + end + end + + def _layout + self.class.layout(formats) + end + end + + class Me4 < WithLayouts + def index + render + end + end + + class Me5 < WithLayouts + def index + render + end + end + + class TestLayouts < ActiveSupport::TestCase + test "layouts are included" do + result = Me4.process(:index) + assert_equal "Me4 Enter : Hello from me4/index.erb : Exit", result.response_obj[:body] + end + + test "it can fall back to the application layout" do + result = Me5.process(:index) + assert_equal "Application Enter : Hello from me5/index.erb : Exit", result.response_obj[:body] + end end end diff --git a/actionpack/test/abstract_controller/views/abstract_controller/testing/me3/formatted.html.erb b/actionpack/test/abstract_controller/views/abstract_controller/testing/me3/formatted.html.erb new file mode 100644 index 0000000000..785bf69191 --- /dev/null +++ b/actionpack/test/abstract_controller/views/abstract_controller/testing/me3/formatted.html.erb @@ -0,0 +1 @@ +Hello from me3/formatted.html.erb \ No newline at end of file diff --git a/actionpack/test/abstract_controller/views/abstract_controller/testing/me3/index.erb b/actionpack/test/abstract_controller/views/abstract_controller/testing/me3/index.erb new file mode 100644 index 0000000000..f079ad8204 --- /dev/null +++ b/actionpack/test/abstract_controller/views/abstract_controller/testing/me3/index.erb @@ -0,0 +1 @@ +Hello from me3/index.erb \ No newline at end of file diff --git a/actionpack/test/abstract_controller/views/abstract_controller/testing/me4/index.erb b/actionpack/test/abstract_controller/views/abstract_controller/testing/me4/index.erb new file mode 100644 index 0000000000..89dce12bdc --- /dev/null +++ b/actionpack/test/abstract_controller/views/abstract_controller/testing/me4/index.erb @@ -0,0 +1 @@ +Hello from me4/index.erb \ No newline at end of file diff --git a/actionpack/test/abstract_controller/views/abstract_controller/testing/me5/index.erb b/actionpack/test/abstract_controller/views/abstract_controller/testing/me5/index.erb new file mode 100644 index 0000000000..84d0b7417e --- /dev/null +++ b/actionpack/test/abstract_controller/views/abstract_controller/testing/me5/index.erb @@ -0,0 +1 @@ +Hello from me5/index.erb \ No newline at end of file diff --git a/actionpack/test/abstract_controller/views/action_with_ivars.erb b/actionpack/test/abstract_controller/views/action_with_ivars.erb new file mode 100644 index 0000000000..8d8ae22fd7 --- /dev/null +++ b/actionpack/test/abstract_controller/views/action_with_ivars.erb @@ -0,0 +1 @@ +<%= @my_ivar %> from index_with_ivars.erb \ No newline at end of file diff --git a/actionpack/test/abstract_controller/views/layouts/abstract_controller/testing/me4.erb b/actionpack/test/abstract_controller/views/layouts/abstract_controller/testing/me4.erb new file mode 100644 index 0000000000..172dd56569 --- /dev/null +++ b/actionpack/test/abstract_controller/views/layouts/abstract_controller/testing/me4.erb @@ -0,0 +1 @@ +Me4 Enter : <%= yield %> : Exit \ No newline at end of file diff --git a/actionpack/test/abstract_controller/views/layouts/application.erb b/actionpack/test/abstract_controller/views/layouts/application.erb new file mode 100644 index 0000000000..27317140ad --- /dev/null +++ b/actionpack/test/abstract_controller/views/layouts/application.erb @@ -0,0 +1 @@ +Application Enter : <%= yield %> : Exit \ No newline at end of file diff --git a/actionpack/test/abstract_controller/views/naked_render.erb b/actionpack/test/abstract_controller/views/naked_render.erb new file mode 100644 index 0000000000..1b3d03878b --- /dev/null +++ b/actionpack/test/abstract_controller/views/naked_render.erb @@ -0,0 +1 @@ +Hello from naked_render.erb \ No newline at end of file -- cgit v1.2.3