Skip to content

Commit

Permalink
Use actionable fields in bulk requests (#433)
Browse files Browse the repository at this point in the history
* Use actionable fields in bulk requests

* Add tests for actionable fields on bulk requests
  • Loading branch information
dsindrilaru authored Nov 17, 2021
1 parent 888eb12 commit 648f424
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Actions/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

/**
* Class Action
* @method JsonResponse handle(Request $request, Model|Collection $models)
* @method JsonResponse handle(Request $request, Model|Collection $models, ?int $row)
* @package Binaryk\LaravelRestify\Actions
*/
abstract class Action implements JsonSerializable
Expand Down
11 changes: 7 additions & 4 deletions src/Fields/FieldCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ public function forStore(RestifyRequest $request, $repository): self
})->values();
}

public function withActions(RestifyRequest $request, $repository): self
public function withActions(RestifyRequest $request, $repository, $row = null): self
{
return $this
->inRequest($request)
->inRequest($request, $row)
->filter(fn (Field $field) => $field->isActionable())
->values();
}
Expand Down Expand Up @@ -154,10 +154,13 @@ public function findFieldByAttribute($attribute, $default = null)
return null;
}

public function inRequest(RestifyRequest $request): self
public function inRequest(RestifyRequest $request, $row = null): self
{
return $this
->filter(fn (Field $field) => $request->has($field->attribute) || $request->hasFile($field->attribute))
->filter(fn (Field $field) =>
$request->hasAny($field->attribute, $row.'.'.$field->attribute)
|| $request->hasFile($field->attribute)
)
->values();
}
}
16 changes: 16 additions & 0 deletions src/Repositories/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,7 @@ public function storeBulk(RepositoryStoreBulkRequest $request)
$this->resource,
$fields = $this->collectFields($request)
->forStoreBulk($request, $this)
->withoutActions($request, $this)
->authorizedUpdateBulk($request),
$row
);
Expand All @@ -680,6 +681,13 @@ public function storeBulk(RepositoryStoreBulkRequest $request)

$fields->each(fn (Field $field) => $field->invokeAfter($request, $this->resource));

$this
->collectFields($request)
->forStoreBulk($request, $this)
->withActions($request, $this, $row)
->authorizedUpdateBulk($request)
->each(fn (Field $field) => $field->actionHandler->handle($request, $this->resource, $row));

return $this->resource;
});
});
Expand Down Expand Up @@ -764,12 +772,20 @@ public function updateBulk(RestifyRequest $request, $repositoryId, int $row)
{
$fields = $this->collectFields($request)
->forUpdateBulk($request, $this)
->withoutActions($request, $this)
->authorizedUpdateBulk($request);

static::fillBulkFields($request, $this->resource, $fields, $row);

$this->resource->save();

$this
->collectFields($request)
->forUpdateBulk($request, $this)
->withActions($request, $this, $row)
->authorizedUpdateBulk($request)
->each(fn (Field $field) => $field->actionHandler->handle($request, $this->resource, $row));

static::updatedBulk($this->resource, $request);

return response()->json();
Expand Down
106 changes: 106 additions & 0 deletions tests/Actions/FieldActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,110 @@ public function handle(RestifyRequest $request, Post $post)
->etc()
);
}

/** @test */
public function can_use_actionable_field_on_bulk_store(): void
{
$action = new class extends Action {
public bool $showOnShow = true;

public function handle(RestifyRequest $request, Post $post, int $row)
{
$description = data_get($request[$row], 'description');

$post->update([
'description' => 'Actionable ' . $description,
]);
}
};

PostRepository::partialMock()
->shouldReceive('fieldsForStoreBulk')
->andreturn([
Field::new('title'),

Field::new('description')->action($action),
]);

$this
->withoutExceptionHandling()
->postJson(PostRepository::to('bulk'), [
[
'title' => $title1 = 'First title',
'description' => 'first description',
],
[
'title' => $title2 = 'Second title',
'description' => 'second description',
],
])
->assertJson(
fn (AssertableJson $json) => $json
->where('data.0.title', $title1)
->where('data.0.description', 'Actionable first description')
->where('data.1.title', $title2)
->where('data.1.description', 'Actionable second description')
->etc()
);
}

/** @test */
public function can_use_actionable_field_on_bulk_update(): void
{
$action = new class extends Action {
public bool $showOnShow = true;

public function handle(RestifyRequest $request, Post $post, int $row)
{
$description = data_get($request[$row], 'description');

$post->update([
'description' => 'Actionable ' . $description,
]);
}
};

PostRepository::partialMock()
->shouldReceive('fieldsForUpdateBulk')
->andreturn([
Field::new('title'),

Field::new('description')->action($action),
]);

$postId1 = $this
->withoutExceptionHandling()
->postJson(PostRepository::to(), [
'title' => 'First title',
])->json('data.id');

$postId2 = $this
->withoutExceptionHandling()
->postJson(PostRepository::to(), [
'title' => 'Second title',
])->json('data.id');

$this
->withoutExceptionHandling()
->postJson(PostRepository::to('bulk/update'), [
[
'id' => $postId1,
'description' => 'first description',
],
[
'id' => $postId2,
'description' => 'second description',
],
])->assertOk();

$this->assertSame(
'Actionable first description',
Post::find($postId1)->description
);

$this->assertSame(
'Actionable second description',
Post::find($postId2)->description
);
}
}

0 comments on commit 648f424

Please sign in to comment.