Skip to content
Browse files

Re-factor element generation.

Add the ability for arbitrary attributes.
Use built-in methods for attribute generation.
Add tests for element generation.
  • Loading branch information...
1 parent 4f12f25 commit a80a7dadf8c46c1b2573f6cea7e4aa908c8c3f99 @markstory markstory committed
View
2 lib/Cake/Test/Case/Utility/CakeTimeTest.php
@@ -196,7 +196,7 @@ public function testTimeAgoInWords() {
$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
26 lib/Cake/Test/Case/View/Helper/TimeHelperTest.php
@@ -119,10 +119,10 @@ public function testEngineOverride() {
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 @@ public function testTimeAgoInWords() {
);
$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
27 lib/Cake/View/Helper/TimeHelper.php
@@ -353,34 +353,31 @@ public function timeAgoInWords($dateTime, $options = array()) {
$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;
}

0 comments on commit a80a7da

Please sign in to comment.
Something went wrong with that request. Please try again.