From af1c25ecec05b42c31258a593aa2b1a2f4eaf2a0 Mon Sep 17 00:00:00 2001 From: mAAdhaTTah Date: Sat, 16 Jan 2021 17:35:53 -0500 Subject: [PATCH] Add listener to save commit on blob update Update the usage of `$this->em->{persist,create}` to `$this->{persist,create}` to ensure the action isn't dispatched when saving relations, so we can hook into the blob save action to persist the commit. Fixes #866. --- app/Database/Repository/AbstractRepository.php | 11 +++++++++++ app/Database/Repository/WordPressPost.php | 6 +++--- app/Model/Repo.php | 1 + test/Integration/Http/Blob/CreateTest.php | 8 +++++++- 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/app/Database/Repository/AbstractRepository.php b/app/Database/Repository/AbstractRepository.php index e61b0efeb..392ad6fdc 100644 --- a/app/Database/Repository/AbstractRepository.php +++ b/app/Database/Repository/AbstractRepository.php @@ -5,6 +5,7 @@ use Intraxia\Gistpen\Contract\Repository; use Intraxia\Gistpen\Model\Blob; use Intraxia\Gistpen\Model\Language; +use Intraxia\Gistpen\Model\Commit; use Intraxia\Gistpen\Model\State; use Intraxia\Jaxion\Contract\Axolotl\EntityManager; use Intraxia\Jaxion\Axolotl\Collection; @@ -100,10 +101,20 @@ protected function fill_relations( Model $model, array $params ) { } } break; + case 'commits': + $value = $this->em->find_by( Commit::class, array_merge( $params, array( + 'repo_id' => $model->get_primary_id(), + 'post_status' => 'any', + 'order' => 'ASC', + 'orderby' => 'date', + ) ) ); + break; } if ( null !== $value ) { + $model->unguard(); $model->set_attribute( $key, $value ); + $model->reguard(); } } diff --git a/app/Database/Repository/WordPressPost.php b/app/Database/Repository/WordPressPost.php index 5850b4314..d6ba20b92 100644 --- a/app/Database/Repository/WordPressPost.php +++ b/app/Database/Repository/WordPressPost.php @@ -254,7 +254,7 @@ public function create( $class, array $data = array(), array $options = array() $blob_data['repo_id'] = $model->get_primary_id(); $blob_data['status'] = $model->get_attribute( 'status' ); - $blob = $this->em->create( Blob::class, $blob_data, array( + $blob = $this->create( Blob::class, $blob_data, array( 'unguarded' => true, ) ); @@ -338,7 +338,7 @@ public function persist( Model $model ) { $blob->status = $model->get_attribute( 'status' ); $blob->reguard(); - $this->em->persist( $blob ); + $this->persist( $blob ); } foreach ( $deleted_blobs as $deleted_blob ) { @@ -389,7 +389,7 @@ public function persist( Model $model ) { $states = new Collection( State::class ); foreach ( $model->states as $state ) { - $state = $this->em->persist( $state ); + $state = $this->persist( $state ); if ( ! is_wp_error( $state ) ) { $states = $states->add( $state ); diff --git a/app/Model/Repo.php b/app/Model/Repo.php index 22f865960..3762be4f9 100644 --- a/app/Model/Repo.php +++ b/app/Model/Repo.php @@ -68,6 +68,7 @@ class Repo extends Model implements UsesWordPressPost { 'gist_id', 'created_at', 'updated_at', + 'commits', ); /** diff --git a/test/Integration/Http/Blob/CreateTest.php b/test/Integration/Http/Blob/CreateTest.php index 1404147e8..0823981d1 100644 --- a/test/Integration/Http/Blob/CreateTest.php +++ b/test/Integration/Http/Blob/CreateTest.php @@ -83,11 +83,16 @@ public function test_saves_blob_with_filename_to_repo() { $repo = $this->app->make( 'database' ) ->find( Repo::class, $this->repo->ID, [ 'with' => [ - 'blobs' => [ + 'blobs' => [ 'with' => [ 'language' => [], ], ], + 'commits' => [ + 'with' => [ + 'states' => [], + ], + ], ], ] ); @@ -108,6 +113,7 @@ public function test_saves_blob_with_filename_to_repo() { ], ] ); $this->assertSame( $blob->filename, $this->blob->filename ); + $this->assertCount( 2, $repo->commits ); } public function test_saves_blob_with_filename_code_and_language() {