framework
framework copied to clipboard
Add updatedAt and createdAt traits to Record
Is the idea to make a trait to mix into the record, or to mix into the field? I hope the latter --- it's allows for more flexibility.
The idea was to create traits to mix into a record, the same way the Mapper traits work. I'm not sure what traits meant to be mixed into a Field would look like. What did you have in mind?
I don't use the mapper traits. I use this:
trait CreatedTimestamp[T <: Mapper[T]] extends LifecycleCallbacks { self: MappedField[java.util.Date, T] => override def beforeSave { // this is the first save, and we have not been modified manually if ((!fieldOwner.saved_?) && (!dirty_?)) set(new java.util.Date) super.beforeSave } } trait ModifiedTimestamp[T <: Mapper[T]] extends LifecycleCallbacks { self: MappedField[java.util.Date, T] => override def beforeSave { if (fieldOwner.dirty_? && (!this.dirty_?)) set(new java.util.Date) super.beforeSave } }
object time extends MappedDateTime(this)
This way I can (a) name the fields whatever I want, (b) have as many or as few as I want, and (c) override and customize the field's behavior.
I actually posted my code before DPP wrote the trait.
On Sun, Nov 4, 2012 at 4:59 AM, Tim Nelson [email protected] wrote:
The idea was to create traits to mix into a record, the same way the Mapper traits work. I'm not sure what traits meant to be mixed into a Field would look like. What did you have in mind?
— Reply to this email directly or view it on GitHubhttps://github.com/lift/framework/issues/1347#issuecomment-10048894.
I like it. If people would prefer traits like the Mapper ones, they could just use these to make them. Or I could maybe provide both types of traits.
+1 for both if it would be sufficiently trivial to do it.
On Nov 4, 2012, at 6:33, Tim Nelson [email protected] wrote:
I like it. If people would prefer traits like the Mapper ones, they could just use these to make them. Or I could maybe provide both types of traits.
— Reply to this email directly or view it on GitHubhttps://github.com/lift/framework/issues/1347#issuecomment-10049531.
Here is our CreatedUpdated:
trait CreatedUpdated[OwnerType <: CreatedUpdated[OwnerType]] extends MongoRecord[OwnerType] {
self: OwnerType =>
var updateUpdated = true
def ignoreUpdatedDate = {
updateUpdated = false
this
}
object createdAt extends DateField(this)
object updatedAt extends DateField(this) with LifecycleCallbacks {
override def beforeSave() = if (updateUpdated) this(Helpers.now)
}
}
I don't use this var but something like that might be useful. (someone else contributed it)