Skip to content

Commit

Permalink
include route params from within context
Browse files Browse the repository at this point in the history
  • Loading branch information
casualjim committed Feb 24, 2013
1 parent 580b15f commit d6e96c1
Showing 1 changed file with 26 additions and 2 deletions.
Expand Up @@ -8,6 +8,7 @@ import javax.servlet.http.{HttpServletResponse, HttpServletRequest, HttpSession}
import org.fusesource.scalate.TemplateEngine
import org.fusesource.scalate.servlet.ServletRenderContext
import javax.servlet.ServletContext
import servlet.ServletApiImplicits._

/**
* A render context integrated with Scalatra. Exposes a few extra
Expand Down Expand Up @@ -114,11 +115,34 @@ class ScalatraRenderContext(
route: Route,
params: Map[String, String],
splats: Iterable[String]
): String =
): String = {

route.reversibleMatcher match {
case Some(matcher: ReversibleRouteMatcher) =>
route.contextPath(request) + matcher.reverse(params, splats.toList)
withRouteMultiParams(MatchedRoute(route.action, multiParams)) {
route.contextPath(request) + matcher.reverse(params, splats.toList)
}
case _ =>
throw new Exception("Route \"%s\" is not reversible" format (route))
}
}


private[this] def withRouteMultiParams[S](matchedRoute: MatchedRoute)(thunk: => S): S = {
val originalParams = multiParams
setMultiparams(matchedRoute, originalParams)
try {
thunk
} finally {
request(MultiParamsKey) = originalParams
}
}

def setMultiparams[S](matchedRoute: MatchedRoute, originalParams: MultiParams) {
val routeParams = matchedRoute.multiParams map {
case (key, values) =>
key -> values.map(UriDecoder.secondStep(_))
}
request(MultiParamsKey) = originalParams ++ routeParams
}
}

0 comments on commit d6e96c1

Please sign in to comment.