How to use traits with params for query methods?

31 views
Skip to first unread message

mr.f...@gmail.com

unread,
May 27, 2015, 12:12:29 PM5/27/15
to activate-p...@googlegroups.com
Hello there!

Thank you for the framework, it is very convinient.

I have few tables which share the same logic - they all have field "url" and i want have the same method for them "findByUrl", defined in the trait.
Here is the code (i use it with play framework):


package models

import models.doerPersistenceContext._

trait HasUrl {

var url: String
}


trait AllWithUrls[T <: Entity with HasUrl] {

def findByUrl(inputUrl: String): Option[T] = transactional {

//
val entities = query {
(entity: T) => where(entity.url :== inputUrl) select entity orderBy entity.url limit 1
}

//
entities.headOption
}
}


I have compilation error: No implicit view available from => T => net.fwbrasil.activate.statement.StatementSelectValue.
       (entity: T) => where(entity.url :== inputUrl) select entity orderBy entity.url limit 1
                                                     ^

Can u please explain what i am doing wrong and is it possible to implement what i want.
Thank you.

fac...@tiendanube.com

unread,
Aug 14, 2015, 9:28:50 PM8/14/15
to Activate Persistence Framework, mr.f...@gmail.com
I had implemented something similar for slugs, that after some time I make it work:

import models.dancePortPersistenceContext._

trait Slug[T <: Entity] {
  def toSlugify: String

  var slug: String = ""

  def calculateSlug[S <: Entity : Manifest](): String = {
    val tentativeSlug = slugify(toSlugify)
    val regex = "^" + tentativeSlug + "(-[0-9]*)?$";

    if (!slug.contains(tentativeSlug)) {
     //Here the query
      val result = query {
        (entity: S with Slug[S]) =>
          where(entity.slug regexp regex) select (entity.slug) orderBy (entity.slug desc) limit (1)
      }

      if (result.isEmpty) {
        slug = tentativeSlug
      } else {
        val parts = result.head.split("-")
        val lastPart = parts(parts.length - 1)
        var lastSlugNumber = 1
        if (lastPart.matches("\\d+"))
          lastSlugNumber = lastPart.toInt + 1

        slug = tentativeSlug + "-" + lastSlugNumber
      }
    }

    slug
  }

  private def slugify(input: String): String = {
    import java.text.Normalizer
    Normalizer.normalize(input, Normalizer.Form.NFD)
        .replaceAll("[^\\w\\s-]", "") // Remove all non-word, non-space or non-dash characters
        .replace('-', ' ')            // Replace dashes with spaces
        .trim                         // Trim leading/trailing whitespace (including what used to be leading/trailing dashes)
        .replaceAll("\\s+", "-")      // Replace whitespace (including newlines and repetitions) with single dashes
        .toLowerCase                  // Lowercase the final results
  }
}


Then in an entity:

class OneEntity(var name: String = "") extends Entity with Slug[SchoolProfile] {


  def onNameChange = on(_.name).change {
    calculateSlug[SchoolProfile]()
  }

  override def toSlugify: String = name
}
Reply all
Reply to author
Forward
0 new messages