Permalink
Browse files

Conditionally make a model instance searchable

  • Loading branch information...
kfirba committed Dec 18, 2017
1 parent 11d19af commit 245c3a32bb36a3a922abe76bf96cd7a0410ad239
Showing with 26 additions and 0 deletions.
  1. +4 −0 src/ModelObserver.php
  2. +10 −0 src/Searchable.php
  3. +12 −0 tests/ModelObserverTest.php
@@ -59,6 +59,10 @@ public function created($model)
if (static::syncingDisabledFor($model)) {
return;
}
if (! $model->shouldBeSearchable()) {
return;
}
$model->searchable();
}
@@ -196,6 +196,16 @@ public function searchableAs()
return config('scout.prefix').$this->getTable();
}
/**
* Determine if the model should be searchable.
*
* @return boolean
*/
public function shouldBeSearchable()
{
return true;
}
/**
* Get the indexable data array for the model.
*
@@ -11,6 +11,7 @@ public function test_created_handler_makes_model_searchable()
{
$observer = new ModelObserver;
$model = Mockery::mock();
$model->shouldReceive('shouldBeSearchable')->andReturn(true);
$model->shouldReceive('searchable');
$observer->created($model);
}
@@ -24,10 +25,20 @@ public function test_created_handler_doesnt_make_model_searchable_when_disabled(
$observer->created($model);
}
public function test_created_handler_doesnt_make_model_searchable_when_disabled_per_model_rule()
{
$observer = new ModelObserver;
$model = Mockery::mock();
$model->shouldReceive('shouldBeSearchable')->andReturn(false);
$model->shouldReceive('searchable')->never();
$observer->created($model);
}
public function test_updated_handler_makes_model_searchable()
{
$observer = new ModelObserver;
$model = Mockery::mock();
$model->shouldReceive('shouldBeSearchable')->andReturn(true);
$model->shouldReceive('searchable');
$observer->updated($model);
}
@@ -44,6 +55,7 @@ public function test_restored_handler_makes_model_searchable()
{
$observer = new ModelObserver;
$model = Mockery::mock();
$model->shouldReceive('shouldBeSearchable')->andReturn(true);
$model->shouldReceive('searchable');
$observer->restored($model);
}

0 comments on commit 245c3a3

Please sign in to comment.