Re-factor element generation.

Add the ability for arbitrary attributes.
Use built-in methods for attribute generation.
Add tests for element generation.
This commit is contained in:
mark_story 2012-05-12 21:16:57 -04:00
parent 4f12f254f4
commit a80a7dadf8
3 changed files with 35 additions and 20 deletions

View file

@ -196,7 +196,7 @@ class CakeTimeTest extends CakeTestCase {
$seconds = round($i + ($i * $i));
$time = strtotime('+' . $seconds . ' seconds');
$result = $this->Time->timeAgoInWords($time);
if ($time == time()) {
$expected = 'just now';
$this->assertEquals($expected, $result);

View file

@ -119,10 +119,10 @@ class TimeHelperTest extends CakeTestCase {
public function testTimeAgoInWords() {
$Time = new TimeHelper($this->View);
$timestamp = strtotime('+8 years, +4 months +2 weeks +3 days');
$result = $Time->timeAgoInWords(
$timestamp,
array('end' => '1 years', 'element' => 'span')
);
$result = $Time->timeAgoInWords($timestamp, array(
'end' => '1 years',
'element' => 'span'
));
$expected = array(
'span' => array(
'title' => $timestamp,
@ -133,6 +133,24 @@ class TimeHelperTest extends CakeTestCase {
);
$this->assertTags($result, $expected);
$result = $Time->timeAgoInWords($timestamp, array(
'end' => '1 years',
'element' => array(
'title' => 'testing',
'rel' => 'test'
)
));
$expected = array(
'span' => array(
'title' => 'testing',
'class' => 'time-ago-in-words',
'rel' => 'test'
),
'on ' . date('j/n/y', $timestamp),
'/span'
);
$this->assertTags($result, $expected);
$timestamp = strtotime('+2 weeks');
$result = $Time->timeAgoInWords(
$timestamp,

View file

@ -353,34 +353,31 @@ class TimeHelper extends AppHelper {
$stringDate = '';
if (isset($options['element'])) {
$element_options = array(
if (!empty($options['element'])) {
$element = array(
'tag' => 'span',
'class' => 'time-ago-in-words',
'title' => $dateTime
);
if (is_array($options['element'])) {
$element = array_merge($element_options, $options['element']);
$element = array_merge($element, $options['element']);
} else {
if ($options['element']) {
$element = $element_options;
$element['tag'] = $options['element'];
} else {
$element = null;
}
$element['tag'] = $options['element'];
}
unset($options['element']);
}
$relativeDate = $this->_engine->timeAgoInWords($dateTime, $options);
// Apply HTML element
if ($element) {
$title = isset($element['title']) ? ' title="'.$element['title'].'"' : '';
$class = isset($element['class']) ? ' class="'.$element['class'].'"' : '';
$relativeDate = '<'.$element['tag'].$title.$class.'>'.$relativeDate.'</'.$element['tag'].'>';
$relativeDate = sprintf(
'<%s%s>%s</%s>',
$element['tag'],
$this->_parseAttributes($element, array('tag')),
$relativeDate,
$element['tag']
);
}
return $relativeDate;
}