From 50ca6408fe5693c462139e17ae334762acd0dfea Mon Sep 17 00:00:00 2001 From: wimski Date: Wed, 5 May 2021 20:34:05 +0200 Subject: [PATCH] Use morph mapping for attachables --- src/Handlers/DeleteAttachments.php | 2 +- src/Models/PendingAttachment.php | 2 +- tests/Fixtures/TestServiceProvider.php | 9 ++++----- tests/FroalaUploadControllerTest.php | 3 +-- tests/TestCase.php | 2 ++ 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Handlers/DeleteAttachments.php b/src/Handlers/DeleteAttachments.php index 01cf377..bc26b3e 100644 --- a/src/Handlers/DeleteAttachments.php +++ b/src/Handlers/DeleteAttachments.php @@ -34,7 +34,7 @@ public function __construct($field) */ public function __invoke(Request $request, $model) { - Attachment::where('attachable_type', get_class($model)) + Attachment::where('attachable_type', $model->getMorphClass()) ->where('attachable_id', $model->getKey()) ->get() ->each diff --git a/src/Models/PendingAttachment.php b/src/Models/PendingAttachment.php index b7928a6..ba9f04d 100644 --- a/src/Models/PendingAttachment.php +++ b/src/Models/PendingAttachment.php @@ -46,7 +46,7 @@ public static function persistDraft($draftId, Froala $field, $model) public function persist(Froala $field, $model) { Attachment::create([ - 'attachable_type' => get_class($model), + 'attachable_type' => $model->getMorphClass(), 'attachable_id' => $model->getKey(), 'attachment' => $this->attachment, 'disk' => $field->disk, diff --git a/tests/Fixtures/TestServiceProvider.php b/tests/Fixtures/TestServiceProvider.php index 126c2ca..025c97e 100644 --- a/tests/Fixtures/TestServiceProvider.php +++ b/tests/Fixtures/TestServiceProvider.php @@ -2,16 +2,15 @@ namespace Froala\NovaFroalaField\Tests\Fixtures; +use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Support\ServiceProvider; class TestServiceProvider extends ServiceProvider { public function boot() { - } - - public function register() - { - $this->app['config']->set('nova.froala-field.attachments_driver', 'trix'); + Relation::morphMap([ + 'article' => Article::class, + ]); } } diff --git a/tests/FroalaUploadControllerTest.php b/tests/FroalaUploadControllerTest.php index f901193..9e110b1 100644 --- a/tests/FroalaUploadControllerTest.php +++ b/tests/FroalaUploadControllerTest.php @@ -5,7 +5,6 @@ use Froala\NovaFroalaField\Models\Attachment; use Froala\NovaFroalaField\Models\PendingAttachment; use function Froala\NovaFroalaField\nova_version_at_least; -use Froala\NovaFroalaField\Tests\Fixtures\Article; use Illuminate\Support\Facades\Storage; class FroalaUploadControllerTest extends TestCase @@ -58,7 +57,7 @@ public function store_attachment() 'attachment' => $this->getAttachmentLocation(), 'url' => Storage::disk(static::DISK)->url($this->getAttachmentLocation()), 'attachable_id' => $response->json('id'), - 'attachable_type' => Article::class, + 'attachable_type' => 'article', ]); } diff --git a/tests/TestCase.php b/tests/TestCase.php index 816142b..d5aaf3e 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -4,6 +4,7 @@ use Froala\NovaFroalaField\FroalaFieldServiceProvider; use Froala\NovaFroalaField\Tests\Fixtures\TestResource; +use Froala\NovaFroalaField\Tests\Fixtures\TestServiceProvider; use Froala\NovaFroalaField\Tests\Fixtures\User; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Route; @@ -49,6 +50,7 @@ protected function getPackageProviders($app) NovaServiceProvider::class, NovaApplicationServiceProvider::class, FroalaFieldServiceProvider::class, + TestServiceProvider::class, ]; }