Skip to content
This repository has been archived by the owner on Jan 24, 2021. It is now read-only.

Commit

Permalink
Merge pull request #771 from phillip-haydon/defaultviewlocation
Browse files Browse the repository at this point in the history
re-ordered default view location and added new module path conventions
  • Loading branch information
thecodejunkie committed Oct 4, 2012
2 parents 28b7688 + c130f04 commit 9509e4d
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public void Should_define_convention_that_returns_viewname()
{
// Given
this.viewLocationConventions.Initialise(this.conventions);
var convention = this.conventions.ViewLocationConventions[5];
var convention = this.conventions.ViewLocationConventions[7];

// When
var result = convention.Invoke(
Expand All @@ -139,7 +139,7 @@ public void Should_define_convention_that_returns_viewname_in_views_folder()
{
// Given
this.viewLocationConventions.Initialise(this.conventions);
var convention = this.conventions.ViewLocationConventions[4];
var convention = this.conventions.ViewLocationConventions[6];

// When
var result = convention.Invoke(
Expand All @@ -156,7 +156,7 @@ public void Should_define_convention_that_returns_viewname_in_modulepath_subfold
{
// Given
this.viewLocationConventions.Initialise(this.conventions);
var convention = this.conventions.ViewLocationConventions[0];
var convention = this.conventions.ViewLocationConventions[2];

// When
var result = convention.Invoke(
Expand All @@ -173,7 +173,7 @@ public void Should_define_convention_that_returns_viewname_in_modulepath_subfold
{
// Given
this.viewLocationConventions.Initialise(this.conventions);
var convention = this.conventions.ViewLocationConventions[0];
var convention = this.conventions.ViewLocationConventions[2];

// When
var result = convention.Invoke(
Expand Down Expand Up @@ -224,7 +224,7 @@ public void Should_define_convention_that_returns_viewname_in_modulepath_folder(
{
// Given
this.viewLocationConventions.Initialise(this.conventions);
var convention = this.conventions.ViewLocationConventions[2];
var convention = this.conventions.ViewLocationConventions[3];

// When
var result = convention.Invoke(
Expand Down Expand Up @@ -275,7 +275,7 @@ public void Should_define_convention_that_returns_viewname_in_modulepath_folder_
{
// Given
this.viewLocationConventions.Initialise(this.conventions);
var convention = this.conventions.ViewLocationConventions[2];
var convention = this.conventions.ViewLocationConventions[3];

// When
var result = convention.Invoke(
Expand All @@ -292,7 +292,7 @@ public void Should_define_convention_that_returns_viewname_in_modulename_subfold
{
// Given
this.viewLocationConventions.Initialise(this.conventions);
var convention = this.conventions.ViewLocationConventions[1];
var convention = this.conventions.ViewLocationConventions[4];

// When
var result = convention.Invoke(
Expand All @@ -309,7 +309,7 @@ public void Should_define_convention_that_returns_viewname_in_modulename_folder(
{
// Given
this.viewLocationConventions.Initialise(this.conventions);
var convention = this.conventions.ViewLocationConventions[3];
var convention = this.conventions.ViewLocationConventions[5];

// When
var result = convention.Invoke(
Expand All @@ -320,5 +320,73 @@ public void Should_define_convention_that_returns_viewname_in_modulename_folder(
// Then
result.ShouldEqual("modulename/viewname");
}

[Fact]
public void Should_define_convention_that_returns_viewname_in_modulename_folder_in_modulepath_folder()
{
// Given
this.viewLocationConventions.Initialise(this.conventions);
var convention = this.conventions.ViewLocationConventions[1];

// When
var result = convention.Invoke(
"viewname",
null,
new ViewLocationContext { ModuleName = "modulename", ModulePath = "modulepath" });

// Then
result.ShouldEqual("modulepath/modulename/viewname");
}

[Fact]
public void Should_define_convention_that_returns_viewname_in_modulename_folder_in_modulepath_folder_in_views_folder()
{
// Given
this.viewLocationConventions.Initialise(this.conventions);
var convention = this.conventions.ViewLocationConventions[0];

// When
var result = convention.Invoke(
"viewname",
null,
new ViewLocationContext { ModuleName = "modulename", ModulePath = "modulepath" });

// Then
result.ShouldEqual("views/modulepath/modulename/viewname");
}

[Fact]
public void Should_define_convention_that_returns_viewname_in_modulename_folder_in_modulepath_folder_when_modulepath_contains_leading_slash()
{
// Given
this.viewLocationConventions.Initialise(this.conventions);
var convention = this.conventions.ViewLocationConventions[1];

// When
var result = convention.Invoke(
"viewname",
null,
new ViewLocationContext { ModuleName = "modulename", ModulePath = "/modulepath" });

// Then
result.ShouldEqual("modulepath/modulename/viewname");
}

[Fact]
public void Should_define_convention_that_returns_viewname_in_modulename_folder_in_modulepath_folder_in_views_folder_when_modulepath_contains_leading_slash()
{
// Given
this.viewLocationConventions.Initialise(this.conventions);
var convention = this.conventions.ViewLocationConventions[0];

// When
var result = convention.Invoke(
"viewname",
null,
new ViewLocationContext { ModuleName = "modulename", ModulePath = "/modulepath" });

// Then
result.ShouldEqual("views/modulepath/modulename/viewname");
}
}
}
38 changes: 34 additions & 4 deletions src/Nancy/Conventions/DefaultViewLocationConventions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,59 @@ private static void ConfigureViewLocationConventions(NancyConventions convention
{
conventions.ViewLocationConventions = new List<Func<string, object, ViewLocationContext, string>>
{
// 0 Handles: views / *modulepath* / *modulename* / *viewname*
(viewName, model, viewLocationContext) =>{
return string.IsNullOrEmpty(viewLocationContext.ModulePath) ? string.Empty : string.Concat("views/", viewLocationContext.ModulePath.TrimStart(new[] {'/'}), "/", viewName);
if (string.IsNullOrEmpty(viewLocationContext.ModulePath))
{
return string.Empty;
}
var path = viewLocationContext.ModulePath.TrimStart(new[] { '/' });
return string.Concat("views/", path, "/", viewLocationContext.ModuleName, "/", viewName);
},

(viewName, model, viewLocationContext) => {
return string.Concat("views/", viewLocationContext.ModuleName, "/", viewName);
// 1 Handles: *modulepath* / *modulename* / *viewname*
(viewName, model, viewLocationContext) =>{
if (string.IsNullOrEmpty(viewLocationContext.ModulePath))
{
return string.Empty;
}
var path = viewLocationContext.ModulePath.TrimStart(new[] { '/' });
return string.Concat(path, "/", viewLocationContext.ModuleName, "/", viewName);
},

// 2 Handles: views / *modulepath* / *viewname*
(viewName, model, viewLocationContext) =>{
return string.IsNullOrEmpty(viewLocationContext.ModulePath) ? string.Empty : string.Concat("views/", viewLocationContext.ModulePath.TrimStart(new[] {'/'}), "/", viewName);
},

// 3 Handles: *modulepath* / *viewname*
(viewName, model, viewLocationContext) =>{
return string.IsNullOrEmpty(viewLocationContext.ModulePath) ? string.Empty : string.Concat(viewLocationContext.ModulePath.TrimStart(new[] { '/' }), "/", viewName);
},

// 4 Handles: views / *modulename* / *viewname*
(viewName, model, viewLocationContext) => {
return string.Concat("views/", viewLocationContext.ModuleName, "/", viewName);
},

// 5 Handles: *modulename* / *viewname*
(viewName, model, viewLocationContext) => {
return string.Concat(viewLocationContext.ModuleName, "/", viewName);
},

// 6 Handles: views / *viewname*
(viewName, model, viewLocationContext) => {
return string.Concat("views/", viewName);
},

// 7 Handles: *viewname*
(viewName, model, viewLocationContext) => {
return viewName;
},
}
};
}
}
Expand Down

0 comments on commit 9509e4d

Please sign in to comment.