Skip to content

Commit

Permalink
Merge pull request #563 from FriendsOfCake/custom-finder
Browse files Browse the repository at this point in the history
Allow all actions to use custom finder with options.
  • Loading branch information
ADmad committed Jan 6, 2018
2 parents 39b29c3 + 20ae205 commit 52b2936
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/Action/Bulk/BaseAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ protected function _constructSubject(array $ids)
{
$repository = $this->_table();

$query = $repository->find($this->findMethod(), $this->_getFindConfig($ids));
list($finder, $options) = $this->_extractFinder();
$options = array_merge($options, $this->_getFindConfig($ids));
$query = $repository->find($finder, $options);

$subject = $this->_subject();
$subject->set([
Expand Down
3 changes: 2 additions & 1 deletion src/Action/IndexAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ class IndexAction extends BaseAction
*/
protected function _handle()
{
$query = $this->_table()->find($this->findMethod());
list($finder, $options) = $this->_extractFinder();
$query = $this->_table()->find($finder, $options);
$subject = $this->_subject(['success' => true, 'query' => $query]);

$this->_trigger('beforePaginate', $subject);
Expand Down
6 changes: 5 additions & 1 deletion src/Action/LookupAction.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Crud\Action;

use Crud\Traits\FindMethodTrait;
use Crud\Traits\SerializeTrait;
use Crud\Traits\ViewTrait;
use Crud\Traits\ViewVarTrait;
Expand All @@ -14,6 +15,7 @@
class LookupAction extends BaseAction
{

use FindMethodTrait;
use SerializeTrait;
use ViewTrait;
use ViewVarTrait;
Expand All @@ -36,7 +38,9 @@ class LookupAction extends BaseAction
*/
protected function _handle()
{
$query = $this->_table()->find($this->config('findMethod'), $this->_getFindConfig());
list($finder, $options) = $this->_extractFinder();
$options = array_merge($options, $this->_getFindConfig());
$query = $this->_table()->find($finder, $options);
$subject = $this->_subject(['success' => true, 'query' => $query]);

$this->_trigger('beforeLookup', $subject);
Expand Down
25 changes: 19 additions & 6 deletions src/Traits/FindMethodTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@ public function findMethod($method = null)
return $this->config('findMethod', $method);
}

/**
* Extracts the finder name and options out of the "findMethod" option.
*
* @return array An array containing in the first position the finder name
* and in the second the options to be passed to it.
*/
protected function _extractFinder()
{
$finder = $this->findMethod();
$options = [];
if (is_array($finder)) {
$options = (array)current($finder);
$finder = key($finder);
}

return [$finder, $options];
}

/**
* Find a record from the ID
*
Expand All @@ -36,12 +54,7 @@ protected function _findRecord($id, Subject $subject)
{
$repository = $this->_table();

$finder = $this->findMethod();
$options = [];
if (is_array($finder)) {
$options = (array)current($finder);
$finder = key($finder);
}
list($finder, $options) = $this->_extractFinder();
$query = $repository->find($finder, $options);
$query->where([current($query->aliasField($repository->primaryKey())) => $id]);

Expand Down
27 changes: 27 additions & 0 deletions tests/TestCase/Action/Bulk/SetValueActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,31 @@ function ($event) {
$this->assertTrue($this->_subject->success);
$this->assertRedirect('/users');
}

/**
* Test custom finder with options
*
* @return void
*/
public function testPostWithCustomFinder()
{
$this->_eventManager->on(
'Dispatcher.invokeController',
['priority' => 1000],
function ($event) {
$this->_subscribeToEvents($this->_controller);
$this->_controller->Crud->action('deactivateAll')
->findMethod(['withCustomOptions' => ['foo' => 'bar']]);
}
);

$this->post('/blogs/deactivateAll', [
'id' => [
1,
2,
],
]);

$this->assertSame(['foo' => 'bar'], $this->_controller->Blogs->customOptions);
}
}
20 changes: 20 additions & 0 deletions tests/TestCase/Action/IndexActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,24 @@ function () {
$this->get('/blogs');
$this->assertContains('Page 1 of 1, showing 1 records out of 1 total', $this->_response->body());
}

/**
* Test using custom finder with options.
*
* @return void
*/
public function testGetWithCustomFinder()
{
$this->_eventManager->on(
'Dispatcher.invokeController',
['priority' => 1000],
function () {
$this->_controller->Crud->action('index')
->findMethod(['withCustomOptions' => ['foo' => 'bar']]);
}
);

$this->get('/blogs');
$this->assertSame(['foo' => 'bar'], $this->_controller->Blogs->customOptions);
}
}
21 changes: 21 additions & 0 deletions tests/TestCase/Action/LookupActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,25 @@ function () {
$this->assertNotNull($this->viewVariable('viewVar'));
$this->assertEquals($expected, $this->viewVariable('blogs')->toArray());
}

/**
* Test using custom finder with options.
*
* @return void
*/
public function testGetWithCustomFinder()
{
$this->_eventManager->on(
'Dispatcher.invokeController',
['priority' => 1000],
function () {
$this->_subscribeToEvents($this->_controller);
$this->_controller->Crud->action('lookup')
->findMethod(['withCustomOptions' => ['foo' => 'bar']]);
}
);

$this->get('/blogs/lookup.json');
$this->assertSame(['foo' => 'bar'], $this->_controller->Blogs->customOptions);
}
}

0 comments on commit 52b2936

Please sign in to comment.