diff --git a/src/EngineManager.php b/src/EngineManager.php index fd96a601..bcc6a3b2 100644 --- a/src/EngineManager.php +++ b/src/EngineManager.php @@ -33,9 +33,10 @@ public function createAlgoliaDriver() UserAgent::addCustomUserAgent('Laravel Scout', '7.0.0'); - return new AlgoliaEngine(Algolia::create( - config('scout.algolia.id'), config('scout.algolia.secret') - )); + return new AlgoliaEngine( + Algolia::create(config('scout.algolia.id'), config('scout.algolia.secret')), + config('scout.soft_delete') + ); } /** diff --git a/src/Engines/AlgoliaEngine.php b/src/Engines/AlgoliaEngine.php index b607c36c..263701e3 100644 --- a/src/Engines/AlgoliaEngine.php +++ b/src/Engines/AlgoliaEngine.php @@ -15,15 +15,24 @@ class AlgoliaEngine extends Engine */ protected $algolia; + /** + * Determines if soft deletes for Scout are enabled or not. + * + * @var bool + */ + protected $softDelete; + /** * Create a new engine instance. * * @param \Algolia\AlgoliaSearch\SearchClient $algolia + * @param bool $softDelete * @return void */ - public function __construct(Algolia $algolia) + public function __construct(Algolia $algolia, $softDelete = false) { $this->algolia = $algolia; + $this->softDelete = $softDelete; } /** @@ -41,20 +50,20 @@ public function update($models) $index = $this->algolia->initIndex($models->first()->searchableAs()); - if ($this->usesSoftDelete($models->first()) && config('scout.soft_delete', false)) { + if ($this->usesSoftDelete($models->first()) && $this->softDelete) { $models->each->pushSoftDeleteMetadata(); } $objects = $models->map(function ($model) { - $array = array_merge( - $model->toSearchableArray(), $model->scoutMetadata() - ); - - if (empty($array)) { + if (empty($searchableData = $model->toSearchableArray())) { return; } - return array_merge(['objectID' => $model->getScoutKey()], $array); + return array_merge( + ['objectID' => $model->getScoutKey()], + $searchableData, + $model->scoutMetadata() + ); })->filter()->values()->all(); if (! empty($objects)) { diff --git a/tests/AlgoliaEngineTest.php b/tests/AlgoliaEngineTest.php index 930f034d..11f2bdfe 100644 --- a/tests/AlgoliaEngineTest.php +++ b/tests/AlgoliaEngineTest.php @@ -117,4 +117,32 @@ public function test_update_empty_searchable_array_does_not_add_objects_to_index $engine = new AlgoliaEngine($client); $engine->update(Collection::make([new EmptyTestModel])); } + + public function test_update_empty_searchable_array_from_soft_deleted_model_does_not_add_objects_to_index() + { + $client = m::mock('Algolia\AlgoliaSearch\SearchClient'); + $client->shouldReceive('initIndex')->with('table')->andReturn($index = m::mock('StdClass')); + $index->shouldNotReceive('saveObjects'); + + $engine = new AlgoliaEngine($client, true); + $engine->update(Collection::make([new SoftDeletedEmptySearchableModel])); + } +} + +class SoftDeletedEmptySearchableModel extends EmptyTestModel +{ + public function toSearchableArray() + { + return []; + } + + public function pushSoftDeleteMetadata() + { + // + } + + public function scoutMetadata() + { + return ['__soft_deleted' => 1]; + } }