Skip to content

Commit

Permalink
Merge 366d363 into 7c6d9c1
Browse files Browse the repository at this point in the history
  • Loading branch information
leoruhland committed Jan 21, 2016
2 parents 7c6d9c1 + 366d363 commit 30203ff
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/Model/Behavior/SluggableBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Cake\ORM\Entity;
use Cake\ORM\Query;
use Cake\Utility\Inflector;
use Cake\Utility\Text;

class SluggableBehavior extends Behavior
{
Expand All @@ -19,6 +20,7 @@ class SluggableBehavior extends Behavior
'field' => 'title',
'slug' => 'slug',
'replacement' => '-',
'maxLength' => false,
];

/**
Expand All @@ -32,6 +34,9 @@ public function slug(Entity $entity)
{
$config = $this->config();
$value = $entity->get($config['field']);
if ($config['maxLength'] > 0) {
$value = Text::truncate($value, $config['maxLength'], ['ellipsis' => '']);
}
$entity->set($config['slug'], strtolower(Inflector::slug($value, $config['replacement'])));
}

Expand Down
5 changes: 3 additions & 2 deletions tests/TestCase/Model/Behavior/SluggableBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public function setUp()
{
$this->Model = TableRegistry::get('Users');
$this->Model->addBehavior('Xety/Cake3Sluggable.Sluggable', [
'field' => 'username'
'field' => 'username',
'maxLength' => 5,
]);
}

Expand Down Expand Up @@ -68,6 +69,6 @@ public function testBeforeSave()
$after = $this->Model->get(1);

$this->assertEquals('mariano', $before->slug);
$this->assertEquals('larry-page', $after->slug);
$this->assertEquals('larry', $after->slug);
}
}

0 comments on commit 30203ff

Please sign in to comment.