Skip to content

Commit

Permalink
Merge pull request #124 from dry-rb/prevent-needless-shared-partial-l…
Browse files Browse the repository at this point in the history
…ookup

Prevent needless partial lookup in `<template name>/shared`
  • Loading branch information
timriley authored Jan 29, 2019
2 parents 8950760 + f918caf commit 245f847
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
10 changes: 6 additions & 4 deletions lib/dry/view/path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ class Path

attr_reader :dir, :root

def initialize(dir, options = {})
def initialize(dir, root: dir)
@dir = Pathname(dir)
@root = Pathname(options.fetch(:root, dir))
@root = root
end

def lookup(name, format)
def lookup(name, format, include_shared: true)
fetch_or_store(dir, root, name, format) do
template?(name, format) || template?("shared/#{name}", format) || !root? && chdir('..').lookup(name, format)
template?(name, format) ||
(include_shared && template?("shared/#{name}", format)) ||
!root? && chdir('..').lookup(name, format)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/dry/view/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def chdir(dirname)

def lookup(name)
paths.inject(false) { |_, path|
result = path.lookup(name, format)
result = path.lookup(name, format, include_shared: false)
break result if result
}
end
Expand Down
13 changes: 13 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ def self.remove_constants
config.after do
Test.remove_constants
end

config.after do
[
Dry::View,
Dry::View::PartBuilder,
Dry::View::Path,
Dry::View::Renderer,
Dry::View::ScopeBuilder,
Dry::View::Tilt,
].each do |klass|
klass.cache.clear
end
end
end

RSpec::Matchers.define :part_including do |data|
Expand Down
18 changes: 13 additions & 5 deletions spec/unit/renderer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
expect(renderer.template(:hello, scope)).to eql('<h1>Hello</h1>')
end

it 'renders template in shared/ subdirectory' do
expect(renderer.template(:_shared_hello, scope)).to eql('<h1>Hello</h1>')
it 'does not include `shared/` subdirectory under root when looking up templates' do
expect {
renderer.template(:_shared_hello, scope)
}.to raise_error(Dry::View::Renderer::TemplateNotFoundError, /_shared_hello/)
end

it 'renders template in upper directory' do
it 'renders template in shared/ when descending from an upper directory' do
expect(renderer.chdir('nested').template(:_shared_hello, scope)).to eql('<h1>Hello</h1>')
end

Expand All @@ -37,8 +39,14 @@
expect(renderer.partial(:hello, scope)).to eql('<h1>Partial hello</h1>')
end

it 'renders partial in shared/ subdirectory' do
expect(renderer.partial(:shared_hello, scope)).to eql('<h1>Hello</h1>')
it 'does not include `shared/` subdirectory under root when looking up partials' do
expect {
renderer.partial(:shared_hello, scope)
}.to raise_error(Dry::View::Renderer::TemplateNotFoundError, /_shared_hello/)
end

it 'renders partial in shared/ subdirectory when descending from an upper directory' do
expect(renderer.chdir('hello').partial(:shared_hello, scope)).to eql('<h1>Hello</h1>')
end

it 'renders partial in upper directory' do
Expand Down

0 comments on commit 245f847

Please sign in to comment.