Skip to content

Commit

Permalink
AbstractController now supports layouts and rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
wycats committed Feb 27, 2009
1 parent b1f078b commit d1157e7
Show file tree
Hide file tree
Showing 14 changed files with 151 additions and 12 deletions.
9 changes: 5 additions & 4 deletions actionpack/lib/action_controller/abstract/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ class Base

attr_internal :response_body
attr_internal :response_obj
cattr_accessor :logger
attr_internal :action_name

def self.process(action)
new.process(action)
end
Expand All @@ -13,8 +13,9 @@ def initialize
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
Expand Down
10 changes: 10 additions & 0 deletions actionpack/lib/action_controller/abstract/layouts.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions actionpack/lib/action_controller/abstract/logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module AbstractController
module Logger
def self.included(klass)
klass.cattr_accessor :logger
end
end
end
26 changes: 20 additions & 6 deletions actionpack/lib/action_controller/abstract/renderer.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_view/template/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def find_by_parts(name, extensions = nil, prefix = nil, partial = nil)
template = find_template(extensioned_path) || find_template(path)
break if template
end
template
template || find_template(path)
end

private
Expand Down
101 changes: 100 additions & 1 deletion actionpack/test/abstract_controller/abstract_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'active_support'
require 'active_support/test_case'
require 'action_controller'
require 'action_view/base'

begin
require 'ruby-debug'
Expand All @@ -16,6 +17,7 @@

require 'action_controller/abstract/base'
require 'action_controller/abstract/renderer'
require 'action_controller/abstract/layouts'

module AbstractController
module Testing
Expand All @@ -27,7 +29,7 @@ class Me < SimpleController
def index
self.response_body = "Hello world"
"Something else"
end
end
end

class TestBasic < ActiveSupport::TestCase
Expand All @@ -47,13 +49,110 @@ class Me2 < RenderingController
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
test "rendering templates works" do
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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello from me3/formatted.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello from me3/index.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello from me4/index.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello from me5/index.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= @my_ivar %> from index_with_ivars.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Me4 Enter : <%= yield %> : Exit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Application Enter : <%= yield %> : Exit
1 change: 1 addition & 0 deletions actionpack/test/abstract_controller/views/naked_render.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello from naked_render.erb

0 comments on commit d1157e7

Please sign in to comment.