Skip to content

Commit

Permalink
refactor: rename method
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammad-Alavi committed Dec 7, 2024
1 parent 9c3a1e3 commit 2c3b3e5
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/Abstracts/Repositories/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ public function boot()
{
parent::boot();

if ($this->includesEagerLoadingEnabled()) {
$this->eagerLoadRequestedRelations();
if ($this->shouldEagerLoadIncludes()) {
$this->eagerLoadRequestedIncludes();
}
}

/**
* Enable or disable eager loading of relations requested by the client via "include" query parameter.
*/
public function includesEagerLoadingEnabled(): bool
public function shouldEagerLoadIncludes(): bool
{
// TODO: BC: disable it by default for v8 and enable by default for v13
return false;
Expand Down
6 changes: 5 additions & 1 deletion src/Services/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Response extends Fractal
* For example, if the include query parameter is "books,children.books", this method will return:
* ['books', 'children', 'children.books']
*/
public static function getRequestedIncludesAsModelRelation(): array
public static function getRequestedIncludes(): array
{
$requestedIncludes = request()?->input(config('fractal.auto_includes.request_key'), []);

Expand Down Expand Up @@ -59,6 +59,10 @@ public function createData(): Scope
$this->withResourceName($this->defaultResourceName());
$this->setAvailableIncludesMeta();

// TODO: enable this and remove everything below
// After the Fractalistic PR's are accepted
// return parent::createData();

if (is_null($this->transformer)) {
throw new NoTransformerSpecified();
}
Expand Down
5 changes: 3 additions & 2 deletions src/Traits/CanEagerLoadTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ trait CanEagerLoadTrait
*
* @see https://apiato.atlassian.net/browse/API-905
*/
protected function eagerLoadRequestedRelations(): void
protected function eagerLoadRequestedIncludes(): void
{
$this->scopeQuery(function (Builder|Model $model) {
if (request()?->has(config('fractal.auto_includes.request_key'))) {
$validIncludes = [];
// TODO: Do we need to do the same for the excludes?
// TODO: Or default includes! Are they eager loaded by default?
foreach (Response::getRequestedIncludesAsModelRelation() as $includeName) {
// TODO: What if the include has parameters? e.g. include=books:limit(5|3)
foreach (Response::getRequestedIncludes() as $includeName) {
$relationParts = explode('.', $includeName);
$camelCasedIncludeName = $this->filterInvalidRelations($this->model, $relationParts);
if ($camelCasedIncludeName) {
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Abstracts/Repositories/RepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function testEagerLoadSingleRelationRequestedViaRequest(
)->has(BookFactory::new()->count(3))
->createOne();
$repository = new class(app()) extends UserRepository {
public function includesEagerLoadingEnabled(): bool
public function shouldEagerLoadIncludes(): bool
{
return true;
}
Expand Down Expand Up @@ -108,7 +108,7 @@ public function testMultipleEagerLoadAppliesAllEagerLoads(): void
)->has(BookFactory::new()->count(3))
->createOne();
$repository = new class(app()) extends UserRepository {
public function includesEagerLoadingEnabled(): bool
public function shouldEagerLoadIncludes(): bool
{
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Services/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ public function testCanGetRequestedIncludes(): void
{
request()->merge(['include' => 'books,children.books']);

$result = Response::getRequestedIncludesAsModelRelation();
$result = Response::getRequestedIncludes();

$this->assertEquals(['books', 'children', 'children.books'], $result);
}
Expand Down

0 comments on commit 2c3b3e5

Please sign in to comment.