Skip to content

Commit

Permalink
Rename 'prefix' => 'scope'.
Browse files Browse the repository at this point in the history
I am concerned that 'prefix' will be easily confused with routing
prefixes, as both this prefix and routing prefixes affect URL
generation. scope is a slightly less confusing term to me.
  • Loading branch information
markstory committed Jul 5, 2016
1 parent f5164aa commit 2a4995e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 26 deletions.
10 changes: 5 additions & 5 deletions src/Controller/Component/PaginatorComponent.php
Expand Up @@ -159,7 +159,7 @@ public function paginate($object, array $settings = [])
$options = $this->validateSort($object, $options);
$options = $this->checkLimit($options);

$options += ['page' => 1, 'prefix' => null];
$options += ['page' => 1, 'scope' => null];
$options['page'] = (int)$options['page'] < 1 ? 1 : (int)$options['page'];
list($finder, $options) = $this->_extractFinder($options);

Expand Down Expand Up @@ -204,7 +204,7 @@ public function paginate($object, array $settings = [])
'limit' => $defaults['limit'] != $limit ? $limit : null,
'sortDefault' => $sortDefault,
'directionDefault' => $directionDefault,
'prefix' => $options['prefix'],
'scope' => $options['scope'],
];

if (!isset($request['paging'])) {
Expand Down Expand Up @@ -259,10 +259,10 @@ public function mergeOptions($alias, $settings)
{
$defaults = $this->getDefaults($alias, $settings);
$request = $this->_registry->getController()->request;
$prefix = Hash::get($settings, 'prefix', null);
$scope = Hash::get($settings, 'scope', null);
$query = $request->query;
if ($prefix) {
$query = Hash::get($request->query, $prefix, []);
if ($scope) {
$query = Hash::get($request->query, $scope, []);
}
$request = array_intersect_key($query, array_flip($this->_config['whitelist']));
return array_merge($defaults, $request);
Expand Down
10 changes: 5 additions & 5 deletions src/View/Helper/PaginatorHelper.php
Expand Up @@ -478,7 +478,7 @@ public function generateUrl(array $options = [], $model = null, $full = false)
];

if (!empty($this->_config['options']['url'])) {
$key = implode('.', array_filter(['options.url', Hash::get($paging, 'prefix', null)]));
$key = implode('.', array_filter(['options.url', Hash::get($paging, 'scope', null)]));
$url = array_merge($url, Hash::get($this->_config, $key, []));
}

Expand All @@ -496,10 +496,10 @@ public function generateUrl(array $options = [], $model = null, $full = false)
) {
$url['sort'] = $url['direction'] = null;
}
if (!empty($paging['prefix'])) {
$url = [$paging['prefix'] => $url] + $this->_config['options']['url'];
if (empty($url[$paging['prefix']]['page'])) {
unset($url[$paging['prefix']]['page']);
if (!empty($paging['scope'])) {
$url = [$paging['scope'] => $url] + $this->_config['options']['url'];
if (empty($url[$paging['scope']]['page'])) {
unset($url[$paging['scope']]['page']);
}
}
return $this->Url->build($url, $full);
Expand Down
28 changes: 14 additions & 14 deletions tests/TestCase/Controller/Component/PaginatorComponentTest.php
Expand Up @@ -144,7 +144,7 @@ public function testPaginateExtraParams()
'order' => ['PaginatorPosts.id' => 'ASC'],
'page' => 1,
'whitelist' => ['limit', 'sort', 'page', 'direction'],
'prefix' => null,
'scope' => null,
]);
$this->Paginator->paginate($table, $settings);
}
Expand Down Expand Up @@ -228,7 +228,7 @@ public function testDefaultPaginateParams()
'page' => 1,
'order' => ['PaginatorPosts.id' => 'DESC'],
'whitelist' => ['limit', 'sort', 'page', 'direction'],
'prefix' => null,
'scope' => null,
]);

$this->Paginator->paginate($table, $settings);
Expand Down Expand Up @@ -260,7 +260,7 @@ public function testDefaultPaginateParamsIntoRequest()
'page' => 1,
'order' => ['PaginatorPosts.id' => 'DESC'],
'whitelist' => ['limit', 'sort', 'page', 'direction'],
'prefix' => null,
'scope' => null,
]);

$this->Paginator->paginate($table, $settings);
Expand Down Expand Up @@ -295,16 +295,16 @@ public function testMergeOptionsModelSpecific()
}

/**
* test mergeOptions with custom prefix
* test mergeOptions with custom scope
*
* @return void
*/
public function testMergeOptionsCustomPrefix()
public function testMergeOptionsCustomScope()
{
$this->request->query = [
'page' => 10,
'limit' => 10,
'prefix' => [
'scope' => [
'page' => 2,
'limit' => 5,
]
Expand All @@ -331,7 +331,7 @@ public function testMergeOptionsCustomPrefix()
'limit' => 20,
'maxLimit' => 100,
'finder' => 'myCustomFind',
'prefix' => 'non-existent',
'scope' => 'non-existent',
];
$result = $this->Paginator->mergeOptions('Post', $settings);
$expected = [
Expand All @@ -340,7 +340,7 @@ public function testMergeOptionsCustomPrefix()
'maxLimit' => 100,
'finder' => 'myCustomFind',
'whitelist' => ['limit', 'sort', 'page', 'direction'],
'prefix' => 'non-existent',
'scope' => 'non-existent',
];
$this->assertEquals($expected, $result);

Expand All @@ -350,7 +350,7 @@ public function testMergeOptionsCustomPrefix()
'limit' => 20,
'maxLimit' => 100,
'finder' => 'myCustomFind',
'prefix' => 'prefix',
'scope' => 'scope',
];
$result = $this->Paginator->mergeOptions('Post', $settings);
$expected = [
Expand All @@ -359,7 +359,7 @@ public function testMergeOptionsCustomPrefix()
'maxLimit' => 100,
'finder' => 'myCustomFind',
'whitelist' => ['limit', 'sort', 'page', 'direction'],
'prefix' => 'prefix',
'scope' => 'scope',
];
$this->assertEquals($expected, $result);
}
Expand Down Expand Up @@ -522,7 +522,7 @@ public function testValidateSortInvalid()
'page' => 1,
'order' => ['PaginatorPosts.id' => 'asc'],
'whitelist' => ['limit', 'sort', 'page', 'direction'],
'prefix' => null,
'scope' => null,
]);

$this->request->query = [
Expand Down Expand Up @@ -1031,7 +1031,7 @@ public function testPaginateCustomFindCount()
'page' => 1,
'order' => [],
'whitelist' => ['limit', 'sort', 'page', 'direction'],
'prefix' => null,
'scope' => null,
]);
$this->Paginator->paginate($table, $settings);
}
Expand Down Expand Up @@ -1065,7 +1065,7 @@ public function testPaginateQuery()
'order' => ['PaginatorPosts.id' => 'ASC'],
'page' => 1,
'whitelist' => ['limit', 'sort', 'page', 'direction'],
'prefix' => null,
'scope' => null,
]);
$this->Paginator->paginate($query, $settings);
}
Expand Down Expand Up @@ -1125,7 +1125,7 @@ public function testPaginateQueryWithLimit()
'order' => ['PaginatorPosts.id' => 'ASC'],
'page' => 1,
'whitelist' => ['limit', 'sort', 'page', 'direction'],
'prefix' => null,
'scope' => null,
]);
$this->Paginator->paginate($query, $settings);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/View/Helper/PaginatorHelperTest.php
Expand Up @@ -446,7 +446,7 @@ public function testSortLinksMultiplePagination()
'sort' => 'date',
'direction' => 'asc',
'page' => 1,
'prefix' => 'article',
'scope' => 'article',
]
];

Expand Down Expand Up @@ -784,7 +784,7 @@ public function testGenerateUrlMultiplePagination()
['base' => '', 'here' => 'posts/index', 'webroot' => '/']
]);

$this->Paginator->request->params['paging']['Article']['prefix'] = 'article';
$this->Paginator->request->params['paging']['Article']['scope'] = 'article';
$this->Paginator->request->params['paging']['Article']['page'] = 3;
$this->Paginator->request->params['paging']['Article']['prevPage'] = true;
$this->Paginator->options(['model' => 'Article']);
Expand Down

0 comments on commit 2a4995e

Please sign in to comment.