Reordered PaginatorHelper::sort() key and title to be more consistent.

Fixes #1000
This commit is contained in:
Jeremy Harris 2010-09-03 16:41:26 -05:00 committed by mark_story
parent 5a75d1d91a
commit 733f4f395e
2 changed files with 20 additions and 19 deletions

12
cake/libs/view/helpers/paginator.php Normal file → Executable file
View file

@ -292,20 +292,20 @@ class PaginatorHelper extends AppHelper {
* - `escape` Whether you want the contents html entity encoded, defaults to true
* - `model` The model to use, defaults to PaginatorHelper::defaultModel()
*
* @param string $title Title for the link.
* @param string $key The name of the key that the recordset should be sorted. If $key is null
* $title will be used for the key, and a title will be generated by inflection.
* @param string $key The name of the key that the recordset should be sorted.
* @param string $title Title for the link. If $title is null $key will be used
* for the title and will be generated by inflection.
* @param array $options Options for sorting link. See above for list of keys.
* @return string A link sorting default by 'asc'. If the resultset is sorted 'asc' by the specified
* key the returned link will sort by 'desc'.
*/
public function sort($title, $key = null, $options = array()) {
public function sort($key, $title = null, $options = array()) {
$options = array_merge(array('url' => array(), 'model' => null), $options);
$url = $options['url'];
unset($options['url']);
if (empty($key)) {
$key = $title;
if (empty($title)) {
$title = $key;
$title = __(Inflector::humanize(preg_replace('/_id$/', '', $title)));
}
$dir = isset($options['direction']) ? $options['direction'] : 'asc';

27
cake/tests/cases/libs/view/helpers/paginator.test.php Normal file → Executable file
View file

@ -168,7 +168,7 @@ class PaginatorHelperTest extends CakeTestCase {
);
$this->assertTags($result, $expected);
$result = $this->Paginator->sort('TestTitle', 'title');
$result = $this->Paginator->sort('title', 'TestTitle');
$expected = array(
'a' => array('href' => '/officespace/accounts/index/param/page:1/sort:title/direction:asc'),
'TestTitle',
@ -176,7 +176,7 @@ class PaginatorHelperTest extends CakeTestCase {
);
$this->assertTags($result, $expected);
$result = $this->Paginator->sort(array('asc' => 'ascending', 'desc' => 'descending'), 'title');
$result = $this->Paginator->sort('title', array('asc' => 'ascending', 'desc' => 'descending'));
$expected = array(
'a' => array('href' => '/officespace/accounts/index/param/page:1/sort:title/direction:asc'),
'ascending',
@ -185,7 +185,7 @@ class PaginatorHelperTest extends CakeTestCase {
$this->assertTags($result, $expected);
$this->Paginator->request->params['paging']['Article']['options']['sort'] = 'title';
$result = $this->Paginator->sort(array('asc' => 'ascending', 'desc' => 'descending'), 'title');
$result = $this->Paginator->sort('title', array('asc' => 'ascending', 'desc' => 'descending'));
$expected = array(
'a' => array('href' => '/officespace/accounts/index/param/page:1/sort:title/direction:desc', 'class' => 'asc'),
'descending',
@ -205,29 +205,29 @@ class PaginatorHelperTest extends CakeTestCase {
$this->Paginator->request->params['paging']['Article']['options']['order'] = array('Article.title' => 'desc');
$this->Paginator->request->params['paging']['Article']['options']['sort'] = null;
$result = $this->Paginator->sort('Title', 'title', array('direction' => 'desc'));
$result = $this->Paginator->sort('title', 'Title', array('direction' => 'desc'));
$this->assertPattern('/\/accounts\/index\/param\/page:1\/sort:title\/direction:asc" class="desc">Title<\/a>$/', $result);
$this->Paginator->request->params['paging']['Article']['options']['order'] = array('Article.title' => 'desc');
$this->Paginator->request->params['paging']['Article']['options']['sort'] = null;
$result = $this->Paginator->sort('Title', 'title', array('direction' => 'asc'));
$result = $this->Paginator->sort('title', 'Title', array('direction' => 'asc'));
$this->assertPattern('/\/accounts\/index\/param\/page:1\/sort:title\/direction:asc" class="desc">Title<\/a>$/', $result);
$this->Paginator->request->params['paging']['Article']['options']['order'] = array('Article.title' => 'asc');
$this->Paginator->request->params['paging']['Article']['options']['sort'] = null;
$result = $this->Paginator->sort('Title', 'title', array('direction' => 'asc'));
$result = $this->Paginator->sort('title', 'Title', array('direction' => 'asc'));
$this->assertPattern('/\/accounts\/index\/param\/page:1\/sort:title\/direction:desc" class="asc">Title<\/a>$/', $result);
$this->Paginator->request->params['paging']['Article']['options']['order'] = array('Article.title' => 'asc');
$this->Paginator->request->params['paging']['Article']['options']['sort'] = null;
$result = $this->Paginator->sort('Title', 'title', array('direction' => 'desc'));
$result = $this->Paginator->sort('title', 'Title', array('direction' => 'desc'));
$this->assertPattern('/\/accounts\/index\/param\/page:1\/sort:title\/direction:desc" class="asc">Title<\/a>$/', $result);
$this->Paginator->request->params['paging']['Article']['options']['order'] = array('Article.title' => 'asc');
$this->Paginator->request->params['paging']['Article']['options']['sort'] = null;
$result = $this->Paginator->sort('Title', 'title', array('direction' => 'desc', 'class' => 'foo'));
$result = $this->Paginator->sort('title', 'Title', array('direction' => 'desc', 'class' => 'foo'));
$this->assertPattern('/\/accounts\/index\/param\/page:1\/sort:title\/direction:desc" class="foo asc">Title<\/a>$/', $result);
}
@ -293,7 +293,7 @@ class PaginatorHelperTest extends CakeTestCase {
));
$this->Paginator->options(array('url' => array('param')));
$result = $this->Paginator->sort('TestTitle', 'title', array('direction' => 'desc'));
$result = $this->Paginator->sort('title', 'TestTitle', array('direction' => 'desc'));
$expected = array(
'a' => array('href' => '/accounts/index/param/page:1/sort:title/direction:desc'),
'TestTitle',
@ -301,7 +301,7 @@ class PaginatorHelperTest extends CakeTestCase {
);
$this->assertTags($result, $expected);
$result = $this->Paginator->sort(array('asc' => 'ascending', 'desc' => 'descending'), 'title', array('direction' => 'desc'));
$result = $this->Paginator->sort('title', array('asc' => 'ascending', 'desc' => 'descending'), array('direction' => 'desc'));
$expected = array(
'a' => array('href' => '/accounts/index/param/page:1/sort:title/direction:desc'),
'descending',
@ -325,7 +325,7 @@ class PaginatorHelperTest extends CakeTestCase {
));
$this->Paginator->request->params['paging']['Article']['options']['order'] = array('Article.title' => 'desc');
$result = $this->Paginator->sort('Title','Article.title');
$result = $this->Paginator->sort('Article.title', 'Title');
$expected = array(
'a' => array('href' => '/officespace/accounts/index/page:1/sort:Article.title/direction:asc', 'class' => 'desc'),
'Title',
@ -333,8 +333,9 @@ class PaginatorHelperTest extends CakeTestCase {
);
$this->assertTags($result, $expected);
$this->Paginator->request->params['paging']['Article']['options']['order'] = array('Article.title' => 'asc');
$result = $this->Paginator->sort('Title','Article.title');
$result = $this->Paginator->sort('Article.title', 'Title');
$expected = array(
'a' => array('href' => '/officespace/accounts/index/page:1/sort:Article.title/direction:desc', 'class' => 'asc'),
'Title',
@ -483,7 +484,7 @@ class PaginatorHelperTest extends CakeTestCase {
$this->assertTags($result, $expected);
$this->Paginator->options(array('url' => array('param')));
$result = $this->Paginator->sort('Title', 'Article.title');
$result = $this->Paginator->sort('Article.title', 'Title');
$expected = array(
'a' => array('href' => '/admin/test/index/param/page:1/sort:Article.title/direction:asc'),
'Title',