Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.0] Use Model collection where appropriate #334

Merged
merged 1 commit into from
Dec 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
nunomaduro marked this conversation as resolved.
Show resolved Hide resolved

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();
nunomaduro marked this conversation as resolved.
Show resolved Hide resolved
}

/**
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