From 63d19e5405bf2f4e0a63a68c032746e64b129514 Mon Sep 17 00:00:00 2001 From: Zach Leigh Date: Mon, 6 Jun 2016 21:16:05 +0900 Subject: [PATCH] Delete database records that are default. --- src/Settings/Settings.php | 57 ++++++++++++++++++++++++++++++++++++--- tests/PropertyBagTest.php | 48 +++++++++++++++++++++++++++------ 2 files changed, 93 insertions(+), 12 deletions(-) diff --git a/src/Settings/Settings.php b/src/Settings/Settings.php index 1a2039a..f217107 100644 --- a/src/Settings/Settings.php +++ b/src/Settings/Settings.php @@ -115,7 +115,7 @@ public function set(array $attributes) } /** - * Set a value in local and db settings. + * Set a value to a key in local and database settings. * * @param string $key * @param mixed $value @@ -125,14 +125,31 @@ public function set(array $attributes) protected function setKeyValue($key, $value) { if ($this->isValid($key, $value)) { - $syncType = ($this->hasSetting($key) ? 'update' : 'new'); - - $method = $syncType.'Record'; + $method = $this->getSyncType($key, $value).'Record'; $this->{$method}($key, $value); } } + /** + * Get the type of database operation to perform for the sync. + * + * @param string $key + * @param mixed $value + * + * @return string + */ + protected function getSyncType($key, $value) + { + if ($this->hasSetting($key) && $this->isDefault($key, $value)) { + return 'delete'; + } elseif ($this->hasSetting($key)) { + return 'update'; + } + + return 'new'; + } + /** * Return all resource settings as array. * @@ -167,6 +184,19 @@ public function isRegistered($key) return $this->registered->has($key); } + /** + * Value is default value for key. + * + * @param string $key + * @param mixed $value + * + * @return bool + */ + public function isDefault($key, $value) + { + return $this->getDefault($key) === $value; + } + /** * Key and value are registered values. * @@ -233,4 +263,23 @@ protected function updateRecord($key, $value) return $record; } + + /** + * Delete a UserSettings record. + * + * @param string $key + * @param mixed $value + * + * @return bool + */ + protected function deleteRecord($key, $value) + { + $model = $this->resource->getPropertyBagClass(); + + $record = $model::where($this->primaryKey, '=', $this->resource->id()) + ->where('key', '=', $key) + ->first(); + + return $record->delete(); + } } diff --git a/tests/PropertyBagTest.php b/tests/PropertyBagTest.php index 17619b0..d47b06f 100644 --- a/tests/PropertyBagTest.php +++ b/tests/PropertyBagTest.php @@ -259,15 +259,15 @@ public function a_resource_can_access_and_use_the_property_bag() $settings->set([ 'test_settings1' => 'monkey', - 'test_settings2' => false, + 'test_settings2' => true, ]); $settings->set([ 'test_settings1' => 'grapes', - 'test_settings2' => true, + 'test_settings2' => false, ]); - $this->assertContains('test_settings1', $settings->all()); + $this->assertContains('grapes', $settings->all()); $this->assertEquals($settings->get('test_settings1'), 'grapes'); @@ -277,14 +277,14 @@ public function a_resource_can_access_and_use_the_property_bag() 'value' => json_encode('["grapes"]') ]); - $this->assertContains('test_settings2', $settings->all()); + $this->assertContains(false, $settings->all()); - $this->assertEquals($settings->get('test_settings2'), true); + $this->assertEquals($settings->get('test_settings2'), false); $this->seeInDatabase('group_settings', [ 'group_id' => $group->id(), 'key' => 'test_settings2', - 'value' => json_encode('[true]') + 'value' => json_encode('[false]') ]); } @@ -357,8 +357,8 @@ public function local_settings_are_always_synced_with_database() $test3 = [ 'test_settings3' => 0, - 'test_settings1' => 'monkey', - 'test_settings2' => true + 'test_settings1' => 8, + 'test_settings2' => false ]; $settings->set($test3); @@ -367,4 +367,36 @@ public function local_settings_are_always_synced_with_database() $this->assertEquals($user->allSettingsFlat()->all(), $test3); } + + /** + * @test + */ + public function if_default_value_is_set_database_entry_is_deleted() + { + $user = $this->makeUser(); + + $this->actingAs($user); + + $settings = $user->settings($this->registered); + + $settings->set([ + 'test_settings1' => 'grapes' + ]); + + $this->seeInDatabase('user_property_bag', [ + 'user_id' => $user->id(), + 'key' => 'test_settings1', + 'value' => json_encode('["grapes"]') + ]); + + $settings->set([ + 'test_settings1' => 'monkey' + ]); + + $this->dontSeeInDatabase('user_property_bag', [ + 'user_id' => $user->id(), + 'key' => 'test_settings1', + 'value' => json_encode('["monkey"]') + ]); + } }