From b19c3ae71c352be1fdac0eaf601386aad70a7014 Mon Sep 17 00:00:00 2001 From: Farrel Lifson Date: Mon, 22 Jun 2015 18:38:42 +0200 Subject: [PATCH 1/7] 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/7] 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) From 04aedc4856218e4cd2cd63a494b3b432a2b29d67 Mon Sep 17 00:00:00 2001 From: Farrel Lifson Date: Fri, 3 Jul 2015 23:28:43 +0200 Subject: [PATCH 3/7] More test cases for parent resource --- test/fixtures.rb | 5 +++++ test/partial_finder_test.rb | 5 ++++- test/rendering_test.rb | 5 ++++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/test/fixtures.rb b/test/fixtures.rb index 0e790683..4c085793 100644 --- a/test/fixtures.rb +++ b/test/fixtures.rb @@ -65,6 +65,11 @@ class NestedView module Organisations + class Action + include Lotus::View + root __dir__ + '/fixtures/templates' + end + module OrderTemplates class Action include Lotus::View diff --git a/test/partial_finder_test.rb b/test/partial_finder_test.rb index 270e62df..92fda4f0 100644 --- a/test/partial_finder_test.rb +++ b/test/partial_finder_test.rb @@ -6,8 +6,11 @@ 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 = Lotus::View::Rendering::PartialFinder.new(Organisations::OrderTemplates::Action, partial: 'partial', format: 'html') partial_finder.find.render(format: 'html').must_match 'Order Template Partial' + + partial_finder = Lotus::View::Rendering::PartialFinder.new(Organisations::Action, partial: 'partial', format: 'html') + partial_finder.find.render(format: 'html').must_match 'Organisation Partial' end end diff --git a/test/rendering_test.rb b/test/rendering_test.rb index 5862af53..f636e146 100644 --- a/test/rendering_test.rb +++ b/test/rendering_test.rb @@ -80,9 +80,12 @@ rendered.must_match %(

Nested

) end - it 'finds and renders partials in nested directories when the class name of the view contains template' do + it 'finds and renders partials in the directory of the view template parent directory' do rendered = Organisations::OrderTemplates::Action.render(format: :html) rendered.must_match %(Order Template Partial) + + rendered = Organisations::Action.render(format: :html) + rendered.must_match %(Organisation Partial) end it 'decorates locals' do From fde9483635939c2250c6b671233abfd9d325ea74 Mon Sep 17 00:00:00 2001 From: Farrel Lifson Date: Sat, 4 Jul 2015 00:06:02 +0200 Subject: [PATCH 4/7] Refactoring out superfluous method --- lib/lotus/view/rendering/partial_finder.rb | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/lotus/view/rendering/partial_finder.rb b/lib/lotus/view/rendering/partial_finder.rb index f18eb205..c871b480 100644 --- a/lib/lotus/view/rendering/partial_finder.rb +++ b/lib/lotus/view/rendering/partial_finder.rb @@ -31,7 +31,7 @@ class PartialFinder < TemplateFinder # # @see Lotus::View::Rendering::TemplateFinder#find def find - if partial_template_exists_under_view? + if partial_template_under_view_path View::Template.new partial_template_under_view_path else super @@ -39,12 +39,8 @@ def find 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 + Dir.glob("#{[root,view_template_dir, template_name].join(separator)}.#{format}.#{engines}").first end def view_template_dir From 43dc966bd7d5b470c8e9affda0060c731ad563f7 Mon Sep 17 00:00:00 2001 From: Luca Guidi Date: Tue, 7 Jul 2015 10:30:02 +0200 Subject: [PATCH 5/7] Cleanup per #73 --- lib/lotus/view/rendering/partial_finder.rb | 17 ++++++++++++----- lib/lotus/view/rendering/templates_finder.rb | 17 +++++++++++++++-- .../templates/organisations/action.html.erb | 1 + .../order_templates/action.html.erb | 1 + test/partial_finder_test.rb | 5 ----- test/rendering_test.rb | 2 ++ 6 files changed, 31 insertions(+), 12 deletions(-) diff --git a/lib/lotus/view/rendering/partial_finder.rb b/lib/lotus/view/rendering/partial_finder.rb index c871b480..6a5887d5 100644 --- a/lib/lotus/view/rendering/partial_finder.rb +++ b/lib/lotus/view/rendering/partial_finder.rb @@ -30,21 +30,28 @@ class PartialFinder < TemplateFinder # @return [Lotus::View::Template] the requested template # # @see Lotus::View::Rendering::TemplateFinder#find - def find - if partial_template_under_view_path - View::Template.new partial_template_under_view_path + # + # @since x.x.x + # @api private + def find + if path = partial_template_under_view_path + View::Template.new path else super end end protected + # @since x.x.x + # @api private def partial_template_under_view_path - Dir.glob("#{[root,view_template_dir, template_name].join(separator)}.#{format}.#{engines}").first + _find(view_template_dir).first end + # @since x.x.x + # @api private def view_template_dir - *all, last = @view.template.split(separator) + *all, _ = @view.template.split(separator) all.join(separator) end diff --git a/lib/lotus/view/rendering/templates_finder.rb b/lib/lotus/view/rendering/templates_finder.rb index e8eb9172..6d977522 100644 --- a/lib/lotus/view/rendering/templates_finder.rb +++ b/lib/lotus/view/rendering/templates_finder.rb @@ -69,12 +69,19 @@ def initialize(view) # Lotus::View::Rendering::TemplatesFinder.new(Articles::Show).find # # => [#] def find - Dir.glob( "#{ [root, recursive, template_name].join(separator) }.#{ format }.#{ engines }" ).map do |template| - View::Template.new template + _find.map do |template| + View::Template.new(template) end end protected + + # @api private + # @since x.x.x + def _find(lookup = search_path) + Dir.glob( "#{ [root, lookup, template_name].join(separator) }.#{ format }.#{ engines }" ) + end + # @api private # @since 0.1.0 def template_name @@ -87,6 +94,12 @@ def root @view.root end + # @api private + # @since x.x.x + def search_path + recursive + end + # @api private # @since 0.2.0 def recursive diff --git a/test/fixtures/templates/organisations/action.html.erb b/test/fixtures/templates/organisations/action.html.erb index 5efcf793..6d2a7eda 100644 --- a/test/fixtures/templates/organisations/action.html.erb +++ b/test/fixtures/templates/organisations/action.html.erb @@ -1 +1,2 @@ <%= render(partial: 'partial') %> +<%= render(partial: 'shared/sidebar') %> diff --git a/test/fixtures/templates/organisations/order_templates/action.html.erb b/test/fixtures/templates/organisations/order_templates/action.html.erb index 5efcf793..6d2a7eda 100644 --- a/test/fixtures/templates/organisations/order_templates/action.html.erb +++ b/test/fixtures/templates/organisations/order_templates/action.html.erb @@ -1 +1,2 @@ <%= render(partial: 'partial') %> +<%= render(partial: 'shared/sidebar') %> diff --git a/test/partial_finder_test.rb b/test/partial_finder_test.rb index 92fda4f0..6130a995 100644 --- a/test/partial_finder_test.rb +++ b/test/partial_finder_test.rb @@ -2,11 +2,6 @@ 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' diff --git a/test/rendering_test.rb b/test/rendering_test.rb index f636e146..d6eeb762 100644 --- a/test/rendering_test.rb +++ b/test/rendering_test.rb @@ -83,9 +83,11 @@ it 'finds and renders partials in the directory of the view template parent directory' do rendered = Organisations::OrderTemplates::Action.render(format: :html) rendered.must_match %(Order Template Partial) + rendered.must_match %() rendered = Organisations::Action.render(format: :html) rendered.must_match %(Organisation Partial) + rendered.must_match %() end it 'decorates locals' do From d874ee64853798e8f4b95a177ca72b2d9fc021a1 Mon Sep 17 00:00:00 2001 From: Alfonso Uceda Date: Wed, 8 Jul 2015 16:55:00 +0200 Subject: [PATCH 6/7] Fixed readme for `layout nil` option, now is `layout false` --- README.md | 2 +- lib/lotus/view/rendering/null_layout.rb | 2 +- test/fixtures.rb | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index fb998a40..3958785c 100644 --- a/README.md +++ b/README.md @@ -460,7 +460,7 @@ module Articles class RssIndex < Index format :rss - layout nil + layout false end end diff --git a/lib/lotus/view/rendering/null_layout.rb b/lib/lotus/view/rendering/null_layout.rb index d8472d8b..2f854496 100644 --- a/lib/lotus/view/rendering/null_layout.rb +++ b/lib/lotus/view/rendering/null_layout.rb @@ -13,7 +13,7 @@ module Rendering # module Articles # class Show # include Lotus::View - # layout nil + # layout false # end # end # diff --git a/test/fixtures.rb b/test/fixtures.rb index 4c085793..c4a331ce 100644 --- a/test/fixtures.rb +++ b/test/fixtures.rb @@ -51,7 +51,7 @@ class AppView end class AppViewLayout < AppView - layout nil + layout false end class AppViewRoot < AppView @@ -111,12 +111,12 @@ def title class RssIndex < Index format :rss - layout nil + layout false end class AtomIndex < RssIndex format :atom - layout nil + layout false end class New @@ -294,7 +294,7 @@ class Index class JsonIndex < Index format :json - layout nil + layout false end class RssIndex < Index From 6e564754bf1631afe6159c3b1c2b01ca22abf9b3 Mon Sep 17 00:00:00 2001 From: Luca Guidi Date: Fri, 10 Jul 2015 14:37:18 +0200 Subject: [PATCH 7/7] Prepare for v0.4.3 --- CHANGELOG.md | 4 ++++ lib/lotus/view/rendering/partial_finder.rb | 6 +++--- lib/lotus/view/rendering/templates_finder.rb | 4 ++-- lib/lotus/view/version.rb | 2 +- lotus-view.gemspec | 4 ++-- test/version_test.rb | 2 +- 6 files changed, 13 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 712501b1..a947f7c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ # Lotus::View View layer for Lotus +## v0.4.3 - 2015-07-10 +### Fixed +- [Farrel Lifson] Force partial finder to be explicit when to templates have the same name. + ## v0.4.2 - 2015-06-23 ### Fixed - [Tom Kadwill] Ensure views to use methods defined by the associated layout. diff --git a/lib/lotus/view/rendering/partial_finder.rb b/lib/lotus/view/rendering/partial_finder.rb index 6a5887d5..b6dc4abd 100644 --- a/lib/lotus/view/rendering/partial_finder.rb +++ b/lib/lotus/view/rendering/partial_finder.rb @@ -31,7 +31,7 @@ class PartialFinder < TemplateFinder # # @see Lotus::View::Rendering::TemplateFinder#find # - # @since x.x.x + # @since 0.4.3 # @api private def find if path = partial_template_under_view_path @@ -42,13 +42,13 @@ def find end protected - # @since x.x.x + # @since 0.4.3 # @api private def partial_template_under_view_path _find(view_template_dir).first end - # @since x.x.x + # @since 0.4.3 # @api private def view_template_dir *all, _ = @view.template.split(separator) diff --git a/lib/lotus/view/rendering/templates_finder.rb b/lib/lotus/view/rendering/templates_finder.rb index 6d977522..9c85d2bf 100644 --- a/lib/lotus/view/rendering/templates_finder.rb +++ b/lib/lotus/view/rendering/templates_finder.rb @@ -77,7 +77,7 @@ def find protected # @api private - # @since x.x.x + # @since 0.4.3 def _find(lookup = search_path) Dir.glob( "#{ [root, lookup, template_name].join(separator) }.#{ format }.#{ engines }" ) end @@ -95,7 +95,7 @@ def root end # @api private - # @since x.x.x + # @since 0.4.3 def search_path recursive end diff --git a/lib/lotus/view/version.rb b/lib/lotus/view/version.rb index 501e13f3..e0c0a4df 100644 --- a/lib/lotus/view/version.rb +++ b/lib/lotus/view/version.rb @@ -3,6 +3,6 @@ module View # Defines the version # # @since 0.1.0 - VERSION = '0.4.2'.freeze + VERSION = '0.4.3'.freeze end end diff --git a/lotus-view.gemspec b/lotus-view.gemspec index e8ab282d..4541a633 100644 --- a/lotus-view.gemspec +++ b/lotus-view.gemspec @@ -6,8 +6,8 @@ require 'lotus/view/version' Gem::Specification.new do |spec| spec.name = 'lotus-view' spec.version = Lotus::View::VERSION - spec.authors = ['Luca Guidi'] - spec.email = ['me@lucaguidi.com'] + spec.authors = ['Luca Guidi', 'Trung LĂȘ', 'Alfonso Uceda Pompa'] + spec.email = ['me@lucaguidi.com', 'trung.le@ruby-journal.com', 'uceda73@gmail.com'] spec.description = %q{View layer for Lotus} spec.summary = %q{View layer for Lotus, with a separation between views and templates} spec.homepage = 'http://lotusrb.org' diff --git a/test/version_test.rb b/test/version_test.rb index 3f01a0e3..f6f7d95d 100644 --- a/test/version_test.rb +++ b/test/version_test.rb @@ -2,6 +2,6 @@ describe Lotus::View::VERSION do it 'returns current version' do - Lotus::View::VERSION.must_equal '0.4.2' + Lotus::View::VERSION.must_equal '0.4.3' end end