Skip to content

Commit

Permalink
Automatically add a role title when missing
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephSilber committed Jan 28, 2018
1 parent f945d9d commit 160c114
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Database/Concerns/IsRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Silber\Bouncer\Helpers;
use Silber\Bouncer\Database\Models;
use Silber\Bouncer\Database\Titles\RoleTitle;
use Silber\Bouncer\Database\Scope\BaseTenantScope;
use Silber\Bouncer\Database\Queries\Roles as RolesQuery;

Expand All @@ -30,6 +31,10 @@ public static function bootIsRole()

static::creating(function ($role) {
Models::scope()->applyToModel($role);

if (is_null($role->title)) {
$role->title = new RoleTitle($role);
}
});
}

Expand Down
13 changes: 13 additions & 0 deletions src/Database/Titles/RoleTitle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Silber\Bouncer\Database\Titles;

use Illuminate\Database\Eloquent\model;

class RoleTitle extends Title
{
public function __construct(Model $role)
{
$this->title = $this->humanize($role->name);
}
}
47 changes: 47 additions & 0 deletions src/Database/Titles/Title.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Silber\Bouncer\Database\Titles;

use Illuminate\Support\Str;

abstract class Title
{
/**
* The human-readable title.
*
* @var string
*/
protected $title;

/**
* Convert the given string into a human-readable format.
*
* @param string|null $value
* @return string
*/
protected function humanize($value)
{
if (is_null($value)) {
return '';
}

// First we'll convert the string to snake case. Then we'll
// convert all dashes and underscores to spaces. Finally,
// we'll uppercase the 1st letter of the whole string.
$value = Str::snake($value);

$value = preg_replace('~(?:-|_)+~', ' ', $value);

return ucfirst($value);
}

/**
* Get the title as a string.
*
* @return string
*/
public function __toString()
{
return $this->title;
}
}
56 changes: 56 additions & 0 deletions tests/AutoTitlesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

use Silber\Bouncer\Database\Role;
use Silber\Bouncer\Database\Ability;

class AutoTitlesTest extends BaseTestCase
{
public function test_role_title_is_never_overwritten()
{
$role = Role::create(['name' => 'admin', 'title' => 'Something Else']);

$this->assertEquals('Something Else', $role->title);
}

public function test_role_title_is_capitalized()
{
$role = Role::create(['name' => 'admin']);

$this->assertEquals('Admin', $role->title);
}

public function test_role_title_with_spaces()
{
$role = Role::create(['name' => 'site admin']);

$this->assertEquals('Site admin', $role->title);
}

public function test_role_title_with_dashes()
{
$role = Role::create(['name' => 'site-admin']);

$this->assertEquals('Site admin', $role->title);
}

public function test_role_title_with_underscores()
{
$role = Role::create(['name' => 'site_admin']);

$this->assertEquals('Site admin', $role->title);
}

public function test_role_title_with_camel_casing()
{
$role = Role::create(['name' => 'siteAdmin']);

$this->assertEquals('Site admin', $role->title);
}

public function test_role_title_with_studly_casing()
{
$role = Role::create(['name' => 'SiteAdmin']);

$this->assertEquals('Site admin', $role->title);
}
}

0 comments on commit 160c114

Please sign in to comment.