Skip to content
This repository has been archived by the owner on Feb 2, 2023. It is now read-only.

Commit

Permalink
Add listener to save commit on blob update
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mAAdhaTTah committed Jan 16, 2021
1 parent af082f3 commit dcdf285
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
9 changes: 9 additions & 0 deletions app/Database/Repository/AbstractRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 ) {
Expand Down
6 changes: 3 additions & 3 deletions app/Database/Repository/WordPressPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) );

Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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 );
Expand Down
8 changes: 7 additions & 1 deletion test/Integration/Http/Blob/CreateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [],
],
],
],
] );

Expand All @@ -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() {
Expand Down

0 comments on commit dcdf285

Please sign in to comment.