Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Random wild refactors #84

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
wip
  • Loading branch information
httpoz committed Feb 9, 2022
commit c5d3546d5dceaeaa4ce3c72377aa92a4e0a22f8b
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ $moderatorRole = \HttpOz\Roles\Models\Role::create([
]);
```

> Because of `Slugable` trait, if you make a mistake and for example leave a space in slug parameter, it'll be replaced with a dot automatically, because of `str_slug` function.
> Because of `Sluggable` trait, if you make a mistake and for example leave a space in slug parameter, it'll be replaced with a dot automatically, because of `str_slug` function.

### Attaching And Detaching Roles

Expand Down
22 changes: 16 additions & 6 deletions src/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@


use HttpOz\Roles\Database\Factories\RoleFactory;
use HttpOz\Roles\Traits\Sluggable;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

use HttpOz\Roles\Traits\RoleHasRelations;
use HttpOz\Roles\Contracts\RoleHasRelations as RoleHasRelationsContract;
use Illuminate\Support\Str;

class Role extends Model implements RoleHasRelationsContract
{

use Sluggable, RoleHasRelations, HasFactory;
use RoleHasRelations, HasFactory;

/**
* The attributes that are mass assignable.
Expand All @@ -25,10 +26,6 @@ class Role extends Model implements RoleHasRelationsContract

/**
* Create a new model instance.
*
* @param array $attributes
*
* @return void
*/
public function __construct(array $attributes = [])
{
Expand All @@ -52,4 +49,17 @@ protected static function newFactory(): RoleFactory
return new RoleFactory();
}

public function Slug(): Attribute
{
return new Attribute(
set: fn ($value) => Str::slug($value, config('roles.separator')),
);
}

public function Group(): Attribute
{
return new Attribute(
set: fn ($value) => Str::slug($value, config('roles.separator')),
);
}
}
26 changes: 0 additions & 26 deletions src/Traits/Sluggable.php

This file was deleted.

20 changes: 20 additions & 0 deletions tests/Unit/RoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,24 @@ public function testRoleCanBeFoundBySlug()
$this->assertEquals($createdRole->name, $foundRole->name);
$this->assertEquals($createdRole->slug, $foundRole->slug);
}

/**
* @dataProvider roleProvider
*/
public function testMinimalInputRoleCreation(string $name, ?string $slug, string $expected): void
{
$role = Role::create(['name' => $name, 'slug' => $slug]);

$this->assertEquals($expected, $role->slug);
}

public function roleProvider(): array
{
return [
['Big Fish', 'Big Fish', 'big.fish'],
['Small Fish', 'small.fish', 'small.fish'],
['Medium Fish', 'medium_fish', 'mediumfish'],
['Dashing Fish', 'dashing-fish', 'dashing.fish']
];
}
}