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

Commit 366cdde

Browse files
author
maxiloc
authored
Merge pull request #70 from algolia/index-name-in-get-algolia-records
Fixes #68. Add index name in getAlgoliaRecords method
2 parents 7e3eb6d + a6a480d commit 366cdde

File tree

5 files changed

+53
-10
lines changed

5 files changed

+53
-10
lines changed

src/AlgoliaEloquentTrait.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function _reindex($safe = true, $setSettings = true)
3737

3838
foreach ($models as $model) {
3939
if ($modelHelper->indexOnly($model, $index->indexName)) {
40-
$records[] = $model->getAlgoliaRecordDefault();
40+
$records[] = $model->getAlgoliaRecordDefault($index->indexName);
4141
}
4242
}
4343

@@ -234,15 +234,15 @@ public function __call($method, $parameters)
234234
/**
235235
* Methods.
236236
*/
237-
public function getAlgoliaRecordDefault()
237+
public function getAlgoliaRecordDefault($indexName)
238238
{
239239
/** @var \AlgoliaSearch\Laravel\ModelHelper $modelHelper */
240240
$modelHelper = App::make('\AlgoliaSearch\Laravel\ModelHelper');
241241

242242
$record = null;
243243

244244
if (method_exists($this, static::$methodGetName)) {
245-
$record = $this->{static::$methodGetName}();
245+
$record = $this->{static::$methodGetName}($indexName);
246246
} else {
247247
$record = $this->toArray();
248248
}
@@ -264,7 +264,7 @@ public function pushToIndex()
264264
/** @var \AlgoliaSearch\Index $index */
265265
foreach ($indices as $index) {
266266
if ($modelHelper->indexOnly($this, $index->indexName)) {
267-
$index->addObject($this->getAlgoliaRecordDefault());
267+
$index->addObject($this->getAlgoliaRecordDefault($index->indexName));
268268
}
269269
}
270270
}

src/EloquentSubscriber.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function saved($model)
2020
/** @var \AlgoliaSearch\Index $index */
2121
foreach ($this->modelHelper->getIndices($model) as $index) {
2222
if ($this->modelHelper->indexOnly($model, $index->indexName)) {
23-
$index->addObject($this->modelHelper->getAlgoliaRecord($model), $this->modelHelper->getObjectId($model));
23+
$index->addObject($this->modelHelper->getAlgoliaRecord($model, $index->indexName), $this->modelHelper->getObjectId($model));
2424
}
2525
}
2626

src/ModelHelper.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ public function getIndicesTmp(Model $model)
131131
return $indices;
132132
}
133133

134-
public function getAlgoliaRecord(Model $model)
134+
public function getAlgoliaRecord(Model $model, $indexName)
135135
{
136-
return $model->getAlgoliaRecordDefault();
136+
return $model->getAlgoliaRecordDefault($indexName);
137137
}
138138
}

tests/AlgoliaEloquentTraitTest.php

+27-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace AlgoliaSearch\Tests;
44

5+
use AlgoliaSearch\Tests\Models\Model11;
56
use AlgoliaSearch\Tests\Models\Model2;
67
use AlgoliaSearch\Tests\Models\Model4;
78
use AlgoliaSearch\Tests\Models\Model6;
@@ -21,8 +22,8 @@ public function setUp()
2122

2223
public function testGetAlgoliaRecordDefault()
2324
{
24-
$this->assertEquals(['id2' => 1, 'objectID' => 1], (new Model2())->getAlgoliaRecordDefault());
25-
$this->assertEquals(['id2' => 1, 'objectID' => 1, 'id3' => 1, 'name' => 'test'], (new Model4())->getAlgoliaRecordDefault());
25+
$this->assertEquals(['id2' => 1, 'objectID' => 1], (new Model2())->getAlgoliaRecordDefault('test'));
26+
$this->assertEquals(['id2' => 1, 'objectID' => 1, 'id3' => 1, 'name' => 'test'], (new Model4())->getAlgoliaRecordDefault('test'));
2627
}
2728

2829
public function testPushToindex()
@@ -40,7 +41,7 @@ public function testPushToindex()
4041

4142
App::instance('\AlgoliaSearch\Laravel\ModelHelper', $modelHelper);
4243

43-
$index->shouldReceive('addObject')->times(2)->with((new Model4())->getAlgoliaRecordDefault());
44+
$index->shouldReceive('addObject')->times(2)->with((new Model4())->getAlgoliaRecordDefault('test'));
4445

4546
$this->assertEquals(null, (new Model4())->pushToIndex());
4647
}
@@ -119,6 +120,29 @@ public function testSetSynonyms()
119120
$this->assertEquals(null, $model10->setSettings());
120121
}
121122

123+
function testPustToIndexWithgetAlgoliaRecordAndIndexName() {
124+
/** @var \AlgoliaSearch\Laravel\ModelHelper $realModelHelper */
125+
$realModelHelper = App::make('\AlgoliaSearch\Laravel\ModelHelper');
126+
127+
$modelHelper = Mockery::mock('\AlgoliaSearch\Laravel\ModelHelper');
128+
129+
$realindices = $realModelHelper->getIndices(new Model11());
130+
$realindex = $realindices[0];
131+
$index = Mockery::mock('\AlgoliaSearch\Index');
132+
$index->indexName = $realindex->indexName;
133+
134+
$modelHelper->shouldReceive('getIndices')->andReturn([$index]);
135+
$modelHelper->shouldReceive('getObjectId')->andReturn($realModelHelper->getObjectId(new Model11()));
136+
$modelHelper->shouldReceive('indexOnly')->andReturn(true);
137+
138+
App::instance('\AlgoliaSearch\Laravel\ModelHelper', $modelHelper);
139+
140+
141+
$index->shouldReceive('addObject')->times(1)->with(["is" => "working", "objectID" => null]);
142+
143+
$this->assertEquals(null, (new Model11())->pushToIndex());
144+
}
145+
122146
public function tearDown()
123147
{
124148
Mockery::close();

tests/Models/Model11.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace AlgoliaSearch\Tests\Models;
4+
5+
use AlgoliaSearch\Laravel\AlgoliaEloquentTrait;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class Model11 extends Model
9+
{
10+
use AlgoliaEloquentTrait;
11+
12+
public function getAlgoliaRecord($indexName)
13+
{
14+
if ($indexName == 'model11s') {
15+
return ["is" => "working"];
16+
}
17+
return ["is not" => "working"];
18+
}
19+
}

0 commit comments

Comments
 (0)