Skip to content

Commit

Permalink
Merge pull request #79 from plank/soft-deletes
Browse files Browse the repository at this point in the history
don't purge meta when soft deleting
  • Loading branch information
frasmage authored Feb 9, 2022
2 parents a8c4ba6 + 25eb66a commit f6bc5f0
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Metable.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public static function bootMetable()
{
// delete all attached meta on deletion
static::deleted(function (self $model) {
if (method_exists($model, 'isForceDeleting') && !$model->isForceDeleting()) {
return;
}
$model->purgeMeta();
});
}
Expand Down
30 changes: 30 additions & 0 deletions tests/Integration/MetableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Database\Eloquent\Collection;
use Plank\Metable\Meta;
use Plank\Metable\Tests\Mocks\SampleMetable;
use Plank\Metable\Tests\Mocks\SampleMetableSoftDeletes;
use Plank\Metable\Tests\TestCase;
use ReflectionClass;

Expand Down Expand Up @@ -214,6 +215,30 @@ public function test_it_clears_meta_on_deletion()
$this->assertEquals(0, $meta->count());
}

public function test_it_does_not_clear_meta_on_soft_deletion()
{
$this->useDatabase();
$metable = $this->createMetableSoftDeletes();
$metable->setMeta('foo', 'bar');

$metable->delete();
$meta = Meta::all();

$this->assertEquals(1, $meta->count());
}

public function test_it_does_clear_meta_on_force_deletion()
{
$this->useDatabase();
$metable = $this->createMetableSoftDeletes();
$metable->setMeta('foo', 'bar');

$metable->forceDelete();
$meta = Meta::all();

$this->assertEquals(0, $meta->count());
}

public function test_it_can_be_queried_by_single_meta_key()
{
$this->useDatabase();
Expand Down Expand Up @@ -502,4 +527,9 @@ private function createMetable(array $attributes = []): SampleMetable
{
return factory(SampleMetable::class)->create($attributes);
}

private function createMetableSoftDeletes(array $attributes = []): SampleMetableSoftDeletes
{
return factory(SampleMetableSoftDeletes::class)->create($attributes);
}
}
15 changes: 15 additions & 0 deletions tests/Mocks/SampleMetableSoftDeletes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Plank\Metable\Tests\Mocks;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Plank\Metable\Metable;

class SampleMetableSoftDeletes extends Model
{
use Metable;
use SoftDeletes;

protected $table = 'sample_metables';
}
5 changes: 5 additions & 0 deletions tests/factories/MetableFactory.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
<?php

use Plank\Metable\Tests\Mocks\SampleMetable;
use Plank\Metable\Tests\Mocks\SampleMetableSoftDeletes;

$factory->define(SampleMetable::class, function (Faker\Generator $faker) {
return [];
});

$factory->define(SampleMetableSoftDeletes::class, function (Faker\Generator $faker) {
return [];
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function up()
{
Schema::create('sample_metables', function (Blueprint $table) {
$table->increments('id');
$table->softDeletes();
$table->timestamps();
});
}
Expand Down

0 comments on commit f6bc5f0

Please sign in to comment.