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

Fix: Respect nullified record action and url in tables #15350

Open
wants to merge 2 commits into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,14 @@ public static function getRelationshipName(): string

protected function makeTable(): Table
{
return $this->makeBaseTable()
$table = $this->makeBaseTable()
->relationship(fn (): Relation | Builder => $this->getRelationship())
->modifyQueryUsing($this->modifyQueryWithActiveTab(...))
->queryStringIdentifier(Str::lcfirst(class_basename(static::class)))
->recordAction(function (Model $record, Table $table): ?string {
->queryStringIdentifier(Str::lcfirst(class_basename(static::class)));

// Only set recordAction and recordUrl if they haven't been explicitly set
if (! $table->hasRecordActionBeenSet()) {
$table->recordAction(function (Model $record, Table $table): ?string {
foreach (['view', 'edit'] as $action) {
$action = $table->getAction($action);

Expand All @@ -80,8 +83,11 @@ protected function makeTable(): Table
}

return null;
})
->recordUrl(function (Model $record, Table $table): ?string {
});
}

if (! $table->hasRecordUrlBeenSet()) {
$table->recordUrl(function (Model $record, Table $table): ?string {
foreach (['view', 'edit'] as $action) {
$action = $table->getAction($action);

Expand All @@ -105,7 +111,9 @@ protected function makeTable(): Table
}

return null;
})
->authorizeReorder(fn (): bool => $this->canReorder());
});
}

return $table->authorizeReorder(fn (): bool => $this->canReorder());
}
}
25 changes: 17 additions & 8 deletions packages/panels/src/Resources/Pages/ListRecords.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,15 @@ public function getPluralModelLabel(): ?string

protected function makeTable(): Table
{
return $this->makeBaseTable()
$table = $this->makeBaseTable()
->query(fn (): Builder => $this->getTableQuery())
->modifyQueryUsing($this->modifyQueryWithActiveTab(...))
->modelLabel($this->getModelLabel() ?? static::getResource()::getModelLabel())
->pluralModelLabel($this->getPluralModelLabel() ?? static::getResource()::getPluralModelLabel())
->recordAction(function (Model $record, Table $table): ?string {
->pluralModelLabel($this->getPluralModelLabel() ?? static::getResource()::getPluralModelLabel());

// Only set recordAction and recordUrl if they haven't been explicitly set
if (! $table->hasRecordActionBeenSet()) {
$table->recordAction(function (Model $record, Table $table): ?string {
foreach (['view', 'edit'] as $action) {
$action = $table->getAction($action);

Expand All @@ -270,9 +273,13 @@ protected function makeTable(): Table
}

return null;
})
->recordTitle(fn (Model $record): string => static::getResource()::getRecordTitle($record))
->recordUrl($this->getTableRecordUrlUsing() ?? function (Model $record, Table $table): ?string {
});
}

$table->recordTitle(fn (Model $record): string => static::getResource()::getRecordTitle($record));

if (! $table->hasRecordUrlBeenSet()) {
$table->recordUrl($this->getTableRecordUrlUsing() ?? function (Model $record, Table $table): ?string {
foreach (['view', 'edit'] as $action) {
$action = $table->getAction($action);

Expand Down Expand Up @@ -314,8 +321,10 @@ protected function makeTable(): Table
}

return null;
})
->authorizeReorder(static::getResource()::canReorder());
});
}

return $table->authorizeReorder(static::getResource()::canReorder());
}

/**
Expand Down
8 changes: 8 additions & 0 deletions packages/tables/src/Table/Concerns/HasRecordAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,21 @@ trait HasRecordAction
{
protected string | Closure | null $recordAction = null;

protected bool $hasRecordActionBeenSet = false;

public function recordAction(string | Closure | null $action): static
{
$this->recordAction = $action;
$this->hasRecordActionBeenSet = true;

return $this;
}

public function hasRecordActionBeenSet(): bool
{
return $this->hasRecordActionBeenSet;
}

public function getRecordAction(Model $record): ?string
{
$action = $this->evaluate(
Expand Down
8 changes: 8 additions & 0 deletions packages/tables/src/Table/Concerns/HasRecordUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ trait HasRecordUrl

protected string | Closure | null $recordUrl = null;

protected bool $hasRecordUrlBeenSet = false;

public function openRecordUrlInNewTab(bool | Closure $condition = true): static
{
$this->shouldOpenRecordUrlInNewTab = $condition;
Expand All @@ -22,10 +24,16 @@ public function recordUrl(string | Closure | null $url, bool | Closure $shouldOp
{
$this->openRecordUrlInNewTab($shouldOpenInNewTab);
$this->recordUrl = $url;
$this->hasRecordUrlBeenSet = true;

return $this;
}

public function hasRecordUrlBeenSet(): bool
{
return $this->hasRecordUrlBeenSet;
}

public function getRecordUrl(Model $record): ?string
{
return $this->evaluate(
Expand Down
218 changes: 218 additions & 0 deletions tests/Tables/TableConfigurationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
<?php

namespace Filament\Tests\Tables;

use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables\Contracts\HasTable;
use Filament\Tables\Table;
use Filament\Tests\TestCase;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Mockery;

class TestOwnerModel extends Model
{
protected $table = 'test_owners';
public $timestamps = false;
protected $fillable = ['name'];

public function items(): HasMany
{
return $this->hasMany(TestItemModel::class, 'owner_id');
}
}

class TestItemModel extends Model
{
protected $table = 'test_items';
public $timestamps = false;
protected $fillable = ['name', 'owner_id'];
}

uses(TestCase::class)->group('tables');

beforeEach(function () {
$this->livewire = Mockery::mock(HasTable::class);

Schema::create('test_owners', function (Blueprint $table) {
$table->id();
$table->string('name')->nullable();
});

Schema::create('test_items', function (Blueprint $table) {
$table->id();
$table->foreignId('owner_id')->nullable()->constrained('test_owners')->cascadeOnDelete();
$table->string('name')->nullable();
});
});

afterEach(function () {
Mockery::close();
Schema::dropIfExists('test_items');
Schema::dropIfExists('test_owners');
});

it('can nullify table record action', function () {
Table::configureUsing(fn (Table $table) => $table
->recordAction(null)
);

$table = Table::make($this->livewire);

expect($table)
->hasRecordActionBeenSet()->toBeTrue()
->getRecordAction(new TestItemModel())->toBeNull();
});

it('can nullify table record url', function () {
Table::configureUsing(fn (Table $table) => $table
->recordUrl(null)
);

$table = Table::make($this->livewire);

expect($table)
->hasRecordUrlBeenSet()->toBeTrue()
->getRecordUrl(new TestItemModel())->toBeNull();
});

it('can set table record action and url together', function () {
Table::configureUsing(fn (Table $table) => $table
->recordAction('edit')
->recordUrl('/edit/1')
);

$table = Table::make($this->livewire);

expect($table)
->hasRecordActionBeenSet()->toBeTrue()
->hasRecordUrlBeenSet()->toBeTrue()
->getRecordAction(new TestItemModel())->toBe('edit')
->getRecordUrl(new TestItemModel())->toBe('/edit/1');
});

it('can nullify table record action and url together', function () {
Table::configureUsing(fn (Table $table) => $table
->recordAction(null)
->recordUrl(null)
);

$table = Table::make($this->livewire);

expect($table)
->hasRecordActionBeenSet()->toBeTrue()
->hasRecordUrlBeenSet()->toBeTrue()
->getRecordAction(new TestItemModel())->toBeNull()
->getRecordUrl(new TestItemModel())->toBeNull();
});

it('respects closure values for record action and url', function () {
$model = new TestItemModel();

Table::configureUsing(fn (Table $table) => $table
->recordAction(fn (Model $record) => 'edit-' . $record::class)
->recordUrl(fn (Model $record) => '/edit/' . $record::class)
);

$table = Table::make($this->livewire);

expect($table)
->hasRecordActionBeenSet()->toBeTrue()
->hasRecordUrlBeenSet()->toBeTrue()
->getRecordAction($model)->toBe('edit-' . $model::class)
->getRecordUrl($model)->toBe('/edit/' . $model::class);
});

it('respects nullified record url in relation manager', function () {
$owner = TestOwnerModel::create(['name' => 'Test Owner']);

$relationManager = Mockery::mock(RelationManager::class)
->makePartial()
->shouldAllowMockingProtectedMethods();

$relationManager->shouldReceive('getRelationship')
->andReturn($owner->items());

$relationManager->shouldReceive('table')
->andReturnUsing(function (Table $table) {
return $table->recordUrl(null);
});

$table = Table::make($relationManager);
$table = $relationManager->table($table);

expect($table)
->hasRecordUrlBeenSet()->toBeTrue()
->getRecordUrl(new TestItemModel())->toBeNull();
});

it('respects nullified record action in relation manager', function () {
$owner = TestOwnerModel::create(['name' => 'Test Owner']);

$relationManager = Mockery::mock(RelationManager::class)
->makePartial()
->shouldAllowMockingProtectedMethods();

$relationManager->shouldReceive('getRelationship')
->andReturn($owner->items());

$relationManager->shouldReceive('table')
->andReturnUsing(function (Table $table) {
return $table->recordAction(null);
});

$table = Table::make($relationManager);
$table = $relationManager->table($table);

expect($table)
->hasRecordActionBeenSet()->toBeTrue()
->getRecordAction(new TestItemModel())->toBeNull();
});

it('respects custom record url in relation manager', function () {
$owner = TestOwnerModel::create(['name' => 'Test Owner']);

$relationManager = Mockery::mock(RelationManager::class)
->makePartial()
->shouldAllowMockingProtectedMethods();

$relationManager->shouldReceive('getRelationship')
->andReturn($owner->items());

$relationManager->shouldReceive('table')
->andReturnUsing(function (Table $table) {
return $table->recordUrl('/custom/url');
});

$table = Table::make($relationManager);
$table = $relationManager->table($table);

expect($table)
->hasRecordUrlBeenSet()->toBeTrue()
->getRecordUrl(new TestItemModel())->toBe('/custom/url');
});

it('respects custom record action in relation manager', function () {
$owner = TestOwnerModel::create(['name' => 'Test Owner']);

$relationManager = Mockery::mock(RelationManager::class)
->makePartial()
->shouldAllowMockingProtectedMethods();

$relationManager->shouldReceive('getRelationship')
->andReturn($owner->items());

$relationManager->shouldReceive('table')
->andReturnUsing(function (Table $table) {
return $table->recordAction('custom-action');
});

$table = Table::make($relationManager);
$table = $relationManager->table($table);

expect($table)
->hasRecordActionBeenSet()->toBeTrue()
->getRecordAction(new TestItemModel())->toBe('custom-action');
});
Loading