Skip to content

Commit

Permalink
Merge pull request #80 from mrubi/raise_template_not_found_error
Browse files Browse the repository at this point in the history
Raises an error when rendering a partial from another template.
  • Loading branch information
Trung Lê committed Oct 14, 2015
2 parents 8d0adf2 + 4bf5183 commit c57b8db
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/lotus/view/rendering/template.rb
Expand Up @@ -39,7 +39,9 @@ def initialize(view, options)
# @api private
# @since 0.1.0
def render
template.render(scope)
found_template = template
raise_missing_template_error if found_template.nil?
found_template.render(scope)
end

protected
Expand All @@ -50,6 +52,12 @@ def template
def scope
Scope.new(@view, @options[:locals])
end

def raise_missing_template_error
template_name = @options[:template] if @options.key?(:template)
template_name = @options[:partial] if @options.key?(:partial)
raise MissingTemplateError.new(template_name, @options[:format])
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures.rb
Expand Up @@ -39,6 +39,10 @@ def each_thing
end
end

class RenderViewWithMissingPartialTemplate
include Lotus::View
end

class JsonRenderView
include Lotus::View
format :json
Expand Down
@@ -0,0 +1 @@
<%= render partial: 'shared/missing_template' %>
7 changes: 7 additions & 0 deletions test/rendering_test.rb
Expand Up @@ -107,6 +107,13 @@
rendered.must_match %(<input type="hidden" name="secret" value="23" />)
end

it 'raises an error when the partial template is missing' do
-> {
RenderViewWithMissingPartialTemplate.render(format: :html)
}.must_raise(Lotus::View::MissingTemplateError)
.message.must_match("Can't find template 'shared/missing_template' for 'html' format.")
end

# @issue https://github.com/lotus/view/issues/3
it 'renders a template within another template' do
parent = OpenStruct.new(children: [], name: 'parent')
Expand Down

0 comments on commit c57b8db

Please sign in to comment.