Skip to content

Commit

Permalink
Merge branch 'master' into 0.5.x
Browse files Browse the repository at this point in the history
  • Loading branch information
jodosha committed Dec 18, 2015
2 parents 38576f9 + 104293d commit bcfcaab
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 50 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
# Lotus::View
View layer for Lotus

## v0.5.0 - (unreleased)
### Added
- [Luca Guidi] Added `Lotus::View::Configuration#default_encoding` to set the encoding for templates

### Fixed
- [Luca Guidi] Let exceptions to be raised as they occur in rendering context. This fixes misleading backtraces for exceptions.
- [Martin Rubi] Raise a `Lotus::View::MissingTemplateError` when rendering a missing partial from a template
- [Luca Guidi] Fix for `template.erb is not valid US-ASCII (Encoding::InvalidByteSequenceError)` when system encoding is not set

### Changed
- [Liam Dawson] Introduced `Lotus::View::Error` and let all the framework exceptions to inherit from it.

## v0.4.4 - 2015-09-30
### Added
- [Luca Guidi] Autoescape for layout helpers.
Expand Down
31 changes: 1 addition & 30 deletions lib/lotus/view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require 'lotus/view/rendering'
require 'lotus/view/escape'
require 'lotus/view/dsl'
require 'lotus/view/errors'
require 'lotus/layout'
require 'lotus/presenter'

Expand All @@ -15,37 +16,7 @@ module Lotus
#
# @since 0.1.0
module View
# Missing template error
#
# This is raised at the runtime when Lotus::View cannot find a template for
# the requested format.
#
# We can't raise this error during the loading phase, because at that time
# we don't know if a view implements its own rendering policy.
# A view is allowed to override `#render`, and this scenario can make the
# presence of a template useless. One typical example is the usage of a
# serializer that returns the output string, without rendering a template.
#
# @since 0.1.0
class MissingTemplateError < ::StandardError
def initialize(template, format)
super("Can't find template '#{ template }' for '#{ format }' format.")
end
end

# Missing format error
#
# This is raised at the runtime when rendering context lacks of the :format
# key.
#
# @since 0.1.0
#
# @see Lotus::View::Rendering#render
class MissingFormatError < ::StandardError
end

include Utils::ClassAttribute

# Framework configuration
#
# @since 0.2.0
Expand Down
47 changes: 47 additions & 0 deletions lib/lotus/view/errors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module Lotus
module View
# @since x.x.x
class Error < ::StandardError
end

# Missing template error
#
# This is raised at the runtime when Lotus::View cannot find a template for
# the requested format.
#
# We can't raise this error during the loading phase, because at that time
# we don't know if a view implements its own rendering policy.
# A view is allowed to override `#render`, and this scenario can make the
# presence of a template useless. One typical example is the usage of a
# serializer that returns the output string, without rendering a template.
#
# @since 0.1.0
class MissingTemplateError < Error
def initialize(template, format)
super("Can't find template '#{ template }' for '#{ format }' format.")
end
end

# Missing format error
#
# This is raised at the runtime when rendering context lacks of the :format
# key.
#
# @since 0.1.0
#
# @see Lotus::View::Rendering#render
class MissingFormatError < Error
end

# Missing template layout error
#
# This is raised at the runtime when Lotus::Layout cannot find it's template.
#
# @since x.x.x
class MissingTemplateLayoutError < Error
def initialize(template)
super("Can't find layout template '#{ template }'")
end
end
end
end
10 changes: 0 additions & 10 deletions lib/lotus/view/rendering/layout_registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@
module Lotus
module View
module Rendering
# Missing template layout error
#
# This is raised at the runtime when Lotus::Layout cannot find it's template.
#
# @since 0.3.0
class MissingTemplateLayoutError < ::StandardError
def initialize(template)
super("Can't find layout template '#{ template }'")
end
end
# Holds the references of all the registered layouts.
# As now the registry is unique at the level of the framework.
#
Expand Down
2 changes: 0 additions & 2 deletions lib/lotus/view/rendering/layout_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,6 @@ def method_missing(m, *args, &blk)
else
::Lotus::View::Escape.html(super)
end
rescue ::NameError
::Kernel.raise ::NoMethodError.new("undefined method `#{ m }' for #{ self.inspect }", m)
end

def renderer(options)
Expand Down
16 changes: 15 additions & 1 deletion test/fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,12 @@ class RssIndex < Index
end
end

class BrokenLogic
def run
raise ArgumentError.new('nope')
end
end

class LayoutForScopeTest
def foo
'x'
Expand All @@ -325,9 +331,17 @@ def bar
'y'
end

def error_inside
def wrong_reference
unknown_method
end

def wrong_method
"string".unknown
end

def raise_error
BrokenLogic.new.run
end
end

module Store
Expand Down
5 changes: 3 additions & 2 deletions test/layout_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
end
end

it "raise error if template isn't found" do
it "raise subclassed error if template isn't found" do
Lotus::View.unload!

class MissingLayout
Expand All @@ -17,8 +17,9 @@ class MissingLayout

error = -> {
Lotus::View.load!
}.must_raise(Lotus::View::Rendering::MissingTemplateLayoutError)
}.must_raise(Lotus::View::MissingTemplateLayoutError)
error.message.must_include "Can't find layout template 'MissingLayout'"
error.class.ancestors.must_include Lotus::View::Error
end

it 'concrete methods are available in layout template' do
Expand Down
24 changes: 19 additions & 5 deletions test/view/rendering/layout_scope_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,30 @@
end

describe 'undefined method on scope' do
it 'raises NoMethodError for unknown' do
it 'raises NoMethodError' do
exception = -> { @scope.unknown }.must_raise NoMethodError
exception.message.must_include 'undefined method `unknown'
end
end

describe 'undefined method inside method on scope' do
it 'raises NoMethodError for unknown' do
exception = -> { @scope.error_inside }.must_raise NoMethodError
exception.message.must_include 'undefined method `error_inside'
describe 'reference wrong method/variable' do
it 'raises NameError' do
exception = -> { @scope.wrong_reference }.must_raise NameError
exception.message.must_include "undefined local variable or method `unknown_method'"
end
end

describe 'undefined method for local variable' do
it 'raises NoMethodError' do
exception = -> { @scope.wrong_method }.must_raise NoMethodError
exception.message.must_include 'undefined method `unknown'
end
end

describe 'internal method invokation raises error' do
it 'raises that error' do
exception = -> { @scope.raise_error }.must_raise ArgumentError
exception.message.must_include 'nope'
end
end
end
Expand Down

0 comments on commit bcfcaab

Please sign in to comment.