Re: [Lift] Creating a path from a Loc
Hello, > Instead of building your Loc inline in the SiteMap declaration and > using findLoc, simply declare the loc as a val (with the appropriate > type parameter) in Boot or a similar object of your choosing. yes, that looks quite nice, although I'm just using User.sitemap for now :). But looking at MetaMegaProtoUser trait I now see that there's for example a "loginPath" val which I can simply use instead of lookuing up the loc. As well as some other useful functions :) -- Adam Warski http://www.warski.org http://www.softwaremill.eu -- You received this message because you are subscribed to the Google Groups "Lift" group. To post to this group, send email to lift...@googlegroups.com. To unsubscribe from this group, send email to liftweb+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.
Re: [Lift] Creating a path from a Loc
Hello, > If you need a String: > > (for { > loc <- SiteMap.findLoc("Login") > path <- loc.createDefaultPath > } yield path.text) openOr "/" > right, I could just convert it to text. That works, thanks :). -- Adam Warski http://www.warski.org http://www.softwaremill.eu -- You received this message because you are subscribed to the Google Groups "Lift" group. To post to this group, send email to lift...@googlegroups.com. To unsubscribe from this group, send email to liftweb+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.
Re: [Lift] Creating a path from a Loc
On Tue, Jan 26, 2010 at 1:17 PM, Adam Warski wrote: > Hello, > >> For this you can use loc.link.createPath. I use something similar in >> my codebase. This has a little cruft you may not be precisely >> interested in, but if you look at the link and flink methods, you >> should be able to get an idea of how to use the Loc.Link: > > > I'm using Link in a similar way to, but here I can't use createPath, as > SiteMap.findLoc(...) returns a Loc[_], so the type parameter is unknown, so > without some casts I won't be able to pass the parameter. > > I tried: > > val loc = SiteMap.findLoc("Login").open_! > val link = loc.link.createPath(loc.currentValue.open_!) Instead of building your Loc inline in the SiteMap declaration and using findLoc, simply declare the loc as a val (with the appropriate type parameter) in Boot or a similar object of your choosing. I have an object I call Site which I use to separate out the Loc stuff from the SiteMap and Menu. Here's what it looks like: object Site { var locs: List[Loc[_]] = Nil def loc(name: String, link: Link[Unit], text: LinkText[Unit], params: AnyLocParam*): Loc[Unit] = { val newLoc = Loc(name, link, text, params: _*) locs = newLoc :: locs newLoc } def uuloc[T <: UUEntity](params: LocParam[T]*)(implicit manifest: Manifest[T]) : UULoc[T] = { val newLoc = new UULoc[T](params.toList) locs = newLoc :: locs newLoc } // Visible locs val home = loc("Home", "index" :: Nil , ?("Home")) val userSearch =loc("User Search", "users" :: "search" :: Nil, ?("User Search")) val orderSearch = loc("Order Search", "orders" :: "search" :: Nil, ?("Order Search")) val orderForm = loc("New Order", "orders" :: "new" :: Nil, ?("New Order")) //... // REST gettable object locs val userDetail =uuloc[User](Hidden) val orderDetail = uuloc[Order](Hidden) //... } Then, in Boot.scala I simply have this: LiftRules.setSiteMap(SiteMap(Site.locs.reverse.map(Menu(_)): _*)) Don't throw away type information when you don't have to! Kris > > but unfortunately the compiler doesn't seem to recognize the fact that the > type parameter for loc is the same as the one returned for loc.currentValue. > > -- > Adam Warski > http://www.warski.org > http://www.softwaremill.eu > > > > > -- > You received this message because you are subscribed to the Google Groups > "Lift" group. > To post to this group, send email to lift...@googlegroups.com. > To unsubscribe from this group, send email to > liftweb+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/liftweb?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Lift" group. To post to this group, send email to lift...@googlegroups.com. To unsubscribe from this group, send email to liftweb+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.
Re: [Lift] Creating a path from a Loc
On Tue, Jan 26, 2010 at 12:17 PM, Adam Warski wrote: > Hello, > > > For this you can use loc.link.createPath. I use something similar in > > my codebase. This has a little cruft you may not be precisely > > interested in, but if you look at the link and flink methods, you > > should be able to get an idea of how to use the Loc.Link: > > > I'm using Link in a similar way to, but here I can't use createPath, as > SiteMap.findLoc(...) returns a Loc[_], so the type parameter is unknown, so > without some casts I won't be able to pass the parameter. > > I tried: > > val loc = SiteMap.findLoc("Login").open_! > val link = loc.link.createPath(loc.currentValue.open_!) > First, stop using open_! If you find yourself using open_!, ask the very important question, "Why am I doing this... anything with an ! in the method name means danger..." In this case, the danger is that that the Box is not Full. If you need a String: (for { loc <- SiteMap.findLoc("Login") path <- loc.createDefaultPath } yield path.text) openOr "/" > > but unfortunately the compiler doesn't seem to recognize the fact that the > type parameter for loc is the same as the one returned for loc.currentValue. > > -- > Adam Warski > http://www.warski.org > http://www.softwaremill.eu > > > > > -- > You received this message because you are subscribed to the Google Groups > "Lift" group. > To post to this group, send email to lift...@googlegroups.com. > To unsubscribe from this group, send email to > liftweb+unsubscr...@googlegroups.com > . > For more options, visit this group at > http://groups.google.com/group/liftweb?hl=en. > > -- Lift, the simply functional web framework http://liftweb.net Beginning Scala http://www.apress.com/book/view/1430219890 Follow me: http://twitter.com/dpp Surf the harmonics -- You received this message because you are subscribed to the Google Groups "Lift" group. To post to this group, send email to lift...@googlegroups.com. To unsubscribe from this group, send email to liftweb+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.
Re: [Lift] Creating a path from a Loc
Hello, > For this you can use loc.link.createPath. I use something similar in > my codebase. This has a little cruft you may not be precisely > interested in, but if you look at the link and flink methods, you > should be able to get an idea of how to use the Loc.Link: I'm using Link in a similar way to, but here I can't use createPath, as SiteMap.findLoc(...) returns a Loc[_], so the type parameter is unknown, so without some casts I won't be able to pass the parameter. I tried: val loc = SiteMap.findLoc("Login").open_! val link = loc.link.createPath(loc.currentValue.open_!) but unfortunately the compiler doesn't seem to recognize the fact that the type parameter for loc is the same as the one returned for loc.currentValue. -- Adam Warski http://www.warski.org http://www.softwaremill.eu -- You received this message because you are subscribed to the Google Groups "Lift" group. To post to this group, send email to lift...@googlegroups.com. To unsubscribe from this group, send email to liftweb+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.
Re: [Lift] Creating a path from a Loc
Hello, > Foo but I need the String representation of the link to use it in a RedirectResponse (not in a html template). Thus my proposition to have both a method to create a String and NodeSeq variants. > In Scala, if the attribute is an Option[NodeSeq] and it is None, the > attribute will not be included where if it's Some(Text("/foo/bar")) it will > be included. Thus the choice of Option rather than Box and the choice of > NodeSeq rather than String. I see, thanks for the explanation. -- Adam Warski http://www.warski.org http://www.softwaremill.eu -- You received this message because you are subscribed to the Google Groups "Lift" group. To post to this group, send email to lift...@googlegroups.com. To unsubscribe from this group, send email to liftweb+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.
Re: [Lift] Creating a path from a Loc
For this you can use loc.link.createPath. I use something similar in my codebase. This has a little cruft you may not be precisely interested in, but if you look at the link and flink methods, you should be able to get an idea of how to use the Loc.Link: class Path(val elems: List[String]) object Path { def apply(str: String) = new Path(str.split("/").toList) def apply(elems: List[String]) = new Path(elems) } case class UULink[T <: UUEntity](path: Path) extends Loc.Link[T](path.elems, false) { override def pathList(entity: T) = path.elems ::: entity.getUuid.toString :: Nil } class UULoc[T <: UUEntity](override val params: List[LocParam[T]])(implicit manifest: Manifest[T]) extends Loc[T] { private val log = Logging.logger("UULoc") private val typeName = manifest.erasure.getSimpleName private val path = Path(typeName.toLowerCase + "s" :: "show" :: Nil) override val name = typeName + " Details" override val link: UULink[T] = new UULink[T](path) override val text = LinkText((t: T) => Text(typeName + " " + t.getUuid)) override val defaultValue = Empty private def isLegalPath(root: String, uuid: String): Boolean = { //log.info("checking path root " + root + " against " + typeName.toLowerCase + " and uuid " + uuid) root == (typeName.toLowerCase + "s") && EM.findByUuid[T](uuid).isDefined } def link(entity: T, linkText: Box[NodeSeq], attrs: (String, String)*): NodeSeq = { {linkText.openOr(title(entity))} //% attrs } def flink(entity: T, linkText: Box[NodeSeq], attrs: (String, String)*)(f: () => Any): NodeSeq = { SHtml.link(link.createPath(entity), f, linkText.openOr(text.text(entity)), attrs: _*) } override def rewrite = { Full({ case RewriteRequest(ParsePath(root :: "show" :: uuid :: Nil,_,_,_),_,_) if isLegalPath(root, uuid) => (RewriteResponse(path.elems, Map(typeName.toLowerCase + "Uuid" -> uuid)), EM.findByUuid[T](uuid).get) }) } } object UULoc { def apply[T <: UUEntity](params: LocParam[T]*)(implicit manifest: Manifest[T]) : UULoc[T] = new UULoc[T](params.toList) } On Tue, Jan 26, 2010 at 3:29 AM, Adam Warski wrote: > Hello, > > I'm having some trouble generating a link basing on a Loc. > My original use-case is to redirect the user to a login page if the user is > not logged in. I found a wiki on this and it says there to simply redirect to > "/user_mgt/login", however I think it would be much nicer if I could generate > the link basing on looking up the right Loc. > > However having the Loc I can only generate a NodeSeq, not a plain String. > > So the solution would be to add a method into the Loc trait parallel to this > one: > > def createDefaultLink: Option[NodeSeq] = currentValue.flatMap(p => > link.createLink(p)).toOption > > which would be: > > def createDefaultPath: Option[String] = currentValue.flatMap(p => > link.createPath(p)).toOption > > (btw., why is it Option here, not Box?) > > Then generating a link to the login page would simply be: > > SiteMap.findLoc("Login").open_!.createDefaultPath > > -- > Adam Warski > http://www.warski.org > http://www.softwaremill.eu > > > > > -- > You received this message because you are subscribed to the Google Groups > "Lift" group. > To post to this group, send email to lift...@googlegroups.com. > To unsubscribe from this group, send email to > liftweb+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/liftweb?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Lift" group. To post to this group, send email to lift...@googlegroups.com. To unsubscribe from this group, send email to liftweb+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.
Re: [Lift] Creating a path from a Loc
Foo In Scala, if the attribute is an Option[NodeSeq] and it is None, the attribute will not be included where if it's Some(Text("/foo/bar")) it will be included. Thus the choice of Option rather than Box and the choice of NodeSeq rather than String. In terms of your code: (for { loc <- SiteMap.findLoc("Login") path <- loc.createDefaultPath } yield Login) openOr NodeSeq.Empty On Tue, Jan 26, 2010 at 2:29 AM, Adam Warski wrote: > Hello, > > I'm having some trouble generating a link basing on a Loc. > My original use-case is to redirect the user to a login page if the user > is not logged in. I found a wiki on this and it says there to simply > redirect to "/user_mgt/login", however I think it would be much nicer if I > could generate the link basing on looking up the right Loc. > > However having the Loc I can only generate a NodeSeq, not a plain String. > > So the solution would be to add a method into the Loc trait parallel to > this one: > > def createDefaultLink: Option[NodeSeq] = currentValue.flatMap(p => > link.createLink(p)).toOption > > which would be: > > def createDefaultPath: Option[String] = currentValue.flatMap(p => > link.createPath(p)).toOption > > (btw., why is it Option here, not Box?) > > Then generating a link to the login page would simply be: > > SiteMap.findLoc("Login").open_!.createDefaultPath > > -- > Adam Warski > http://www.warski.org > http://www.softwaremill.eu > > > > > -- > You received this message because you are subscribed to the Google Groups > "Lift" group. > To post to this group, send email to lift...@googlegroups.com. > To unsubscribe from this group, send email to > liftweb+unsubscr...@googlegroups.com > . > For more options, visit this group at > http://groups.google.com/group/liftweb?hl=en. > > -- Lift, the simply functional web framework http://liftweb.net Beginning Scala http://www.apress.com/book/view/1430219890 Follow me: http://twitter.com/dpp Surf the harmonics -- You received this message because you are subscribed to the Google Groups "Lift" group. To post to this group, send email to lift...@googlegroups.com. To unsubscribe from this group, send email to liftweb+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.
[Lift] Creating a path from a Loc
Hello, I'm having some trouble generating a link basing on a Loc. My original use-case is to redirect the user to a login page if the user is not logged in. I found a wiki on this and it says there to simply redirect to "/user_mgt/login", however I think it would be much nicer if I could generate the link basing on looking up the right Loc. However having the Loc I can only generate a NodeSeq, not a plain String. So the solution would be to add a method into the Loc trait parallel to this one: def createDefaultLink: Option[NodeSeq] = currentValue.flatMap(p => link.createLink(p)).toOption which would be: def createDefaultPath: Option[String] = currentValue.flatMap(p => link.createPath(p)).toOption (btw., why is it Option here, not Box?) Then generating a link to the login page would simply be: SiteMap.findLoc("Login").open_!.createDefaultPath -- Adam Warski http://www.warski.org http://www.softwaremill.eu -- You received this message because you are subscribed to the Google Groups "Lift" group. To post to this group, send email to lift...@googlegroups.com. To unsubscribe from this group, send email to liftweb+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.