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

[7.0] Fix empty update with soft delete #370

Merged
merged 2 commits into from
Apr 1, 2019
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
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 @@ -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];
}
}