-
Notifications
You must be signed in to change notification settings - Fork 21.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
139 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,7 +1,43 @@ | |||
module AbstractController | module AbstractController | ||
module Layouts | module Layouts | ||
|
|||
def self.included(base) | |||
base.extend ClassMethods | |||
end | |||
|
|||
module ClassMethods | |||
def _layout() end | |||
end | |||
|
|||
def _render_template(template, options) | def _render_template(template, options) | ||
_action_view._render_template_with_layout(template, options[:_layout]) | _action_view._render_template_with_layout(template, options[:_layout]) | ||
end | end | ||
|
|||
private | |||
|
|||
def _layout_for_option(name) | |||
case name | |||
when String then _layout_for_name(name) | |||
when true then _default_layout(true) | |||
when false then nil | |||
end | |||
end | |||
|
|||
def _layout_for_name(name) | |||
view_paths.find_by_parts(name, formats, "layouts") | |||
end | |||
|
|||
def _default_layout(require_layout = false) | |||
# begin | |||
# _layout_for_name(controller_path) | |||
# rescue ActionView::MissingTemplate | |||
# begin | |||
# _layout_for_name("application") | |||
# rescue ActionView::MissingTemplate => e | |||
# raise e if require_layout | |||
# end | |||
# end | |||
_layout_for_option(self.class._layout) | |||
end | |||
end | end | ||
end | end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,64 @@ | |||
require File.join(File.expand_path(File.dirname(__FILE__)), "test_helper") | |||
|
|||
module AbstractControllerTests | |||
module Layouts | |||
|
|||
# Base controller for these tests | |||
class Base < AbstractController::Base | |||
include AbstractController::Renderer | |||
include AbstractController::Layouts | |||
|
|||
self.view_paths = [ActionView::FixtureTemplate::FixturePath.new( | |||
"layouts/hello.html.erb" => "With String <%= yield %>" | |||
)] | |||
|
|||
def render_to_string(options) | |||
options[:_layout] = _default_layout | |||
super | |||
end | |||
end | |||
|
|||
class Blank < Base | |||
self.view_paths = [] | |||
|
|||
def index | |||
render :_template => ActionView::TextTemplate.new("Hello blank!") | |||
end | |||
end | |||
|
|||
class WithString < Base | |||
layout "hello" | |||
|
|||
def index | |||
render :_template => ActionView::TextTemplate.new("Hello string!") | |||
end | |||
end | |||
|
|||
class WithMissingLayout < Base | |||
layout "missing" | |||
|
|||
def index | |||
render :_template => ActionView::TextTemplate.new("Hello missing!") | |||
end | |||
end | |||
|
|||
|
|||
class TestBase < ActiveSupport::TestCase | |||
test "when no layout is specified, and no default is available, render without a layout" do | |||
result = Blank.process(:index) | |||
assert_equal "Hello blank!", result.response_obj[:body] | |||
end | |||
|
|||
test "when layout is specified as a string, render with that layout" do | |||
result = Blank.process(:index) | |||
assert_equal "With String Hello string!", result.response_obj[:body] | |||
end | |||
|
|||
test "when layout is specified as a string, but the layout is missing, raise an exception" do | |||
assert_raises(ActionView::MissingTemplate) { WithMissingLayout.process(:index) } | |||
end | |||
end | |||
|
|||
|
|||
end | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,35 @@ | |||
module ActionView #:nodoc: | |||
class FixtureTemplate < Template | |||
class FixturePath < Template::Path | |||
def initialize(hash = {}) | |||
@hash = {} | |||
|
|||
hash.each do |k, v| | |||
@hash[k.sub(/\.\w+$/, '')] = FixtureTemplate.new(v, k.split("/").last, self) | |||
end | |||
|
|||
super("fixtures://root") | |||
end | |||
|
|||
def find_template(path) | |||
@hash[path] | |||
end | |||
end | |||
|
|||
def initialize(body, *args) | |||
@body = body | |||
super(*args) | |||
end | |||
|
|||
def source | |||
@body | |||
end | |||
|
|||
private | |||
|
|||
def find_full_path(path, load_paths) | |||
return '/', path | |||
end | |||
|
|||
end | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters