Skip to content

Commit

Permalink
Flush a model's settings when deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
rawilk committed Sep 28, 2023
1 parent cecfb33 commit 97a8c6e
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/Models/HasSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Rawilk\Settings\Facades\Settings as SettingsFacade;
use Rawilk\Settings\Settings;
use Rawilk\Settings\Support\Context;
use Rawilk\Settings\Support\KeyGenerators\Md5KeyGenerator;

/**
* @mixin \Illuminate\Database\Eloquent\Model
Expand All @@ -27,11 +28,29 @@ public function settings(): Settings
return SettingsFacade::context($this->context());
}

protected static function bootHasSettings(): void
{
static::deleted(function (self $model) {
if ($model->shouldFlushSettingsOnDelete()) {
$model->settings()->flush();
}
});
}

/**
* Additional arguments that uniquely identify this model.
*/
protected function contextArguments(): array
{
return [];
}

protected function shouldFlushSettingsOnDelete(): bool
{
if (SettingsFacade::getKeyGenerator() instanceof Md5KeyGenerator) {
return false;
}

return static::$flushSettingsOnDelete ?? true;
}
}
51 changes: 51 additions & 0 deletions tests/Feature/HasSettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Rawilk\Settings\Facades\Settings;
use Rawilk\Settings\Support\Context;
use Rawilk\Settings\Support\ContextSerializers\DotNotationContextSerializer;
use Rawilk\Settings\Support\KeyGenerators\ReadableKeyGenerator;
use Rawilk\Settings\Tests\Support\Models\Company;
use Rawilk\Settings\Tests\Support\Models\CustomUser;
use Rawilk\Settings\Tests\Support\Models\User;
Expand Down Expand Up @@ -76,3 +78,52 @@
expect(Settings::context($user1Context)->get('program.name'))->toBe($user1->email)
->and(Settings::context($user2Context)->get('program.name'))->toBe($user2->email);
});

test("a model's settings are flushed when the model is deleted", function () {
$settings = settings();
(fn () => $this->keyGenerator = (new ReadableKeyGenerator)->setContextSerializer(new DotNotationContextSerializer))->call($settings);

$user1 = User::first();
$user2 = User::where('id', '>', $user1->getKey())->first();

Settings::set('user.email', 'foo');

$user1->settings()->set('user.email', $user1->email);
$user2->settings()->set('user.email', $user2->email);

$this->assertDatabaseCount('settings', 3);

$user1->delete();

$this->assertDatabaseCount('settings', 2);

$this->assertDatabaseMissing('settings', [
'key' => 'user.email:c:::model:user::id:' . $user1->getKey(),
]);
});

test('a model can be configured to not flush settings on delete', function () {
$settings = settings();
(fn () => $this->keyGenerator = (new ReadableKeyGenerator)->setContextSerializer(new DotNotationContextSerializer))->call($settings);

$company = Company::factory()->create();
$company->settings()->set('foo', 'bar');

$this->assertDatabaseCount('settings', 1);

$company->delete();

$this->assertDatabaseCount('settings', 1);
});

test("a model's settings will not be flushed if the md5 key generator is being used", function () {
$user = User::first();

$user->settings()->set('foo', 'bar');

$this->assertDatabaseCount('settings', 1);

$user->delete();

$this->assertDatabaseCount('settings', 1);
});
2 changes: 2 additions & 0 deletions tests/Support/Models/Company.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ final class Company extends Model
use HasFactory;
use HasSettings;

protected static bool $flushSettingsOnDelete = false;

protected static function newFactory(): CompanyFactory
{
return new CompanyFactory;
Expand Down

0 comments on commit 97a8c6e

Please sign in to comment.