Skip to content

Commit

Permalink
Merge pull request #12 from carsdotcom/issue-11
Browse files Browse the repository at this point in the history
fix: JsonModels in CollectionOfJsonModel should not be ->isDirty after ->fresh
  • Loading branch information
czedonis authored Nov 21, 2024
2 parents 9f4bbc7 + b230d34 commit 78d9eb6
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
5 changes: 4 additions & 1 deletion app/CollectionOfJsonModels.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ public function setPrimaryKey(string $key = null): self
*/
public function fresh(): self
{
return $this->fill($this->getLinkedData() ?: []);
return $this
->fill($this->getLinkedData() ?: [])
// We loaded them from linked data, they are not dirty
->each(fn ($model) => $model->syncOriginal());
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/CollectionOfJsonModelsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ public function testElementsThatExistWhenCollectionIsCreatedExist()
->fresh();
self::assertCount(1, $collection);
self::assertTrue($collection[0]->exists);
self::assertFalse($collection[0]->isDirty());
}

public function testSaveCollectionSkipsCreatingListenerForExistingChildren()
Expand Down
2 changes: 2 additions & 0 deletions tests/Unit/Traits/HasJsonModelAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function testWholeAttributeExists()
};
self::assertInstanceOf(ConcreteJsonModel::class, $model->jsonModel);
self::assertSame(1, $model->jsonModel->a);
self::assertFalse($model->jsonModel->isDirty(), "Loaded attributes are not dirty");
}

public function testWholeAttributeMissingIsEmptyObject()
Expand Down Expand Up @@ -406,6 +407,7 @@ public function testJsonModelCollectionHydratesChildrenToClass()
self::assertCount(1, $model->jsons);
self::assertInstanceOf(ConcreteJsonModel::class, $model->jsons->first());
self::assertSame(1, $model->jsons->first()->id);
self::assertFalse($model->jsons->first()->isDirty(), "Loaded models in a collection-attribute are not dirty");
}

public function testAttributeCanBeTestedWithIsset()
Expand Down

0 comments on commit 78d9eb6

Please sign in to comment.