Skip to content

[10.x] searchableSync / unsearchableSync #920

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

Merged
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
62 changes: 60 additions & 2 deletions src/Searchable.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ public function registerSearchableMacros()
BaseCollection::macro('unsearchable', function () use ($self) {
$self->queueRemoveFromSearch($this);
});

BaseCollection::macro('searchableSync', function () use ($self) {
$self->syncMakeSearchable($this);
});

BaseCollection::macro('unsearchableSync', function () use ($self) {
$self->syncRemoveFromSearch($this);
});
}

/**
Expand All @@ -60,14 +68,29 @@ public function queueMakeSearchable($models)
}

if (! config('scout.queue')) {
return $models->first()->makeSearchableUsing($models)->first()->searchableUsing()->update($models);
return $this->syncMakeSearchable($models);
}

dispatch((new Scout::$makeSearchableJob($models))
->onQueue($models->first()->syncWithSearchUsingQueue())
->onConnection($models->first()->syncWithSearchUsing()));
}

/**
* Synchronously make the given models searchable.
*
* @param \Illuminate\Database\Eloquent\Collection $models
* @return void
*/
public function syncMakeSearchable($models)
{
if ($models->isEmpty()) {
return;
}

return $models->first()->makeSearchableUsing($models)->first()->searchableUsing()->update($models);
}

/**
* Dispatch the job to make the given models unsearchable.
*
Expand All @@ -81,14 +104,29 @@ public function queueRemoveFromSearch($models)
}

if (! config('scout.queue')) {
return $models->first()->searchableUsing()->delete($models);
return $this->syncRemoveFromSearch($models);
}

dispatch(new Scout::$removeFromSearchJob($models))
->onQueue($models->first()->syncWithSearchUsingQueue())
->onConnection($models->first()->syncWithSearchUsing());
}

/**
* Synchronously make the given models unsearchable.
*
* @param \Illuminate\Database\Eloquent\Collection $models
* @return void
*/
public function syncRemoveFromSearch($models)
{
if ($models->isEmpty()) {
return;
}

return $models->first()->searchableUsing()->delete($models);
}

/**
* Determine if the model should be searchable.
*
Expand Down Expand Up @@ -183,6 +221,16 @@ public function searchable()
$this->newCollection([$this])->searchable();
}

/**
* Synchronously make the given model instance searchable.
*
* @return void
*/
public function searchableSync()
{
$this->newCollection([$this])->searchableSync();
}

/**
* Remove all instances of the model from the search index.
*
Expand All @@ -205,6 +253,16 @@ public function unsearchable()
$this->newCollection([$this])->unsearchable();
}

/**
* Synchronously remove the given model instance from the search index.
*
* @return void
*/
public function unsearchableSync()
{
$this->newCollection([$this])->unsearchableSync();
}

/**
* Determine if the model existed in the search index prior to an update.
*
Expand Down
39 changes: 37 additions & 2 deletions tests/Feature/SearchableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,25 @@ class SearchableTest extends TestCase
public function test_searchable_using_update_is_called_on_collection()
{
$collection = m::mock();
$collection->shouldReceive('isEmpty')->once()->andReturn(false);
$collection->shouldReceive('isEmpty')->times(2)->andReturn(false);
$collection->shouldReceive('first->makeSearchableUsing')->with($collection)->once()->andReturn($collection);
$collection->shouldReceive('first->searchableUsing->update')->with($collection)->once();

$model = new SearchableModel;
$model->queueMakeSearchable($collection);
}

public function test_searchable_using_update_is_called_on_collection_sync()
{
$collection = m::mock();
$collection->shouldReceive('isEmpty')->once()->andReturn(false);
$collection->shouldReceive('first->makeSearchableUsing')->with($collection)->once()->andReturn($collection);
$collection->shouldReceive('first->searchableUsing->update')->with($collection)->once();

$model = new SearchableModel;
$model->syncMakeSearchable($collection);
}

public function test_searchable_using_update_is_not_called_on_empty_collection()
{
$collection = m::mock();
Expand All @@ -35,6 +46,13 @@ public function test_searchable_using_update_is_not_called_on_empty_collection()

$model = new SearchableModel;
$model->queueMakeSearchable($collection);

$collection = m::mock();
$collection->shouldReceive('isEmpty')->andReturn(true);
$collection->shouldNotReceive('first->searchableUsing->update');

$model = new SearchableModel;
$model->syncMakeSearchable($collection);
}

public function test_overridden_make_searchable_is_dispatched()
Expand All @@ -58,13 +76,23 @@ public function test_overridden_make_searchable_is_dispatched()
public function test_searchable_using_delete_is_called_on_collection()
{
$collection = m::mock();
$collection->shouldReceive('isEmpty')->once()->andReturn(false);
$collection->shouldReceive('isEmpty')->times(2)->andReturn(false);
$collection->shouldReceive('first->searchableUsing->delete')->with($collection);

$model = new SearchableModel;
$model->queueRemoveFromSearch($collection);
}

public function test_searchable_using_delete_is_called_on_collection_sycn()
{
$collection = m::mock();
$collection->shouldReceive('isEmpty')->once()->andReturn(false);
$collection->shouldReceive('first->searchableUsing->delete')->with($collection);

$model = new SearchableModel;
$model->syncRemoveFromSearch($collection);
}

public function test_searchable_using_delete_is_not_called_on_empty_collection()
{
$collection = m::mock();
Expand All @@ -73,6 +101,13 @@ public function test_searchable_using_delete_is_not_called_on_empty_collection()

$model = new SearchableModel;
$model->queueRemoveFromSearch($collection);

$collection = m::mock();
$collection->shouldReceive('isEmpty')->once()->andReturn(true);
$collection->shouldNotReceive('first->searchableUsing->delete');

$model = new SearchableModel;
$model->syncRemoveFromSearch($collection);
}

public function test_overridden_remove_from_search_is_dispatched()
Expand Down