-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fields actions. * Fix styling * Actionable fields. * Fix styling Co-authored-by: binaryk <[email protected]>
- Loading branch information
Showing
7 changed files
with
175 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace Binaryk\LaravelRestify\Fields\Concerns; | ||
|
||
use Binaryk\LaravelRestify\Actions\Action; | ||
|
||
trait HasAction | ||
{ | ||
protected ?Action $actionHandler = null; | ||
|
||
public function action(Action $action): self | ||
{ | ||
if (! $action->onlyOnShow()) { | ||
$key = $action::$uriKey; | ||
|
||
abort(400, "The action $key should be only for show."); | ||
} | ||
|
||
$this->actionHandler = $action; | ||
|
||
return $this; | ||
} | ||
|
||
public function isActionable(): bool | ||
{ | ||
return $this->actionHandler instanceof Action; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace Binaryk\LaravelRestify\Tests\Actions; | ||
|
||
use Binaryk\LaravelRestify\Actions\Action; | ||
use Binaryk\LaravelRestify\Fields\Field; | ||
use Binaryk\LaravelRestify\Http\Requests\RestifyRequest; | ||
use Binaryk\LaravelRestify\Tests\Fixtures\Post\Post; | ||
use Binaryk\LaravelRestify\Tests\Fixtures\Post\PostRepository; | ||
use Binaryk\LaravelRestify\Tests\IntegrationTest; | ||
use Illuminate\Testing\Fluent\AssertableJson; | ||
|
||
class FieldActionTest extends IntegrationTest | ||
{ | ||
/** * @test */ | ||
public function can_use_actionable_field(): void | ||
{ | ||
$action = new class extends Action { | ||
public bool $showOnShow = true; | ||
|
||
public function handle(RestifyRequest $request, Post $post) | ||
{ | ||
$description = $request->input('description'); | ||
|
||
$post->update([ | ||
'description' => 'Actionable ' . $description, | ||
]); | ||
} | ||
}; | ||
|
||
PostRepository::partialMock() | ||
->shouldReceive('fieldsForStore') | ||
->andreturn([ | ||
Field::new('title'), | ||
|
||
Field::new('description')->action($action), | ||
]); | ||
|
||
$this | ||
->withoutExceptionHandling() | ||
->postJson(PostRepository::to(), [ | ||
'description' => 'Description', | ||
'title' => $updated = 'Title', | ||
]) | ||
->assertJson( | ||
fn (AssertableJson $json) => $json | ||
->where('data.attributes.title', $updated) | ||
->where('data.attributes.description', 'Actionable Description') | ||
->etc() | ||
); | ||
} | ||
} |