From b19c3ae71c352be1fdac0eaf601386aad70a7014 Mon Sep 17 00:00:00 2001 From: Farrel Lifson Date: Mon, 22 Jun 2015 18:38:42 +0200 Subject: [PATCH 1/2] Failing tests illustrating issue #72 --- test/fixtures.rb | 10 ++++++++++ .../templates/organisations/_partial.html.erb | 1 + .../templates/organisations/action.html.erb | 1 + .../organisations/order_templates/_partial.html.erb | 1 + .../organisations/order_templates/action.html.erb | 1 + test/partial_finder_test.rb | 13 +++++++++++++ test/rendering_test.rb | 5 +++++ 7 files changed, 32 insertions(+) create mode 100644 test/fixtures/templates/organisations/_partial.html.erb create mode 100644 test/fixtures/templates/organisations/action.html.erb create mode 100644 test/fixtures/templates/organisations/order_templates/_partial.html.erb create mode 100644 test/fixtures/templates/organisations/order_templates/action.html.erb create mode 100644 test/partial_finder_test.rb diff --git a/test/fixtures.rb b/test/fixtures.rb index 45cf3a36..0e790683 100644 --- a/test/fixtures.rb +++ b/test/fixtures.rb @@ -63,6 +63,16 @@ class NestedView root __dir__ + '/fixtures/templates' end + +module Organisations + module OrderTemplates + class Action + include Lotus::View + root __dir__ + '/fixtures/templates' + end + end +end + class MissingTemplateView include Lotus::View end diff --git a/test/fixtures/templates/organisations/_partial.html.erb b/test/fixtures/templates/organisations/_partial.html.erb new file mode 100644 index 00000000..7bb54398 --- /dev/null +++ b/test/fixtures/templates/organisations/_partial.html.erb @@ -0,0 +1 @@ +Organisation Partial diff --git a/test/fixtures/templates/organisations/action.html.erb b/test/fixtures/templates/organisations/action.html.erb new file mode 100644 index 00000000..5efcf793 --- /dev/null +++ b/test/fixtures/templates/organisations/action.html.erb @@ -0,0 +1 @@ +<%= render(partial: 'partial') %> diff --git a/test/fixtures/templates/organisations/order_templates/_partial.html.erb b/test/fixtures/templates/organisations/order_templates/_partial.html.erb new file mode 100644 index 00000000..25dd0ee4 --- /dev/null +++ b/test/fixtures/templates/organisations/order_templates/_partial.html.erb @@ -0,0 +1 @@ +Order Template Partial diff --git a/test/fixtures/templates/organisations/order_templates/action.html.erb b/test/fixtures/templates/organisations/order_templates/action.html.erb new file mode 100644 index 00000000..5efcf793 --- /dev/null +++ b/test/fixtures/templates/organisations/order_templates/action.html.erb @@ -0,0 +1 @@ +<%= render(partial: 'partial') %> diff --git a/test/partial_finder_test.rb b/test/partial_finder_test.rb new file mode 100644 index 00000000..270e62df --- /dev/null +++ b/test/partial_finder_test.rb @@ -0,0 +1,13 @@ +require 'test_helper' + +describe Lotus::View::Rendering::PartialFinder do + it 'finds the correct partial' do + path = Organisations::OrderTemplates::Action.root.join('organisations/order_templates/action.html.erb') + template = Lotus::View::Template.new(path) + + inner_resource_action = Organisations::OrderTemplates::Action.new(template, {}) + partial_finder = Lotus::View::Rendering::PartialFinder.new(Organisations::OrderTemplates::Action, partial: 'partial', format: 'html') + + partial_finder.find.render(format: 'html').must_match 'Order Template Partial' + end +end diff --git a/test/rendering_test.rb b/test/rendering_test.rb index 07767ac7..5862af53 100644 --- a/test/rendering_test.rb +++ b/test/rendering_test.rb @@ -80,6 +80,11 @@ rendered.must_match %(

Nested

) end + it 'finds and renders partials in nested directories when the class name of the view contains template' do + rendered = Organisations::OrderTemplates::Action.render(format: :html) + rendered.must_match %(Order Template Partial) + end + it 'decorates locals' do map = Map.new(['Rome', 'Cambridge']) From 086a4d3970e79ed8f506b238de0c97af3476647e Mon Sep 17 00:00:00 2001 From: Farrel Lifson Date: Mon, 29 Jun 2015 00:06:52 +0200 Subject: [PATCH 2/2] Modify PartialFinder to search for partial template under directory of view template, if not found search recursively from root. --- lib/lotus/view/rendering/partial_finder.rb | 29 ++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/lib/lotus/view/rendering/partial_finder.rb b/lib/lotus/view/rendering/partial_finder.rb index 1d0a4caf..f18eb205 100644 --- a/lib/lotus/view/rendering/partial_finder.rb +++ b/lib/lotus/view/rendering/partial_finder.rb @@ -22,7 +22,36 @@ class PartialFinder < TemplateFinder # "_sidebar.html.erb" PREFIX = '_'.freeze + # Find a template for a partial. Initially it will look for the + # partial template under the directory of the parent directory + # view template, if not found it will search recursivly from + # the view root. + # + # @return [Lotus::View::Template] the requested template + # + # @see Lotus::View::Rendering::TemplateFinder#find + def find + if partial_template_exists_under_view? + View::Template.new partial_template_under_view_path + else + super + end + end + protected + def partial_template_exists_under_view? + File.exists?(partial_template_under_view_path) + end + + def partial_template_under_view_path + Dir.glob("#{[root,view_template_dir, template_name].join(separator)}.#{format}.#{engines}").first.to_s + end + + def view_template_dir + *all, last = @view.template.split(separator) + all.join(separator) + end + def template_name *all, last = partial_name.split(separator) all.push( last.prepend(prefix) ).join(separator)