Example #1
0
 public static function boot()
 {
     parent::boot();
     static::creating(function ($object) {
         $object->setJsonAttribute('metadata', 'author', UserProvider::getCurrentUserId());
     });
     static::created(function ($object) {
         $revision = new ObjectRevision();
         $revision->object_id = $object->id;
         $revision->revision_id = 0;
         // Data patch
         $revision->data_patch = json_encode(JsonPatch::diff([], json_decode($object->data, 1)));
         // Metadata patch
         $revision->metadata_patch = json_encode(JsonPatch::diff([], json_decode($object->metadata, 1)));
         $revision->author = UserProvider::getCurrentUserId();
         $revision->created_at = Carbon::now();
         $revision->save();
     });
     static::updating(function ($updatingObject) {
         // Get the object again from DB so we are sure we have up-to-date content to make the diff from
         $object = Object::find($updatingObject->id);
         var_dump("here updating");
         $lastRevision = ObjectRevision::where('object_id', $object->id)->orderBy('revision_id', 'DESC')->firstOrFail();
         $revision = new ObjectRevision();
         $revision->object_id = $object->id;
         $revision->revision_id = $lastRevision->revision_id + 1;
         // Data patch
         $revision->data_patch = json_encode(JsonPatch::diff(json_decode($object->data, 1), json_decode($updatingObject->data, 1)));
         // Metadata patch
         $revision->metadata_patch = json_encode(JsonPatch::diff(json_decode($object->metadata, 1), json_decode($updatingObject->metadata, 1)));
         $revision->author = UserProvider::getCurrentUserId();
         $revision->created_at = Carbon::now();
         $revision->save();
     });
 }
Example #2
0
 public function testCreateNewEmptyData()
 {
     $this->logger->info("testCreateNewEmptyData ...");
     $data = new Object();
     $data->type = 'blog-post';
     $data->save();
     $this->assertEquals(1, Object::count());
     $this->assertEquals(1, ObjectRevision::count());
     $fetched = Object::first();
     $this->assertEquals($fetched->data, '{}');
     $fetched->setJsonAttribute('metadata', 'modified', 'true');
     $fetched->save();
     $this->assertEquals(2, ObjectRevision::count());
     $data = new Object();
     $data->type = 'blog-post';
     $data->save();
     $this->assertEquals(2, Object::count());
 }