Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
279 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module Lotus | ||
module View | ||
module Rendering | ||
PartialFile = Struct.new(:key, :format, :template) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
require 'lotus/view/template' | ||
require 'lotus/view/rendering/partial_file' | ||
|
||
module Lotus | ||
module View | ||
module Rendering | ||
# Find partial templates in the file system | ||
# | ||
# @api private | ||
# @since x.x.x | ||
# | ||
# @see View::Template | ||
class PartialTemplatesFinder | ||
# Search pattern for partial file names | ||
# | ||
# @api private | ||
# @since x.x.x | ||
PARTIAL_PATTERN = '_*'.freeze | ||
|
||
attr_reader :configuration | ||
|
||
# Initializes a new PartialTemplatesFinder | ||
# | ||
# @param configuration [Configuration] the configuration object | ||
# | ||
# @since x.x.x | ||
def initialize(configuration) | ||
@configuration = configuration | ||
end | ||
|
||
# Find partials under the given path | ||
# | ||
# @return [Array] array of PartialFinder objects | ||
# | ||
# @since x.x.x | ||
def find | ||
_find_partials(configuration.root).map do |template| | ||
path_name = Pathname(template) | ||
partial_path, partial_base_name = Pathname(template).relative_path_from(configuration.root).split | ||
partial_base_parts = partial_base_name.to_s.split('.') | ||
PartialFile.new( | ||
"#{partial_path}#{::File::SEPARATOR}#{partial_base_parts[0]}", | ||
partial_base_parts[1], | ||
View::Template.new(template, configuration.default_encoding)) | ||
end | ||
end | ||
|
||
private | ||
|
||
# Find partial template file paths | ||
# | ||
# @param path [String] the path under which we should search for partials | ||
# | ||
# @return [Array] an array of strings for each matching partial template file found | ||
# | ||
# @since x.x.x | ||
# @api private | ||
def _find_partials(path) | ||
Dir.glob("#{ [path, TemplatesFinder::RECURSIVE, PARTIAL_PATTERN].join(::File::SEPARATOR) }.#{TemplatesFinder::FORMAT}.#{TemplatesFinder::ENGINES}") | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<%= render partial: 'shared/app1_partial' %> |
1 change: 1 addition & 0 deletions
1
test/fixtures/templates/app1/templates/shared/_app1_partial.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
app 1 partial |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<%= render partial: 'shared/app2_partial' %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<%= render partial: 'shared/app1_partial' %> |
1 change: 1 addition & 0 deletions
1
test/fixtures/templates/app2/templates/shared/_app2_partial.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
app 2 partial |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{ "sidebar": [] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
require 'test_helper' | ||
|
||
describe 'Framework configuration' do | ||
it "App1 can render a view containing one of it's own partials" do | ||
rendered = App1::Views::Home::Index.render(format: :html) | ||
rendered.must_include 'app 1 partial' | ||
end | ||
|
||
it "App2 can render a view containing one of it's own partials" do | ||
rendered = App2::Views::Home::Index.render(format: :html) | ||
rendered.must_include 'app 2 partial' | ||
end | ||
|
||
it "App2 cannot render a view containing a partial from App1" do | ||
-> { | ||
App2::Views::Home::Show.render(format: :html) | ||
}.must_raise(Lotus::View::MissingTemplateError) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,33 @@ | ||
require 'test_helper' | ||
require 'reload_configuration_helper' | ||
|
||
describe Lotus::View::Rendering::PartialFinder do | ||
it 'finds the correct partial' do | ||
reload_configuration! | ||
|
||
it 'finds the correct partial in the same directory as the parent view' 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.find.render({}).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' | ||
partial_finder.find.render({}).must_match 'Organisation Partial' | ||
end | ||
|
||
it 'finds the correct partial in a different directory to the parent view' do | ||
partial_finder = Lotus::View::Rendering::PartialFinder.new(Organisations::OrderTemplates::Action, partial: 'shared/sidebar', format: 'html') | ||
partial_finder.find.render({}).must_match '<div id="sidebar"></div>' | ||
end | ||
|
||
it 'finds the correct partial with a different format' do | ||
partial_finder = Lotus::View::Rendering::PartialFinder.new(Organisations::OrderTemplates::Action, partial: 'shared/sidebar', format: 'json') | ||
partial_finder.find.render({}).must_match '{ "sidebar": [] }' | ||
end | ||
|
||
it 'finds the correct partial from the cache rather than reading from the file system' do | ||
partial_finder = Lotus::View::Rendering::PartialFinder.new(Organisations::OrderTemplates::Action, partial: 'partial', format: 'html') | ||
partial_finder.find.wont_be_nil | ||
partial_finder = Lotus::View::Rendering::PartialFinder.new(Organisations::OrderTemplates::Action, partial: 'shared/sidebar', format: 'html') | ||
partial_finder.find.wont_be_nil | ||
partial_finder = Lotus::View::Rendering::PartialFinder.new(Organisations::OrderTemplates::Action, partial: 'shared/sidebar', format: 'json') | ||
partial_finder.find.wont_be_nil | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class Minitest::Spec | ||
def self.reload_configuration! | ||
before do | ||
Lotus::View.unload! | ||
Lotus::View.class_eval do | ||
configure do | ||
root Pathname.new __dir__ + '/fixtures/templates' | ||
end | ||
end | ||
|
||
Lotus::View.load! | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.