Skip to content

Commit

Permalink
Merge pull request #5 from AuroraWebSoftware/add-deleting-event
Browse files Browse the repository at this point in the history
- Added deleting event
  • Loading branch information
emreakay authored Oct 10, 2024
2 parents c18bc8f + 1578ea0 commit ea2b585
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Traits/Flexy.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,22 @@ public static function bootFlexy(): void
$flexyModelContract->flexy->setRawAttributes($flexyModelContract->flexy->getAttributes(), true);
FlexyField::dropAndCreatePivotView();
}

}

});

static::deleted(function (FlexyModelContract $flexyModelContract) {
$modelType = static::getModelType();
$modelId = $flexyModelContract->id;

Value::where([
'model_type' => $modelType,
'model_id' => $modelId,
])->delete();

FlexyField::dropAndCreatePivotView();
});
}

public static function setFlexyShape(
Expand Down
27 changes: 27 additions & 0 deletions tests/PackageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use AuroraWebSoftware\FlexyField\Enums\FlexyFieldType;
use AuroraWebSoftware\FlexyField\Exceptions\FlexyFieldIsNotInShape;
use AuroraWebSoftware\FlexyField\Models\Shape;
use AuroraWebSoftware\FlexyField\Models\Value;
use AuroraWebSoftware\FlexyField\Tests\Models\ExampleFlexyModel;
use AuroraWebSoftware\FlexyField\Tests\Models\ExampleShapelyFlexyModel;
use Illuminate\Database\Schema\Blueprint;
Expand Down Expand Up @@ -253,3 +254,29 @@
// dd(ExampleShapelyFlexyModel::getAllFlexyShapes());
expect(ExampleShapelyFlexyModel::getAllFlexyShapes())->toHaveCount(2);
});

it('can delete flexy values', function () {

$flexyModel1 = ExampleShapelyFlexyModel::create(['name' => 'ExampleFlexyModel 1']);
$flexyModel2 = ExampleShapelyFlexyModel::create(['name' => 'ExampleFlexyModel 2']);
ExampleShapelyFlexyModel::$hasShape = true;

$flexyModel1::setFlexyShape('a', FlexyFieldType::INTEGER, 3, 'numeric|max:7');
$flexyModel2::setFlexyShape('b', FlexyFieldType::INTEGER, 2, 'numeric|max:7');

$flexyModel1->flexy->a = 5;
$flexyModel1->save();
$flexyModel2->flexy->b = 1;
$flexyModel2->save();

expect(Value::where('model_type', ExampleShapelyFlexyModel::getModelType())
->where('model_id', $flexyModel1->id)->exists())->toBeTrue();

$flexyModel1->delete();

expect(Value::where('model_type', ExampleShapelyFlexyModel::getModelType())
->where('model_id', $flexyModel1->id)->exists())->toBeFalse();

expect(Value::where('model_type', ExampleShapelyFlexyModel::getModelType())
->where('model_id', $flexyModel2->id)->exists())->toBeTrue();
});

0 comments on commit ea2b585

Please sign in to comment.