Skip to content

Commit bfd0aef

Browse files
committed
Ability to exclude searchable from 'searchables' => 'all'
1 parent 8cc3785 commit bfd0aef

4 files changed

Lines changed: 43 additions & 1 deletion

File tree

src/Search/ProvidesSearchables.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@ public function provide(): Collection|LazyCollection;
1818
public function contains($searchable): bool;
1919

2020
public function find(array $keys): Collection;
21+
22+
public function includedInAll(): bool;
2123
}

src/Search/Searchables.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ private function makeProviders()
3030
$providers = collect(Arr::wrap($this->index->config()['searchables'] ?? []));
3131

3232
if ($providers->contains('all')) {
33-
return $manager->providers()->map(fn ($_, $key) => $manager->make($key, $this->index, ['*']));
33+
return $manager->providers()
34+
->filter->includedInAll()
35+
->map(fn ($_, $key) => $manager->make($key, $this->index, ['*']));
3436
}
3537

3638
return $providers

src/Search/Searchables/Provider.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ public function setKeys(array $keys): self
5050
return $this;
5151
}
5252

53+
public function includedInAll(): bool
54+
{
55+
return true;
56+
}
57+
5358
protected function usesWildcard()
5459
{
5560
return in_array('*', $this->keys);

tests/Search/SearchablesTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,39 @@ public function all_searchables_include_entries_terms_assets_and_users()
9999
$this->assertEquals($everything, $items->all());
100100
}
101101

102+
#[Test]
103+
public function all_searchables_doesnt_include_searchable_where_included_in_all_is_false()
104+
{
105+
app(Providers::class)->register($entries = Mockery::mock(Entries::class)->makePartial());
106+
app(Providers::class)->register($terms = Mockery::mock(Terms::class)->makePartial());
107+
app(Providers::class)->register($assets = Mockery::mock(Assets::class)->makePartial());
108+
app(Providers::class)->register($users = Mockery::mock(Users::class)->makePartial());
109+
110+
$entries->shouldReceive('provide')->andReturn(collect([$entryA = Entry::make(), $entryB = Entry::make()]));
111+
$terms->shouldReceive('provide')->andReturn(collect([$termA = Term::make(), $termB = Term::make()]));
112+
$assets->shouldReceive('provide')->andReturn(collect([$assetA = Asset::make(), $assetB = Asset::make()]));
113+
114+
$users->shouldReceive('includedInAll')->andReturn(false);
115+
$users->shouldReceive('provide')->andReturn(collect([$userA = User::make(), $userB = User::make()]));
116+
117+
$searchables = $this->makeSearchables(['searchables' => 'all']);
118+
119+
$everythingApartFromUsers = [
120+
$entryA,
121+
$entryB,
122+
$termA,
123+
$termB,
124+
$assetA,
125+
$assetB,
126+
];
127+
128+
$items = $searchables->all();
129+
$this->assertInstanceOf(Collection::class, $items);
130+
$this->assertEquals($everythingApartFromUsers, $items->all());
131+
$this->assertFalse($searchables->contains($userA));
132+
$this->assertFalse($searchables->contains($userB));
133+
}
134+
102135
#[Test]
103136
public function it_gets_searchables_from_specific_providers()
104137
{

0 commit comments

Comments
 (0)