Skip to content

Commit

Permalink
refactor: searchShouldUpdate contains all the logic
Browse files Browse the repository at this point in the history
In order to not introduce a new concept to scout ("scout sensitive attributes"), we'll provide a hook so the developers can implement the sensitive attributes feature themselves, or handle it in a different way.
  • Loading branch information
juampi92 committed Jul 4, 2021
1 parent a61c695 commit c0ee0e7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 26 deletions.
25 changes: 3 additions & 22 deletions src/Searchable.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,33 +99,14 @@ public function shouldBeSearchable()
}

/**
* Get the update-sensitive attributes that, when changed, trigger an engine update.
*
* @return string[]
*/
public function scoutSensitiveAttributes()
{
return ['*'];
}

/**
* Returns true if at least one the searchSensitiveAttributes is being updated.
* When updating a model, this method determines if we
* should perform a search engine update or not.
*
* @return bool
*/
public function searchShouldUpdate(): bool
{
$sensitiveAttributes = $this->scoutSensitiveAttributes();

if ($sensitiveAttributes === ['*']) {
return true;
}

$updatedAttributes = array_keys($this->getDirty());

return collect($sensitiveAttributes)
->intersect($updatedAttributes)
->isNotEmpty();
return true;
}

/**
Expand Down
13 changes: 9 additions & 4 deletions tests/Fixtures/SearchableModelWithSensitiveAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@ class SearchableModelWithSensitiveAttributes extends Model
protected $fillable = ['first_name', 'last_name', 'remember_token', 'password'];

/**
* Get the update-sensitive attributes that, when changed, trigger an engine update.
* When updating a model, this method determines if we
* should perform a search engine update or not.
*
* @return string[]
* @return bool
*/
public function scoutSensitiveAttributes()
public function searchShouldUpdate(): bool
{
return ['first_name', 'last_name'];
$sensitiveAttributeKeys = ['first_name', 'last_name'];

return collect($this->getDirty())->keys()
->intersect($sensitiveAttributeKeys)
->isNotEmpty();
}
}

0 comments on commit c0ee0e7

Please sign in to comment.