Skip to content

Commit

Permalink
refact: rename RoleModel -> Roleable
Browse files Browse the repository at this point in the history
This is part of the renaming refactoring to abide to Laravel standards.
  • Loading branch information
uwla committed Nov 20, 2024
1 parent 29a3739 commit 82a1549
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 39 deletions.
10 changes: 5 additions & 5 deletions database/migrations/create_acl_tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ public function up()
$table->timestamps();
});

Schema::create('role_model', function (Blueprint $table) {
$table->string('model_type');
$table->string('model_id');
Schema::create('roleables', function (Blueprint $table) {
$table->string('roleable_type');
$table->string('roleable_id');
$table->unsignedBigInteger('role_id');
$table->foreign('role_id')
->references('id')
->on('roles')
->cascadeOnDelete();
$table->primary(['model_type', 'model_id', 'role_id']);
$table->primary(['roleable_type', 'roleable_id', 'role_id']);
$table->timestamps();
});
}
Expand All @@ -67,7 +67,7 @@ public function up()
public function down()
{
Schema::dropIfExists('permissionables');
Schema::dropIfExists('role_model');
Schema::dropIfExists('roleables');
Schema::dropIfExists('roles');
Schema::dropIfExists('permissions');
}
Expand Down
4 changes: 2 additions & 2 deletions src/Models/RoleModel.php → src/Models/Roleable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class RoleModel extends Model
class Roleable extends Model
{
use HasFactory;

Expand All @@ -14,5 +14,5 @@ class RoleModel extends Model
*
* @var string
*/
protected $table = 'role_model';
protected $table = 'roleables';
}
10 changes: 5 additions & 5 deletions src/Traits/HasPermission.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Uwla\Lacl\Contracts\HasRoleContract;
use Uwla\Lacl\Models\Permission;
use Uwla\Lacl\Models\Permissionable;
use Uwla\Lacl\Models\RoleModel;
use Uwla\Lacl\Models\Roleable;

trait HasPermission
{
Expand Down Expand Up @@ -218,9 +218,9 @@ private function getThisPermissionsIds()
return $query->pluck('permission_id');
}

$role_ids = RoleModel::where([
'model_type' => $model,
'model_id' => $model_id,
$role_ids = Roleable::where([
'roleable_type' => $model,
'roleable_id' => $model_id,
])->pluck('role_id');

if ($role_ids->count() > 0) {
Expand Down Expand Up @@ -563,4 +563,4 @@ public static function withPermissionNames($models): Collection
}
return $models;
}
}
}
34 changes: 17 additions & 17 deletions src/Traits/HasRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
use Uwla\Lacl\Models\Role;
use Uwla\Lacl\Models\RoleModel;
use Uwla\Lacl\Models\Roleable;

trait HasRole
{
Expand All @@ -19,9 +19,9 @@ trait HasRole
*/
private function getBaseQuery(): Builder
{
return RoleModel::where([
'model_type' => $this::class,
'model_id' => $this->getModelId(),
return Roleable::where([
'roleable_type' => $this::class,
'roleable_id' => $this->getModelId(),
]);
}

Expand Down Expand Up @@ -71,12 +71,12 @@ public function addRoles($roles): void
$toAdd = [];
foreach ($roles as $role) {
$toAdd[] = [
'model_type' => $model,
'model_id' => $model_id,
'roleable_type' => $model,
'roleable_id' => $model_id,
'role_id' => $role->id,
];
}
RoleModel::insert($toAdd);
Roleable::insert($toAdd);
}

/*
Expand Down Expand Up @@ -212,13 +212,13 @@ public static function addRolesToMany($roles, $models): void
foreach ($role_ids as $rid) {
foreach ($model_ids as $mid) {
$toCreate[] = [
'model_type' => $model,
'model_id' => $mid,
'roleable_type' => $model,
'roleable_id' => $mid,
'role_id' => $rid,
];
}
}
RoleModel::insert($toCreate);
Roleable::insert($toCreate);
}

/*
Expand All @@ -245,10 +245,10 @@ public static function delRolesFromMany($roles, $models): void
$roles = static::normalizeRoles($roles);
$rids = $roles->pluck('id');
$uids = $models->pluck(static::getIdColumn());
RoleModel::query()
Roleable::query()
->whereIn('role_id', $rids)
->whereIn('model_id', $uids)
->where('model_type', static::class)
->whereIn('roleable_id', $uids)
->where('roleable_type', static::class)
->delete();
}

Expand All @@ -263,9 +263,9 @@ public static function withRoles($models): Collection
$models = static::normalizeModels($models);
$idColumn = static::getIdColumn();
$mids = $models->pluck($idColumn);
$role_models = RoleModel::query()
->where('model_type', static::class)
->whereIn('model_id', $mids)
$role_models = Roleable::query()
->where('roleable_type', static::class)
->whereIn('roleable_id', $mids)
->get();
$role_ids = $role_models->pluck('role_id');
$roles = Role::whereIn('id', $role_ids)->get();
Expand All @@ -289,7 +289,7 @@ public static function withRoles($models): Collection
$model->roles = collect();

foreach ($role_models as $role_model) {
$model_id = $role_model->model_id;
$model_id = $role_model->roleable_id;
$role_id = $role_model->role_id;
$model = $id2model[$model_id];
$role = $id2role[$role_id];
Expand Down
20 changes: 10 additions & 10 deletions tests/Feature/HasRoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Tests\App\Models\Role;
use Tests\App\Models\User;
use Tests\TestCase;
use Uwla\Lacl\Models\RoleModel;
use Uwla\Lacl\Models\Roleable;

class HasRoleTest extends TestCase
{
Expand Down Expand Up @@ -35,9 +35,9 @@ public function test_add_role(): void

$user->addRole($role);
$this->assertTrue(
RoleModel::where([
'model_type' => $user::class,
'model_id' => $user->id,
Roleable::where([
'roleable_type' => $user::class,
'roleable_id' => $user->id,
'role_id' => $role->id
])->exists()
);
Expand All @@ -54,10 +54,10 @@ public function test_add_roles(): void
$roles = Role::factory($this->m)->create();

$user->addRoles($roles);
$ids = RoleModel::query()
$ids = Roleable::query()
->where([
'model_id' => $user->id,
'model_type' => $user::class
'roleable_id' => $user->id,
'roleable_type' => $user::class
])->pluck('role_id');
$user_roles = Role::whereIn('id', $ids)->get();
$this->assertTrue($roles->diff($user_roles)->isEmpty());
Expand Down Expand Up @@ -214,10 +214,10 @@ public function test_mass_role_attribution(): void

$uid = $users->pluck('id');
$rid = $roles->pluck('id');
$f = fn() => RoleModel::query()
$f = fn() => Roleable::query()
->whereIn('role_id', $rid)
->whereIn('model_id', $uid)
->where('model_type', $users->first()::class)
->whereIn('roleable_id', $uid)
->where('roleable_type', $users->first()::class)
->count();

User::addRolesToMany($roles, $users);
Expand Down

0 comments on commit 82a1549

Please sign in to comment.