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

Commit

Permalink
Merge pull request #70 from algolia/index-name-in-get-algolia-records
Browse files Browse the repository at this point in the history
Fixes #68. Add index name in getAlgoliaRecords method
  • Loading branch information
maxiloc committed Jun 23, 2016
2 parents 7e3eb6d + a6a480d commit 366cdde
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/AlgoliaEloquentTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function _reindex($safe = true, $setSettings = true)

foreach ($models as $model) {
if ($modelHelper->indexOnly($model, $index->indexName)) {
$records[] = $model->getAlgoliaRecordDefault();
$records[] = $model->getAlgoliaRecordDefault($index->indexName);
}
}

Expand Down Expand Up @@ -234,15 +234,15 @@ public function __call($method, $parameters)
/**
* Methods.
*/
public function getAlgoliaRecordDefault()
public function getAlgoliaRecordDefault($indexName)
{
/** @var \AlgoliaSearch\Laravel\ModelHelper $modelHelper */
$modelHelper = App::make('\AlgoliaSearch\Laravel\ModelHelper');

$record = null;

if (method_exists($this, static::$methodGetName)) {
$record = $this->{static::$methodGetName}();
$record = $this->{static::$methodGetName}($indexName);
} else {
$record = $this->toArray();
}
Expand All @@ -264,7 +264,7 @@ public function pushToIndex()
/** @var \AlgoliaSearch\Index $index */
foreach ($indices as $index) {
if ($modelHelper->indexOnly($this, $index->indexName)) {
$index->addObject($this->getAlgoliaRecordDefault());
$index->addObject($this->getAlgoliaRecordDefault($index->indexName));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/EloquentSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function saved($model)
/** @var \AlgoliaSearch\Index $index */
foreach ($this->modelHelper->getIndices($model) as $index) {
if ($this->modelHelper->indexOnly($model, $index->indexName)) {
$index->addObject($this->modelHelper->getAlgoliaRecord($model), $this->modelHelper->getObjectId($model));
$index->addObject($this->modelHelper->getAlgoliaRecord($model, $index->indexName), $this->modelHelper->getObjectId($model));
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/ModelHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ public function getIndicesTmp(Model $model)
return $indices;
}

public function getAlgoliaRecord(Model $model)
public function getAlgoliaRecord(Model $model, $indexName)
{
return $model->getAlgoliaRecordDefault();
return $model->getAlgoliaRecordDefault($indexName);
}
}
30 changes: 27 additions & 3 deletions tests/AlgoliaEloquentTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace AlgoliaSearch\Tests;

use AlgoliaSearch\Tests\Models\Model11;
use AlgoliaSearch\Tests\Models\Model2;
use AlgoliaSearch\Tests\Models\Model4;
use AlgoliaSearch\Tests\Models\Model6;
Expand All @@ -21,8 +22,8 @@ public function setUp()

public function testGetAlgoliaRecordDefault()
{
$this->assertEquals(['id2' => 1, 'objectID' => 1], (new Model2())->getAlgoliaRecordDefault());
$this->assertEquals(['id2' => 1, 'objectID' => 1, 'id3' => 1, 'name' => 'test'], (new Model4())->getAlgoliaRecordDefault());
$this->assertEquals(['id2' => 1, 'objectID' => 1], (new Model2())->getAlgoliaRecordDefault('test'));
$this->assertEquals(['id2' => 1, 'objectID' => 1, 'id3' => 1, 'name' => 'test'], (new Model4())->getAlgoliaRecordDefault('test'));
}

public function testPushToindex()
Expand All @@ -40,7 +41,7 @@ public function testPushToindex()

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

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

$this->assertEquals(null, (new Model4())->pushToIndex());
}
Expand Down Expand Up @@ -119,6 +120,29 @@ public function testSetSynonyms()
$this->assertEquals(null, $model10->setSettings());
}

function testPustToIndexWithgetAlgoliaRecordAndIndexName() {
/** @var \AlgoliaSearch\Laravel\ModelHelper $realModelHelper */
$realModelHelper = App::make('\AlgoliaSearch\Laravel\ModelHelper');

$modelHelper = Mockery::mock('\AlgoliaSearch\Laravel\ModelHelper');

$realindices = $realModelHelper->getIndices(new Model11());
$realindex = $realindices[0];
$index = Mockery::mock('\AlgoliaSearch\Index');
$index->indexName = $realindex->indexName;

$modelHelper->shouldReceive('getIndices')->andReturn([$index]);
$modelHelper->shouldReceive('getObjectId')->andReturn($realModelHelper->getObjectId(new Model11()));
$modelHelper->shouldReceive('indexOnly')->andReturn(true);

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


$index->shouldReceive('addObject')->times(1)->with(["is" => "working", "objectID" => null]);

$this->assertEquals(null, (new Model11())->pushToIndex());
}

public function tearDown()
{
Mockery::close();
Expand Down
19 changes: 19 additions & 0 deletions tests/Models/Model11.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace AlgoliaSearch\Tests\Models;

use AlgoliaSearch\Laravel\AlgoliaEloquentTrait;
use Illuminate\Database\Eloquent\Model;

class Model11 extends Model
{
use AlgoliaEloquentTrait;

public function getAlgoliaRecord($indexName)
{
if ($indexName == 'model11s') {
return ["is" => "working"];
}
return ["is not" => "working"];
}
}

0 comments on commit 366cdde

Please sign in to comment.