Skip to content

Commit

Permalink
Merge pull request #370 from driesvints/fix-empty-update-with-soft-de…
Browse files Browse the repository at this point in the history
…lete

[7.0] Fix empty update with soft delete
  • Loading branch information
taylorotwell authored Apr 1, 2019
2 parents a1c4e9f + a76269e commit 7c9e07f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 11 deletions.
7 changes: 4 additions & 3 deletions src/EngineManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
);
}

/**
Expand Down
25 changes: 17 additions & 8 deletions src/Engines/AlgoliaEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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)) {
Expand Down
28 changes: 28 additions & 0 deletions tests/AlgoliaEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,32 @@ public function toSearchableArray()
{
return [];
}

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];
}
}

0 comments on commit 7c9e07f

Please sign in to comment.