Skip to content
This repository was archived by the owner on Sep 11, 2018. It is now read-only.

Commit d4fd542

Browse files
author
maxiloc
authored
Merge pull request #5 from GiampaoloFalqui/improvements
Adds reverse check of indexOnly to delete not-anymore-indexable documents.
2 parents 0c78cdd + f89e4f7 commit d4fd542

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/EloquentSubscriber.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@ public function saved($model)
1919

2020
/** @var \AlgoliaSearch\Index $index */
2121
foreach ($this->modelHelper->getIndices($model) as $index) {
22+
2223
if ($this->modelHelper->indexOnly($model, $index->indexName)) {
2324
$index->addObject($this->modelHelper->getAlgoliaRecord($model), $this->modelHelper->getKey($model));
25+
} else if ($this->modelHelper->wouldBeIndexed($model, $index->indexName)) {
26+
$index->deleteObject($this->modelHelper->getObjectId($model));
2427
}
2528
}
2629

@@ -46,4 +49,4 @@ public function subscribe($events)
4649
$events->listen('eloquent.saved*', '\AlgoliaSearch\Laravel\EloquentSubscriber@saved');
4750
$events->listen('eloquent.deleted*', '\AlgoliaSearch\Laravel\EloquentSubscriber@deleted');
4851
}
49-
}
52+
}

src/ModelHelper.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,19 @@ public function indexOnly(Model $model, $index_name)
4343
return !method_exists($model, 'indexOnly') || $model->indexOnly($index_name);
4444
}
4545

46+
public function wouldBeIndexed(Model $model, $index_name)
47+
{
48+
if (! method_exists($model, 'indexOnly')) {
49+
return false;
50+
}
51+
52+
$cloned = clone $model;
53+
54+
$cloned->setRawAttributes($cloned->getOriginal());
55+
56+
return $cloned->indexOnly($index_name) === true;
57+
}
58+
4659
public function getObjectId(Model $model)
4760
{
4861
return $model->{$this->getObjectIdKey($model)};
@@ -113,4 +126,4 @@ public function getAlgoliaRecord(Model $model)
113126
{
114127
return $model->getAlgoliaRecordDefault();
115128
}
116-
}
129+
}

0 commit comments

Comments
 (0)