Skip to content

Commit

Permalink
Delete database records that are default.
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleigh committed Jun 6, 2016
1 parent c185a5d commit 63d19e5
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 12 deletions.
57 changes: 53 additions & 4 deletions src/Settings/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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();
}
}
48 changes: 40 additions & 8 deletions tests/PropertyBagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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]')
]);
}

Expand Down Expand Up @@ -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);
Expand All @@ -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"]')
]);
}
}

0 comments on commit 63d19e5

Please sign in to comment.