ensure teams have a slug

This commit is contained in:
Thibault Duplessis 2012-12-18 01:06:38 +01:00
parent f2be5e79c2
commit 808ef19c5d
3 changed files with 6 additions and 2 deletions

View file

@ -3,6 +3,7 @@ package team
import user.User import user.User
import ornicar.scalalib.Random
import org.joda.time.DateTime import org.joda.time.DateTime
import com.novus.salat.annotations.Key import com.novus.salat.annotations.Key
import java.text.Normalizer import java.text.Normalizer
@ -44,5 +45,7 @@ object Team {
createdAt = DateTime.now, createdAt = DateTime.now,
createdBy = createdBy.id) createdBy = createdBy.id)
def nameToId(name: String) = templating.StringHelper slugify name def nameToId(name: String) = (templating.StringHelper slugify name) |> { slug
slug.isEmpty.fold(Random nextString 8, slug)
}
} }

View file

@ -12,7 +12,7 @@ object StringHelper extends StringHelper
trait StringHelper { trait StringHelper {
def slugify(input: String) = { def slugify(input: String) = {
val nowhitespace = input.replace(" ", "-") val nowhitespace = input.trim.replace(" ", "-")
val normalized = Normalizer.normalize(nowhitespace, Normalizer.Form.NFD) val normalized = Normalizer.normalize(nowhitespace, Normalizer.Form.NFD)
val slug = """[^\w-]""".r.replaceAllIn(normalized, "") val slug = """[^\w-]""".r.replaceAllIn(normalized, "")
slug.toLowerCase slug.toLowerCase

1
todo
View file

@ -49,3 +49,4 @@ show all join requests
show join requests in teams list show join requests in teams list
kick someone out of a team kick someone out of a team
team search team search
team forum recent posts is broken