Skip to content

Commit 06457a2

Browse files
committed
Registration of events for partial updating of an index is simplified.
1 parent c00115e commit 06457a2

File tree

4 files changed

+18
-25
lines changed

4 files changed

+18
-25
lines changed

README.md

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,25 +127,19 @@ class Dummy extends Model implements Searchable
127127
```
128128

129129
### Partial updating of search index
130-
For register models events (save/update/delete) `use Nqxcode\LuceneSearch\Model\Search` and call `registerEventsForSearchIndexUpdate` method of trait in `boot` method of model:
130+
For register of necessary events (save/update/delete) `use Nqxcode\LuceneSearch\Model\SearchTrait` in target model:
131131

132132
```php
133133

134134
use Illuminate\Database\Eloquent\Model;
135135
use Nqxcode\LuceneSearch\Model\Searchable;
136-
use Nqxcode\LuceneSearch\Model\Search as SearchTrait;
136+
use Nqxcode\LuceneSearch\Model\SearchTrait;
137137

138138
class Dummy extends Model implements Searchable
139139
{
140140
use SearchTrait;
141141

142142
// ...
143-
144-
public static function boot()
145-
{
146-
parent::boot();
147-
self::registerEventsForSearchIndexUpdate();
148-
}
149143
}
150144

151145
```

src/Nqxcode/LuceneSearch/Console/RebuildCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php namespace Nqxcode\LuceneSearch\Console;
1+
<?php namespace Nqxcode\LuceneSearch\Console;
22

33
use Illuminate\Console\Command;
44
use Nqxcode\LuceneSearch\Search;
@@ -18,7 +18,7 @@ public function fire()
1818
$this->output = new NullOutput;
1919
}
2020

21-
if (is_dir(Config::get('laravel-lucene-search::index.path'))) {
21+
if (is_dir(Config::get('laravel-lucene-search.index.path'))) {
2222
$this->call('search:clear');
2323
}
2424

src/Nqxcode/LuceneSearch/Model/SearchTrait.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,23 @@
66
* Class Search
77
* @package Nqxcode\LuceneSearch\Model
88
*/
9-
trait Search
9+
trait SearchTrait
1010
{
1111
/**
1212
* Set event handlers for updating of search index.
1313
*/
14-
public static function registerEventsForSearchIndexUpdate()
14+
public static function bootSearchTrait()
1515
{
16-
self::saved(function ($model) {
17-
App::offsetGet('search')->update($model);
18-
});
16+
self::saved(
17+
function ($model) {
18+
App::offsetGet('search')->update($model);
19+
}
20+
);
1921

20-
self::deleting(function ($model) {
21-
App::offsetGet('search')->delete($model);
22-
});
22+
self::deleting(
23+
function ($model) {
24+
App::offsetGet('search')->delete($model);
25+
}
26+
);
2327
}
2428
}

tests/models/Product.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use Illuminate\Database\Eloquent\Model;
44
use Illuminate\Database\Eloquent\Builder;
55
use Nqxcode\LuceneSearch\Model\Searchable;
6-
use Nqxcode\LuceneSearch\Model\Search;
6+
use Nqxcode\LuceneSearch\Model\SearchTrait;
77

88
/**
99
* Class Product
@@ -15,7 +15,7 @@
1515
*/
1616
class Product extends Model implements Searchable
1717
{
18-
use Search;
18+
use SearchTrait;
1919

2020
/**
2121
* @inheritdoc
@@ -25,11 +25,6 @@ public function isSearchable()
2525
return $this->publish;
2626
}
2727

28-
public static function boot()
29-
{
30-
self::registerEventsForSearchIndexUpdate();
31-
}
32-
3328
public function getOptionalAttributesAttribute()
3429
{
3530
return ['optional_attribute1' => "optional_value1"];

0 commit comments

Comments
 (0)