Skip to content

Commit

Permalink
Merge pull request #32 from PortableStudios/feature/FILACMS-30
Browse files Browse the repository at this point in the history
Fix Lint
  • Loading branch information
kyoungportable authored Apr 13, 2024
2 parents b1e1c18 + cf957bb commit cb83ea4
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 14 deletions.
17 changes: 9 additions & 8 deletions src/Filament/Resources/RoleResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Portable\FilaCms\Filament\Resources\RoleResource\Pages;
use Portable\FilaCms\Filament\Resources\RoleResource\RelationManagers;
use Portable\FilaCms\Filament\Traits\IsProtectedResource;
use Spatie\Permission\Models\Role;

Expand Down Expand Up @@ -37,9 +38,9 @@ public static function table(Table $table): Table
->columns([
TextColumn::make('name')->sortable(),
TextColumn::make('permissions.name')
->limitList(4)
->badge()
->distinctList()
->listWithLineBreaks()
->bulleted(),
])
->searchable()
->filters([
Expand All @@ -55,19 +56,19 @@ public static function table(Table $table): Table
]);
}

public static function getRelations(): array
public static function getPages(): array
{
return [
//
'index' => Pages\ListRoles::route('/'),
'create' => Pages\CreateRole::route('/create'),
'edit' => Pages\EditRole::route('/{record}/edit'),
];
}

public static function getPages(): array
public static function getRelations(): array
{
return [
'index' => Pages\ListRoles::route('/'),
'create' => Pages\CreateRole::route('/create'),
'edit' => Pages\EditRole::route('/{record}/edit'),
RelationManagers\UsersRelationManager::class,
];
}
}
14 changes: 13 additions & 1 deletion src/Filament/Resources/RoleResource/Pages/EditRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace Portable\FilaCms\Filament\Resources\RoleResource\Pages;

use Filament\Actions;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\EditRecord;
use Portable\FilaCms\Filament\Resources\RoleResource;
use Spatie\Permission\Models\Role;

class EditRole extends EditRecord
{
Expand All @@ -13,7 +15,17 @@ class EditRole extends EditRecord
protected function getHeaderActions(): array
{
return [
Actions\DeleteAction::make(),
Actions\DeleteAction::make()
->before(function (Actions\DeleteAction $action, Role $role) {
if ($role->users->count() > 0) {
Notification::make()
->title('Unable to perform action')
->body('You cannot delete a role that is assigned to user(s)')
->status('warning')
->send();
$action->cancel();
}
}),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Portable\FilaCms\Filament\Resources\RoleResource\RelationManagers;

use Filament\Forms\Form;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables\Columns\ViewColumn;
use Filament\Tables\Table;

class UsersRelationManager extends RelationManager
{
protected static string $relationship = 'users';

public function form(Form $form): Form
{
return $form
->schema([]);
}

public function table(Table $table): Table
{
return $table
->recordTitleAttribute('Users')
->columns([
ViewColumn::make('name')
->view('fila-cms::tables.columns.roles-user'),
ViewColumn::make('created_at')
->label('Creation Date')
->view('fila-cms::tables.columns.created_at'),
]);
}
}
3 changes: 3 additions & 0 deletions tests/Factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Portable\FilaCms\Tests\User;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\User>
*/
class UserFactory extends Factory
{
protected $model = User::class;

/**
* The current password being used by the factory.
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/Filament/PageResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public function test_can_save_form(): void
$this->assertEquals($data->title, $new->title);
$this->assertEquals($data->author_id, $new->author_id);
$this->assertEquals($data->is_draft, $new->is_draft);
$this->assertGreaterThanOrEqual($data->updated_at->format('U'), $updatedTime->format('U'));
$this->assertGreaterThanOrEqual($updatedTime->format('U'), $data->updated_at->format('U'));
}

public function test_can_create_page_with_taxonomies(): void
Expand Down
2 changes: 1 addition & 1 deletion tests/Filament/PermissionResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,6 @@ public function test_can_save_form(): void

$data->refresh();
$this->assertEquals($data->name, $new->name);
$this->assertGreaterThanOrEqual($data->updated_at->format('U'), $updatedTime->format('U'));
$this->assertGreaterThanOrEqual($updatedTime->format('U'), $data->updated_at->format('U'));
}
}
49 changes: 46 additions & 3 deletions tests/Filament/RoleResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@

namespace Portable\FilaCms\Tests\Filament;

use Portable\FilaCms\Tests\TestCase;
use Illuminate\Foundation\Testing\Concerns\InteractsWithSession;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Livewire\Livewire;
use Portable\FilaCms\Filament\Resources\RoleResource as TargetResource;
use Portable\FilaCms\Filament\Resources\RoleResource\Pages\EditRole;
use Portable\FilaCms\Tests\TestCase;
use Portable\FilaCms\Tests\User;
use Spatie\Permission\Models\Role as TargetModel;
use Spatie\Permission\Models\Role;
use Livewire\Livewire;
use Illuminate\Foundation\Testing\WithFaker;

class RoleResourceTest extends TestCase
{
use RefreshDatabase;
use WithFaker;
use InteractsWithSession;

protected function setUp(): void
{
Expand Down Expand Up @@ -102,4 +106,43 @@ public function test_can_save_form(): void
$this->assertEquals($data->name, $new->name);
$this->assertGreaterThanOrEqual($data->updated_at->format('U'), $updatedTime->format('U'));
}

public function test_can_delete_without_users()
{
$role = Role::create(['name' => 'dummy-role']);
$livewireResponse = Livewire::test(EditRole::class, [
'record' => $role->getRoutekey(),
])
->call('mountAction', 'delete')
->call('callMountedAction');

$livewireResponse->assertSessionHas('filament.notifications', function ($notifications) {
return collect($notifications)->first()['title'] === 'Deleted';
});

$role = Role::find($role->id);
$this->assertNull($role);
}


public function test_cannot_delete_with_users()
{
$role = Role::create(['name' => 'dependant-role']);
$user = User::factory()->create();
$user->assignRole($role);

$livewire = Livewire::test(EditRole::class, [
'record' => $role->getRoutekey(),
]);

$livewire = $livewire->call('mountAction', 'delete');
$livewire = $livewire->call('callMountedAction');

$livewire->assertSessionHas('filament.notifications', function ($notifications) {
return collect($notifications)->first()['body'] === 'You cannot delete a role that is assigned to user(s)';
});

$role = Role::find($role->id);
$this->assertNotNull($role);
}
}
9 changes: 9 additions & 0 deletions views/tables/columns/created_at.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div class="fi-ta-text grid w-full gap-y-1 px-3 py-4">
<div class="flex">
<div class="fi-ta-text-item inline-flex items-center gap-1.5 ">
<span class="fi-ta-text-item-label text-sm leading-6 text-gray-950 dark:text-white">
{{ $getRecord()->created_at }}
</span>
</div>
</div>
</div>
9 changes: 9 additions & 0 deletions views/tables/columns/roles-user.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div class="fi-ta-text grid w-full gap-y-1 px-3 py-4">
<div class="flex">
<div class="fi-ta-text-item inline-flex items-center gap-1.5 ">
<span class="fi-ta-text-item-label text-sm leading-6 text-gray-950 dark:text-white">
{{ $getRecord()->name ?? $getRecord()->first_name }}
</span>
</div>
</div>
</div>

0 comments on commit cb83ea4

Please sign in to comment.