Skip to content
This repository was archived by the owner on Jul 31, 2018. It is now read-only.

Commit 98cc405

Browse files
committed
Allow custom autoIndex and autoDelete. Fixes #57
1 parent a06a0d2 commit 98cc405

File tree

6 files changed

+96
-2
lines changed

6 files changed

+96
-2
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,40 @@ for ($i = 0; $i < 10000; $i++) {
170170
Contact::reindex(); // Will use batch operations.
171171
```
172172

173+
You can also make a dynamic condition for those two parameters creating an ``autoIndex``` and/or ```autoDelete method```
174+
on your model
175+
176+
```
177+
use Illuminate\Database\Eloquent\Model;
178+
179+
class Contact extends Model
180+
{
181+
use AlgoliaEloquentTrait;
182+
183+
public function autoIndex()
184+
{
185+
if (\App::environment() === 'test') {
186+
return false;
187+
}
188+
189+
return true;
190+
}
191+
192+
public static autoDelete()
193+
{
194+
if (\App::environment() === 'test') {
195+
return false;
196+
}
197+
198+
return true;
199+
}
200+
}
201+
```
202+
203+
Be careful those two methods are defined in AlgoliaEloquentTrait.
204+
When putting those methods in a parent class they will be "erased" by AlgoliaEloquentTrait if used in a child class
205+
(because of php inheritance)
206+
173207
## Custom Index Name
174208

175209
By default, the index name will be the pluralized class name, e.g. "Contacts". You can customize the index name by using the `$indices` option:

src/AlgoliaEloquentTrait.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,4 +265,14 @@ public function removeFromIndex()
265265
$index->deleteObject($modelHelper->getObjectId($this));
266266
}
267267
}
268+
269+
public function autoIndex()
270+
{
271+
return (property_exists($this, 'autoIndex') == false || $this::$autoIndex === true);
272+
}
273+
274+
public function autoDelete()
275+
{
276+
return (property_exists($this, 'autoDelete') == false || $this::$autoDelete === true);
277+
}
268278
}

src/ModelHelper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ private function hasAlgoliaTrait(Model $class, $autoload = false)
4747

4848
public function isAutoIndex(Model $model)
4949
{
50-
return ($this->hasAlgoliaTrait($model) && (property_exists($model, 'autoIndex') == false || $model::$autoIndex === true));
50+
return ($this->hasAlgoliaTrait($model) && $model->autoIndex());
5151
}
5252

5353
public function isAutoDelete(Model $model)
5454
{
55-
return ($this->hasAlgoliaTrait($model) && (property_exists($model, 'autoDelete') == false || $model::$autoDelete === true));
55+
return ($this->hasAlgoliaTrait($model) && $model->autoDelete());
5656
}
5757

5858
public function getKey(Model $model)

tests/ModelHelperTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use AlgoliaSearch\Tests\Models\Model4;
99
use AlgoliaSearch\Tests\Models\Model5;
1010
use AlgoliaSearch\Tests\Models\Model7;
11+
use AlgoliaSearch\Tests\Models\Model8;
12+
use AlgoliaSearch\Tests\Models\Model9;
1113
use Orchestra\Testbench\TestCase;
1214

1315
class ModelHelperTest extends TestCase
@@ -34,6 +36,12 @@ public function testAutoIndexAndAutoDelete()
3436
$this->assertEquals(false, $this->modelHelper->isAutoDelete(new Model2()));
3537
$this->assertEquals(false, $this->modelHelper->isAutoDelete(new Model3()));
3638
$this->assertEquals(true, $this->modelHelper->isAutoDelete(new Model7()));
39+
40+
$this->assertEquals(true, $this->modelHelper->isAutoIndex(new Model8()));
41+
$this->assertEquals(false, $this->modelHelper->isAutoDelete(new Model8()));
42+
43+
$this->assertEquals(false, $this->modelHelper->isAutoIndex(new Model9()));
44+
$this->assertEquals(true, $this->modelHelper->isAutoDelete(new Model9()));
3745
}
3846

3947
public function testGetKey()

tests/Models/Model8.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace AlgoliaSearch\Tests\Models;
4+
5+
use AlgoliaSearch\Tests\Traits\Trait1;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class Model8 extends Model
9+
{
10+
use Trait1;
11+
12+
public function autoIndex()
13+
{
14+
return true;
15+
}
16+
17+
public function autoDelete()
18+
{
19+
return false;
20+
}
21+
}

tests/Models/Model9.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace AlgoliaSearch\Tests\Models;
4+
5+
use AlgoliaSearch\Tests\Traits\Trait1;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class Model9 extends Model
9+
{
10+
use Trait1;
11+
12+
public function autoIndex()
13+
{
14+
return false;
15+
}
16+
17+
public function autoDelete()
18+
{
19+
return true;
20+
}
21+
}

0 commit comments

Comments
 (0)