Skip to content

Commit

Permalink
Get Base2 layouts to work :)
Browse files Browse the repository at this point in the history
  • Loading branch information
wycats committed Apr 8, 2009
1 parent 95c9718 commit 3cecbc2
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 22 deletions.
34 changes: 21 additions & 13 deletions actionpack/lib/action_controller/abstract/layouts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ def _implied_layout_name
name.underscore
end

# Takes the specified layout and creates a _layout method to be called
# by _default_layout
#
# If the specified layout is a:
# String:: return the string
# Symbol:: call the method specified by the symbol
# false:: return nil
# none:: If a layout is found in the view paths with the controller's
# name, return that string. Otherwise, use the superclass'
# layout (which might also be implied)
def _write_layout_method
case @_layout
when String
Expand Down Expand Up @@ -46,25 +56,23 @@ def _render_template(template, options)
private

def _layout() end # This will be overwritten

def _layout_for_option(name)
case name
when String then _layout_for_name(name)
when true then _default_layout(true)
when false, nil then nil
else
raise ArgumentError,
"String, true, or false, expected for `layout'; you passed #{name.inspect}"
end
end

def _layout_for_name(name)
view_paths.find_by_parts(name, formats, "layouts")
unless [String, FalseClass, NilClass].include?(name.class)
raise ArgumentError, "String, false, or nil expected; you passed #{name.inspect}"
end

name && view_paths.find_by_parts(name, formats, "layouts")
end

def _default_layout(require_layout = false)
if require_layout && !_layout
raise ArgumentError,
"There was no default layout for #{self.class} in #{view_paths.inspect}"
end

begin
_layout_for_option(_layout)
layout = _layout_for_name(_layout)
rescue NameError => e
raise NoMethodError,
"You specified #{@_layout.inspect} as the layout, but no such method was found"
Expand Down
23 changes: 23 additions & 0 deletions actionpack/lib/action_controller/new_base/layouts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,35 @@ module Layouts
depends_on ActionController::Renderer
depends_on AbstractController::Layouts

module ClassMethods
def _implied_layout_name
controller_path
end
end

def render_to_string(options)
# render :text => ..., :layout => ...
# or
# render :anything_else
if !options.key?(:text) || options.key?(:layout)
options[:_layout] = options.key?(:layout) ? _layout_for_option(options[:layout]) : _default_layout
end

super
end

private

def _layout_for_option(name)
case name
when String then _layout_for_name(name)
when true then _default_layout(true)
when false, nil then nil
else
raise ArgumentError,
"String, true, or false, expected for `layout'; you passed #{name.inspect}"
end
end

end
end
13 changes: 11 additions & 2 deletions actionpack/test/abstract_controller/layouts_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ def index
AbstractController::Base.subclasses.each do |klass|
klass = klass.constantize
next unless klass < AbstractController::Layouts
p klass
klass.class_eval do
_write_layout_method
end
Expand Down Expand Up @@ -217,7 +216,17 @@ class TestBase < ActiveSupport::TestCase
"parent has specified a layout, use the child controller layout" do
result = WithChildOfImplied.process(:index)
assert_equal "With Implied Hello string!", result.response_obj[:body]
end
end

test "raises an exception when specifying layout true" do
assert_raises ArgumentError do
Object.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
class ::BadOmgFailLolLayout < AbstractControllerTests::Layouts::Base
layout true
end
RUBY_EVAL
end
end
end
end
end
12 changes: 7 additions & 5 deletions actionpack/test/new_base/render_action_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ class TestLayoutTrue < SimpleRouteCase
describe "rendering a normal template with full path with layout => true"

test "raises an exception when requesting a layout and none exist" do
assert_raise(ActionView::MissingTemplate) { get "/render_action/basic/hello_world_with_layout" }
assert_raise(ArgumentError, /no default layout for RenderAction::BasicController in/) do
get "/render_action/basic/hello_world_with_layout"
end
end
end

Expand Down Expand Up @@ -124,13 +126,13 @@ class TestCustomLayout < SimpleRouteCase
module RenderActionWithApplicationLayout

# # ==== Render actions with layouts ====
class BasicController < ActionController::Base2

class BasicController < ::ApplicationController
# Set the view path to an application view structure with layouts
self.view_paths = self.view_paths = [ActionView::FixtureTemplate::FixturePath.new(
"render_action_with_application_layout/basic/hello_world.html.erb" => "Hello World!",
"layouts/application.html.erb" => "OHAI <%= yield %> KTHXBAI",
"layouts/greetings.html.erb" => "Greetings <%= yield %> Bai"
"layouts/application.html.erb" => "OHAI <%= yield %> KTHXBAI",
"layouts/greetings.html.erb" => "Greetings <%= yield %> Bai"
)]

def hello_world
Expand Down
45 changes: 45 additions & 0 deletions actionpack/test/new_base/render_layout_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require File.join(File.expand_path(File.dirname(__FILE__)), "test_helper")

module ControllerLayouts
class ImplicitController < ::ApplicationController

self.view_paths = [ActionView::FixtureTemplate::FixturePath.new(
"layouts/application.html.erb" => "OMG <%= yield %> KTHXBAI",
"basic.html.erb" => "Hello world!"
)]

def index
render :template => "basic"
end
end

class TestImplicitLayout < SimpleRouteCase
describe "rendering a normal template, but using the implicit layout"

get "/controller_layouts/implicit/index"
assert_body "OMG Hello world! KTHXBAI"
assert_status 200
end

class ImplicitNameController < ::ApplicationController

self.view_paths = [ActionView::FixtureTemplate::FixturePath.new(
"layouts/controller_layouts/implicit_name.html.erb" => "OMGIMPLICIT <%= yield %> KTHXBAI",
"basic.html.erb" => "Hello world!"
)]

def index
render :template => "basic"
end
end

class TestImplicitNamedLayout < SimpleRouteCase
describe "rendering a normal template, but using an implicit NAMED layout"

get "/controller_layouts/implicit_name/index"
assert_body "OMGIMPLICIT Hello world! KTHXBAI"
assert_status 200
end


end
4 changes: 2 additions & 2 deletions actionpack/test/new_base/render_template_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ class TestTemplateRenderInTopDirectoryWithSlash < SimpleRouteCase
assert_body "Elastica"
assert_status 200
end

class RenderTemplateWithLayoutController < ActionController::Base2
class RenderTemplateWithLayoutController < ::ApplicationController

self.view_paths = [ActionView::FixtureTemplate::FixturePath.new(
"test/basic.html.erb" => "Hello from basic.html.erb",
Expand Down
12 changes: 12 additions & 0 deletions actionpack/test/new_base/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ class Rack::TestCase < ActiveSupport::TestCase
end

ActionController::Routing.use_controllers!(controllers)

# Move into a bootloader
AbstractController::Base.subclasses.each do |klass|
klass = klass.constantize
next unless klass < AbstractController::Layouts
klass.class_eval do
_write_layout_method
end
end
end

def app
Expand Down Expand Up @@ -123,6 +132,9 @@ def self.assert_header(name, value)

end

class ::ApplicationController < ActionController::Base2
end

class SimpleRouteCase < Rack::TestCase
setup do
ActionController::Routing::Routes.draw do |map|
Expand Down

0 comments on commit 3cecbc2

Please sign in to comment.