diff options
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/lib/action_controller/abstract/layouts.rb | 36 | ||||
-rw-r--r-- | actionpack/lib/action_controller/new_base/layouts.rb | 26 | ||||
-rw-r--r-- | actionpack/test/abstract_controller/layouts_test.rb | 64 | ||||
-rw-r--r-- | actionpack/test/abstract_controller/test_helper.rb | 2 | ||||
-rw-r--r-- | actionpack/test/lib/fixture_template.rb | 35 | ||||
-rw-r--r-- | actionpack/test/new_base/test_helper.rb | 38 |
6 files changed, 139 insertions, 62 deletions
diff --git a/actionpack/lib/action_controller/abstract/layouts.rb b/actionpack/lib/action_controller/abstract/layouts.rb index c6b99a6d45..29b610610f 100644 --- a/actionpack/lib/action_controller/abstract/layouts.rb +++ b/actionpack/lib/action_controller/abstract/layouts.rb @@ -1,7 +1,43 @@ module AbstractController module Layouts + + def self.included(base) + base.extend ClassMethods + end + + module ClassMethods + def _layout() end + end + def _render_template(template, options) _action_view._render_template_with_layout(template, options[:_layout]) 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
\ No newline at end of file diff --git a/actionpack/lib/action_controller/new_base/layouts.rb b/actionpack/lib/action_controller/new_base/layouts.rb index 3b47a76077..c0efb669b2 100644 --- a/actionpack/lib/action_controller/new_base/layouts.rb +++ b/actionpack/lib/action_controller/new_base/layouts.rb @@ -7,31 +7,5 @@ module ActionController 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 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 - end end end
\ No newline at end of file diff --git a/actionpack/test/abstract_controller/layouts_test.rb b/actionpack/test/abstract_controller/layouts_test.rb new file mode 100644 index 0000000000..35329eea83 --- /dev/null +++ b/actionpack/test/abstract_controller/layouts_test.rb @@ -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
\ No newline at end of file diff --git a/actionpack/test/abstract_controller/test_helper.rb b/actionpack/test/abstract_controller/test_helper.rb index 5fbd3a9e23..b9248c6bbd 100644 --- a/actionpack/test/abstract_controller/test_helper.rb +++ b/actionpack/test/abstract_controller/test_helper.rb @@ -1,11 +1,13 @@ $:.unshift(File.dirname(__FILE__) + '/../../lib') $:.unshift(File.dirname(__FILE__) + '/../../../activesupport/lib') +$:.unshift(File.dirname(__FILE__) + '/../lib') require 'test/unit' require 'active_support' require 'active_support/test_case' require 'action_controller' require 'action_view/base' +require 'fixture_template' begin require 'ruby-debug' diff --git a/actionpack/test/lib/fixture_template.rb b/actionpack/test/lib/fixture_template.rb new file mode 100644 index 0000000000..26f6ec2d0c --- /dev/null +++ b/actionpack/test/lib/fixture_template.rb @@ -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
\ No newline at end of file diff --git a/actionpack/test/new_base/test_helper.rb b/actionpack/test/new_base/test_helper.rb index bb4667007b..e33b4239ad 100644 --- a/actionpack/test/new_base/test_helper.rb +++ b/actionpack/test/new_base/test_helper.rb @@ -1,11 +1,13 @@ $:.unshift(File.dirname(__FILE__) + '/../../lib') $:.unshift(File.dirname(__FILE__) + '/../../../activesupport/lib') +$:.unshift(File.dirname(__FILE__) + '/../lib') require 'test/unit' require 'active_support' require 'active_support/test_case' require 'action_controller' require 'action_view/base' +require 'fixture_template' begin require 'ruby-debug' @@ -50,42 +52,6 @@ module ActionController end end -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 - # Temporary base class class Rack::TestCase < ActiveSupport::TestCase include Rack::Test::Methods |