From 175b28621b739d99626c19658ec01f2416ffbe3b Mon Sep 17 00:00:00 2001 From: Robert Reinhard Date: Tue, 19 Dec 2017 09:13:35 -0800 Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20write=20an=20=E2=80=9Cupdated?= =?UTF-8?q?=E2=80=9D=20change=20when=20only=20published=20state=20changed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- classes/Models/Change.php | 58 +++++++++++++++++++++++-------- tests/Integration/ChangesTest.php | 19 +++++++++- 2 files changed, 61 insertions(+), 16 deletions(-) diff --git a/classes/Models/Change.php b/classes/Models/Change.php index 6fe98a83..891ddf43 100644 --- a/classes/Models/Change.php +++ b/classes/Models/Change.php @@ -95,14 +95,11 @@ public function shouldLogChange($action) */ public static function log(Model $model, $action, Admin $admin = null) { - // Get the changed attributes - $changed = $model->getDirty(); - if ($action == 'deleted' || empty($changed)) { - $changed = null; - } - // Create a new change instance - $change = static::createLog($model, $action, $admin, $changed); + if (static::shouldWriteChange($model)) { + $changed = static::getChanged($model, $action); + $change = static::createLog($model, $action, $admin, $changed); + } // Log published / unpblished changes static::logPublishingChange($model, $action, $admin); @@ -113,20 +110,51 @@ public static function log(Model $model, $action, Admin $admin = null) DB::table('changes') ->where('model', get_class($model)) ->where('key', $model->getKey()) - ->update(['deleted' => 1]) - ; + ->update(['deleted' => 1]); } // Return the changed instance - return $change; + if (isset($change)) { + return $change; + } } /** - * Create a change entry + * Don't log changes when the only thing that changed was the published + * state * * @param Model $model The model being touched - * @param string $action Generally a CRUD verb: "created", "updated", "deleted" - * @param Admin $admin The admin acting on the record + * @return boolean + */ + static private function shouldWriteChange(Model $model) + { + $only_published_changed = count($model->getDirty()) == 1 + && $model->isDirty('public'); + return !$only_published_changed; + } + + /** + * Get the changes attributes + * + * @param Model $model The model being touched + * @param string $action + * @return array|null + */ + static private function getChanged(Model $model, $action) + { + $changed = $model->getDirty(); + if ($action == 'deleted' || empty($changed)) { + $changed = null; + } + return $changed; + } + + /** + * Create a change entry + * + * @param Model $model Th + * @param string $action + * @param Admin $admin */ static protected function createLog( Model $model, @@ -147,7 +175,7 @@ static protected function createLog( /** * Get the title of the model * - * @param Model $model The model being touched + * @param Model $model * @return string */ static protected function getModelTitle(Model $model) @@ -174,7 +202,7 @@ static protected function getAdminId(Admin $admin = null) * Log changes to publishing state. The initial publish should be logged * but not an initil unpublished state. * - * @param Model $model The model being touched + * @param Model $model * @param string $action * @param Admin $admin * @return void diff --git a/tests/Integration/ChangesTest.php b/tests/Integration/ChangesTest.php index aaeb19ee..e8711009 100644 --- a/tests/Integration/ChangesTest.php +++ b/tests/Integration/ChangesTest.php @@ -143,7 +143,7 @@ public function testPublishChanges() 'model' => 'App\Article', ]); - // Make an article that isn't publisehd + // Make an article that isn't published $article = factory(Article::class)->create([ 'public' => 0, ]); @@ -168,6 +168,13 @@ public function testPublishChanges() 'action' => 'unpublished', ]); + // Confirm that only changing the published state does not create a + // change + $this->assertDatabaseMissing('changes', [ + 'model' => 'App\Article', + 'action' => 'updated', + ]); + // Unpublish it $article->public = 0; $article->save(); @@ -179,5 +186,15 @@ public function testPublishChanges() 'model' => 'App\Article', 'action' => 'unpublished', ]); + $this->assertDatabaseMissing('changes', [ + 'model' => 'App\Article', + 'action' => 'updated', + ]); + + // No update should be written + $this->assertDatabaseMissing('changes', [ + 'model' => 'App\Article', + 'action' => 'updated', + ]); } }