Created
June 20, 2012 12:55
-
-
Save opyate/2959764 to your computer and use it in GitHub Desktop.
Gist for scala-salat issue: https://groups.google.com/forum/?hl=en_US&fromgroups#!topic/scala-salat/IAiLu7yDFT0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
(1) The findOne method stopped working | |
(2) I get a weird ClassCastException from MegaPoll to MegaPoll, even though I use a custom context and define dao as a val. | |
Polls now get persisted as: | |
*/ | |
{ | |
"_id":ObjectId("4fe18acb286f6aca172f95bb"), | |
"_typeHint":"util.mongo.MegaPoll", | |
"poll":{ | |
"_typeHint":"controllers.ResponsePoll", | |
"id":NumberLong(1), | |
"name":"Posted via API", | |
"created":NumberLong("1340181194808"), | |
"question":"Was this created in scenario 0?", | |
"option_a":"Sure was!", | |
"option_b":"I don't know.", | |
"expires":NumberLong("1338220191555"), | |
"short_url":"http://localhost:9000/alpha/#polls/1", | |
"user":{ | |
"_typeHint":"controllers.ResponseUser", | |
"id":NumberLong(2), | |
"name":"Juan M Uys", | |
"mobile":"+447702783956", | |
"created":NumberLong("1340181194512"), | |
"avatarURL":"https://secure.gravatar.com/avatar/c22bbf2aac2a3841d80f5363d73e3ebe?s=50&d=https://d37eplffuz3ep4.cloudfront.net/av67.png" | |
} | |
}, | |
"votes":[ | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package util.mongo | |
import play.api.Play | |
import com.mongodb.casbah._ | |
import play.api.Play.current | |
import com.novus.salat._ | |
import com.novus.salat.dao.ModelCompanion | |
import com.novus.salat.dao.SalatDAO | |
import com.mongodb.casbah.Imports._ | |
import se.radley.plugin.salat._ | |
import controllers.ResponsePoll | |
import controllers.ResponseUser | |
import controllers.ResponseVote | |
import models.PollResults | |
import models.Poll | |
import com.mongodb.casbah.Imports._ | |
import globals.ctx | |
object MegaPoll extends ModelCompanion[MegaPoll, ObjectId] { | |
val collection = mongoCollection("polls") | |
val dao = new SalatDAO[MegaPoll, ObjectId](collection = collection) {} | |
def findOneByPollId(pollId: Long): Option[MegaPoll] = dao.findOne(MongoDBObject("poll.id" -> pollId)) | |
/** | |
* Update the MegaPoll by Mongo Object ID. | |
* | |
* If the latter wasn't provided, we get it from the database. | |
*/ | |
def update(updated: MegaPoll, mongoObjectId: Option[String]): Unit = mongoObjectId match { | |
case Some(id) => { | |
import com.mongodb.casbah.commons.conversions.scala._ | |
RegisterJodaTimeConversionHelpers() | |
val o = MongoDBObject("$set" -> MongoDBObject("poll" -> updated)) | |
dao.update(MongoDBObject("_id" -> new ObjectId(id)), o, false, false, new WriteConcern) | |
} | |
case None => { | |
Poll.findById(updated.poll.id) match { | |
case Some(poll) if poll.mongo_object_id.isDefined => { | |
val o = MongoDBObject("$set" -> MongoDBObject("poll" -> updated)) | |
dao.update(MongoDBObject("_id" -> new ObjectId(poll.mongo_object_id.get)), o, false, false, new WriteConcern) | |
} | |
case None => play.Logger.error("The object ID should have been saved when the poll=%s was originally created." format updated.poll.id) | |
} | |
} | |
} | |
} | |
case class MegaPoll(poll: ResponsePoll, results: Option[PollResults], votes: List[ResponseVote]) | |
// for https://groups.google.com/forum/?hl=en_US&fromgroups#!topic/scala-salat/IAiLu7yDFT0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package util | |
import play.api.Play | |
import com.mongodb.casbah._ | |
import play.api.Play.current | |
import com.novus.salat._ | |
import com.novus.salat.dao._ | |
import com.mongodb.casbah.Imports._ | |
import se.radley.plugin.salat._ | |
import controllers.ResponsePoll | |
import controllers.ResponseUser | |
import controllers.ResponseVote | |
import models.PollResults | |
import models.Poll | |
import com.mongodb.casbah.Imports._ | |
object MegaPoll extends ModelCompanion[MegaPoll, ObjectId] { | |
val collection = mongoCollection("polls") | |
val dao = new SalatDAO[MegaPoll, ObjectId](collection = collection) {} | |
def findOneByPollId(pollId: Long): Option[MegaPoll] = dao.findOne(MongoDBObject("poll._id" -> pollId)) | |
/** | |
* Update the MegaPoll by Mongo Object ID. | |
* | |
* If the latter wasn't provided, we get it from the database. | |
*/ | |
def update(updated: MegaPoll, mongoObjectId: Option[String]) = mongoObjectId match { | |
case Some(id) => { | |
import com.mongodb.casbah.commons.conversions.scala._ | |
RegisterJodaTimeConversionHelpers() | |
dao.update(MongoDBObject("_id" -> new ObjectId(id)), updated, false, false, new WriteConcern) | |
} | |
case None => { | |
Poll.findById(updated.poll.id) match { | |
case Some(poll) if poll.mongo_object_id.isDefined => { | |
dao.update(MongoDBObject("_id" -> new ObjectId(poll.mongo_object_id.get)), updated, false, false, new WriteConcern) | |
} | |
case None => play.Logger.error("The object ID should have been saved when the poll=%s was originally created." format updated.poll.id) | |
} | |
} | |
} | |
} | |
case class MegaPoll(poll: ResponsePoll, results: Option[PollResults], votes: List[ResponseVote]) | |
// for https://groups.google.com/forum/?hl=en_US&fromgroups#!topic/scala-salat/IAiLu7yDFT0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment