Skip to content

Commit

Permalink
Use Model collection where appropriate
Browse files Browse the repository at this point in the history
This way when the model's collection is overwritten it still gets applied.

Fixes #188
  • Loading branch information
driesvints committed Dec 7, 2018
1 parent a05a238 commit e94938d
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 23 deletions.
3 changes: 1 addition & 2 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Illuminate\Pagination\Paginator;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Pagination\LengthAwarePaginator;

class Builder
Expand Down Expand Up @@ -267,7 +266,7 @@ public function paginate($perPage = null, $pageName = 'page', $page = null)

$perPage = $perPage ?: $this->model->getPerPage();

$results = Collection::make($engine->map(
$results = $this->model->newCollection($engine->map(
$this, $rawResults = $engine->paginate($this, $perPage, $page), $this->model
));

Expand Down
19 changes: 7 additions & 12 deletions src/Engines/AlgoliaEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Laravel\Scout\Builder;
use AlgoliaSearch\Client as Algolia;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\SoftDeletes;

class AlgoliaEngine extends Engine
Expand Down Expand Up @@ -171,20 +170,16 @@ public function mapIds($results)
public function map(Builder $builder, $results, $model)
{
if (count($results['hits']) === 0) {
return Collection::make();
return $model->newCollection();
}

$models = $model->getScoutModelsByIds(
$builder, collect($results['hits'])->pluck('objectID')->values()->all()
)->keyBy(function ($model) {
return $model->getScoutKey();
});
$objectIds = collect($results['hits'])->pluck('objectID')->values()->all();

return Collection::make($results['hits'])->map(function ($hit) use ($models) {
if (isset($models[$hit['objectID']])) {
return $models[$hit['objectID']];
}
})->filter()->values();
return $model->getScoutModelsByIds(
$builder, $objectIds
)->filter(function ($model) use ($objectIds) {
return in_array($model->getScoutKey(), $objectIds);
});
}

/**
Expand Down
5 changes: 2 additions & 3 deletions src/Engines/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Laravel\Scout\Engines;

use Laravel\Scout\Builder;
use Illuminate\Database\Eloquent\Collection;

abstract class Engine
{
Expand Down Expand Up @@ -94,8 +93,8 @@ public function keys(Builder $builder)
*/
public function get(Builder $builder)
{
return Collection::make($this->map(
return $this->map(
$builder, $this->search($builder), $builder->model
));
);
}
}
3 changes: 2 additions & 1 deletion src/Engines/NullEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Laravel\Scout\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Collection as BaseCollection;

class NullEngine extends Engine
{
Expand Down Expand Up @@ -61,7 +62,7 @@ public function paginate(Builder $builder, $perPage, $page)
*/
public function mapIds($results)
{
return Collection::make();
return BaseCollection::make();
}

/**
Expand Down
5 changes: 2 additions & 3 deletions src/Searchable.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Laravel\Scout;

use Laravel\Scout\Jobs\MakeSearchable;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Collection as BaseCollection;

Expand Down Expand Up @@ -137,7 +136,7 @@ public static function makeAllSearchable()
*/
public function searchable()
{
Collection::make([$this])->searchable();
$this->newCollection([$this])->searchable();
}

/**
Expand All @@ -159,7 +158,7 @@ public static function removeAllFromSearch()
*/
public function unsearchable()
{
Collection::make([$this])->unsearchable();
$this->newCollection([$this])->unsearchable();
}

/**
Expand Down
3 changes: 2 additions & 1 deletion tests/AlgoliaEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ public function test_map_correctly_maps_results_to_models()
$model = m::mock('StdClass');
$model->shouldReceive('newQuery')->andReturn($model);
$model->shouldReceive('getKeyName')->andReturn('id');
$model->shouldReceive('getScoutModelsByIds')->andReturn(Collection::make([new AlgoliaEngineTestModel]));
$model->shouldReceive('getScoutModelsByIds')->andReturn($models = Collection::make([new AlgoliaEngineTestModel]));
$model->shouldReceive('newCollection')->andReturn($models);

$builder = m::mock(Builder::class);

Expand Down
4 changes: 3 additions & 1 deletion tests/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ public function test_pagination_correctly_handles_paginated_results()
$model->shouldReceive('searchableUsing')->andReturn($engine = m::mock());

$engine->shouldReceive('paginate');
$engine->shouldReceive('map')->andReturn(Collection::make([new stdClass]));
$engine->shouldReceive('map')->andReturn($results = Collection::make([new stdClass]));
$engine->shouldReceive('getTotalCount');

$model->shouldReceive('newCollection')->andReturn($results);

$builder->paginate();
}

Expand Down

0 comments on commit e94938d

Please sign in to comment.