Skip to content

Commit 7862e23

Browse files
committed
Added ability to performing of operations without indexing for searchable models.
1 parent 2af106e commit 7862e23

File tree

4 files changed

+56
-11
lines changed

4 files changed

+56
-11
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php namespace Nqxcode\LuceneSearch\Model;
2+
3+
use App;
4+
5+
/**
6+
* Class SearchObserver
7+
* @package LuceneSearch\Model
8+
*/
9+
class SearchObserver
10+
{
11+
/** @var bool */
12+
private static $enabled = true;
13+
14+
/**
15+
* @param bool $enabled
16+
*/
17+
public static function setEnabled($enabled)
18+
{
19+
self::$enabled = $enabled;
20+
}
21+
22+
public function saved($model)
23+
{
24+
if (self::$enabled) {
25+
App::offsetGet('search')->update($model);
26+
}
27+
}
28+
29+
public function deleting($model)
30+
{
31+
if (self::$enabled) {
32+
App::offsetGet('search')->delete($model);
33+
}
34+
}
35+
}

src/Nqxcode/LuceneSearch/Model/SearchTrait.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,13 @@ trait SearchTrait
1313
*/
1414
public static function bootSearchTrait()
1515
{
16-
self::saved(
17-
function ($model) {
18-
App::offsetGet('search')->update($model);
19-
}
20-
);
16+
self::observe(new SearchObserver);
17+
}
2118

22-
self::deleting(
23-
function ($model) {
24-
App::offsetGet('search')->delete($model);
25-
}
26-
);
19+
public static function withoutSyncingToSearch(\Closure $closure)
20+
{
21+
SearchObserver::setEnabled(false);
22+
$closure();
23+
SearchObserver::setEnabled(true);
2724
}
2825
}

tests/functional/EventsTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,17 @@ public function testDelete()
4646

4747
$this->assertEquals(0, Search::query('observer')->count());
4848
}
49+
50+
public function testWithoutSyncingToSearch()
51+
{
52+
$this->assertEquals(0, Search::query('observer')->count());
53+
54+
Product::withoutSyncingToSearch(function () {
55+
$p = Product::first();
56+
$p->name = 'observer';
57+
$p->save();
58+
});
59+
60+
$this->assertEquals(0, Search::query('observer')->count());
61+
}
4962
}

tests/models/Product.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* @property string $name
1212
* @property string $description
1313
* @property boolean $publish
14-
* @method Builder wherePublish(boolean $publish)
14+
* @method static Builder wherePublish(boolean $publish)
1515
* @package tests\models
1616
*/
1717
class Product extends Model implements SearchableInterface

0 commit comments

Comments
 (0)