diff --git a/lib/lotus/rendering_policy.rb b/lib/lotus/rendering_policy.rb index 7bc0ed577..94d17078a 100644 --- a/lib/lotus/rendering_policy.rb +++ b/lib/lotus/rendering_policy.rb @@ -23,6 +23,7 @@ def initialize(configuration) @controller_pattern = %r{#{ configuration.controller_pattern.gsub(/\%\{(controller|action)\}/) { "(?<#{ $1 }>(.*))" } }} @view_pattern = configuration.view_pattern @namespace = configuration.namespace + @templates = configuration.templates end def render(response) diff --git a/lib/lotus/views/default.rb b/lib/lotus/views/default.rb index d0115dc16..111bd458d 100644 --- a/lib/lotus/views/default.rb +++ b/lib/lotus/views/default.rb @@ -15,6 +15,19 @@ class Default def title response[2].first end + + def render + status_code = response[0] + templates = Lotus::Application.configuration.templates + template = "#{Pathname.new(templates).join(status_code.to_s)}.html.erb" + + if File.exists?(template) + scope = Lotus::View::Rendering::Scope.new(self) + Lotus::View::Template.new(template).render scope + else + super + end + end end end end diff --git a/test/fixtures/one_file/404.html.erb b/test/fixtures/one_file/404.html.erb new file mode 100644 index 000000000..a3434db53 --- /dev/null +++ b/test/fixtures/one_file/404.html.erb @@ -0,0 +1 @@ +Custom 404 page diff --git a/test/fixtures/one_file/500.html.erb b/test/fixtures/one_file/500.html.erb new file mode 100644 index 000000000..fb1d2445a --- /dev/null +++ b/test/fixtures/one_file/500.html.erb @@ -0,0 +1 @@ +Custom 500 page diff --git a/test/fixtures/one_file/application.rb b/test/fixtures/one_file/application.rb index d812c838b..f366b03f9 100644 --- a/test/fixtures/one_file/application.rb +++ b/test/fixtures/one_file/application.rb @@ -1,8 +1,12 @@ +require 'tilt/erb' + module OneFile class Application < Lotus::Application configure do routes do get '/', to: 'dashboard#index' + get '/error', to: 'dashboard#error' + get '/four_hundred', to: 'dashboard#four_hundred' end end @@ -17,5 +21,18 @@ def call(params) self.body = 'Hello' end end + + action 'Error' do + def call(params) + raise 'the roof' + end + end + + action 'FourHundred' do + def call(params) + self.status = 400 + self.body = 'Four Hundred' + end + end end end diff --git a/test/integration/error_page_test.rb b/test/integration/error_page_test.rb new file mode 100644 index 000000000..5fef9c5c9 --- /dev/null +++ b/test/integration/error_page_test.rb @@ -0,0 +1,50 @@ +require 'test_helper' +require 'rack/test' +require 'fixtures/one_file/application' + +describe 'Error page test' do + include Rack::Test::Methods + + before do + @current_dir = Dir.pwd + Dir.chdir FIXTURES_ROOT.join('one_file') + @app = OneFile::Application.new + end + + after do + Dir.chdir @current_dir + @current_dir = nil + end + + def app + @app + end + + def response + last_response + end + + it 'returns custom error pages' do + get '/four_hundred' + response.status.must_equal 400 + response.body.must_equal <<-EOF + + + + Four Hundred + + +

Four Hundred

+ + +EOF + + get '/error' + response.status.must_equal 500 + response.body.must_equal %(Custom 500 page\n) + + get '/foo' + response.status.must_equal 404 + response.body.must_equal %(Custom 404 page\n) + end +end