From dcdf285435dd10203d0f24df46a15d743b62a63c 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 | 9 +++++++++ app/Database/Repository/WordPressPost.php | 6 +++--- test/Integration/Http/Blob/CreateTest.php | 8 +++++++- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/app/Database/Repository/AbstractRepository.php b/app/Database/Repository/AbstractRepository.php index e61b0efeb..7a149e110 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,6 +101,14 @@ 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 ) { 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/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() {