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/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/lib/lotus/view/rendering/partial_finder.rb b/lib/lotus/view/rendering/partial_finder.rb index 1d0a4caf..b6dc4abd 100644 --- a/lib/lotus/view/rendering/partial_finder.rb +++ b/lib/lotus/view/rendering/partial_finder.rb @@ -22,7 +22,39 @@ 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 + # + # @since 0.4.3 + # @api private + def find + if path = partial_template_under_view_path + View::Template.new path + else + super + end + end + protected + # @since 0.4.3 + # @api private + def partial_template_under_view_path + _find(view_template_dir).first + end + + # @since 0.4.3 + # @api private + def view_template_dir + *all, _ = @view.template.split(separator) + all.join(separator) + end + def template_name *all, last = partial_name.split(separator) all.push( last.prepend(prefix) ).join(separator) diff --git a/lib/lotus/view/rendering/templates_finder.rb b/lib/lotus/view/rendering/templates_finder.rb index e8eb9172..9c85d2bf 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 0.4.3 + 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 0.4.3 + def search_path + recursive + end + # @api private # @since 0.2.0 def recursive 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/fixtures.rb b/test/fixtures.rb index 45cf3a36..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 @@ -63,6 +63,21 @@ class NestedView root __dir__ + '/fixtures/templates' end + +module Organisations + class Action + include Lotus::View + root __dir__ + '/fixtures/templates' + end + + module OrderTemplates + class Action + include Lotus::View + root __dir__ + '/fixtures/templates' + end + end +end + class MissingTemplateView include Lotus::View end @@ -96,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 @@ -279,7 +294,7 @@ class Index class JsonIndex < Index format :json - layout nil + layout false end class RssIndex < Index 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..6d2a7eda --- /dev/null +++ b/test/fixtures/templates/organisations/action.html.erb @@ -0,0 +1,2 @@ +<%= render(partial: 'partial') %> +<%= render(partial: 'shared/sidebar') %> 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..6d2a7eda --- /dev/null +++ b/test/fixtures/templates/organisations/order_templates/action.html.erb @@ -0,0 +1,2 @@ +<%= render(partial: 'partial') %> +<%= render(partial: 'shared/sidebar') %> diff --git a/test/partial_finder_test.rb b/test/partial_finder_test.rb new file mode 100644 index 00000000..6130a995 --- /dev/null +++ b/test/partial_finder_test.rb @@ -0,0 +1,11 @@ +require 'test_helper' + +describe Lotus::View::Rendering::PartialFinder do + it 'finds the correct partial' do + 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 07767ac7..d6eeb762 100644 --- a/test/rendering_test.rb +++ b/test/rendering_test.rb @@ -80,6 +80,16 @@ rendered.must_match %(

Nested

) end + 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 map = Map.new(['Rome', 'Cambridge']) 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