Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need a way to customize fail behavior in menus/param menus #1487

Merged
merged 4 commits into from
Oct 2, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions web/webkit/src/main/scala/net/liftweb/sitemap/Loc.scala
Original file line number Diff line number Diff line change
Expand Up @@ -383,14 +383,12 @@ trait Loc[T] {
}

def doesMatch_?(req: Req): Boolean = {
(if (link.isDefinedAt(req)) {
link(req) match {
case Full(x) if testAllParams(allParams, req) => x
case Full(x) => false
case x => x.openOr(false)
}
} else false) && currentValue.isDefined
// the loc only matches if we've got a current value
link.isDefinedAt(req) &&
testAllParams(allParams, req) &&
(
currentValue.isDefined ||
params.contains(Loc.MatchWithoutCurrentValue)
)
}

def breadCrumbs: List[Loc[_]] = _menu.breadCrumbs ::: List(this)
Expand Down Expand Up @@ -736,6 +734,27 @@ object Loc {
*/
case object Hidden extends AnyLocParam

/**
* If this parameter is included, the Loc will continue to execute even if
* currentValue is not defined.
*
* By default, Lift will determine that a Loc does not match a given request
* if its currentValue comes up Empty, and as a result will return an HTTP 404.
* For situations where this is not the desired, "Not Found" behavior, you can
* add the MatchWithoutCurrentValue LocParam to a Loc, then use the IfValue
* LocParam to define what should happen when the currentValue is Empty.
*
* For example, given some class Thing, you could do the following to trigger
* a redirect when a Thing with a particular ID isn't found.
*
* {{{
* Menu.param[Thing]("Thing", "Thing", Thing.find(_), _.id) >>
* MatchWithoutCurrentValue >>
* IfValue(_.isDefined, () => RedirectResponse("/page/to/redirect/to"))
* }}}
*/
case object MatchWithoutCurrentValue extends AnyLocParam

/**
* If this is a submenu, use the parent Loc's params
*/
Expand Down
32 changes: 32 additions & 0 deletions web/webkit/src/test/scala/net/liftweb/sitemap/LocSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ package net.liftweb
package sitemap

import common._
import mockweb._
import MockWeb._
import mocks._

import org.specs2.mutable.Specification


Expand All @@ -40,6 +44,34 @@ object LocSpec extends Specification {
val loc = (Menu.param[Param]("Test", "Test", s => Full(Param(s)), p => p.s) / "foo" / "bar" / *).toLoc
loc.calcHref(Param("myparam")) mustEqual "/foo/bar/myparam"
}

"should not match a Req matching its Link when currentValue is Empty" in {
val testMenu = Menu.param[Param]("Test", "Test", s => Empty, p => "bacon") / "foo" / "bar" / *
val testSiteMap = SiteMap(testMenu)

val testLoc = testMenu.toLoc
val mockReq = new MockHttpServletRequest("http://test/foo/bar/123")

testS(mockReq) {
testReq(mockReq) { req =>
testLoc.doesMatch_?(req) mustEqual false
}
}
}

"should match a Req matching its Link when currentValue is Empty and MatchWithoutCurrentValue is a param" in {
val testMenu = Menu.param[Param]("Test", "Test", s => Empty, p => "bacon") / "foo" / "bar" / * >> Loc.MatchWithoutCurrentValue
val testSiteMap = SiteMap(testMenu)

val testLoc = testMenu.toLoc
val mockReq = new MockHttpServletRequest("http://test/foo/bar/123")

testS(mockReq) {
testReq(mockReq) { req =>
testLoc.doesMatch_?(req) mustEqual true
}
}
}
}
}