framework icon indicating copy to clipboard operation
framework copied to clipboard

Add updatedAt and createdAt traits to Record

Open eltimn opened this issue 12 years ago • 6 comments

eltimn avatar Nov 03 '12 12:11 eltimn

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.

nafg avatar Nov 04 '12 07:11 nafg

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?

eltimn avatar Nov 04 '12 09:11 eltimn

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.

nafg avatar Nov 04 '12 10:11 nafg

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.

eltimn avatar Nov 04 '12 11:11 eltimn

+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.

Shadowfiend avatar Nov 04 '12 14:11 Shadowfiend

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)

lkuczera avatar Nov 04 '12 16:11 lkuczera