Skip to content

Commit 6b3fe69

Browse files
committed
Enhance getComments method to support optional limit parameter and update CommentList component to utilize this feature.
1 parent 7cc9c64 commit 6b3fe69

File tree

4 files changed

+76
-8
lines changed

4 files changed

+76
-8
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ Sometimes you might want to render non-Comments in the list of comments. For exa
540540
```php
541541
use Kirschbaum\Commentions\RenderableComment;
542542
543-
public function getComments(): Collection
543+
public function getComments(?int $limit = null): Collection
544544
{
545545
$statusHistory = $this->statusHistory()->get()->map(fn (StatusHistory $statusHistory) => new RenderableComment(
546546
id: $statusHistory->id,
@@ -551,7 +551,13 @@ public function getComments(): Collection
551551
552552
$comments = $this->comments()->latest()->with('author')->get();
553553
554-
return $statusHistory->merge($comments);
554+
$mergedCollection = $statusHistory->merge($comments);
555+
556+
if ($limit) {
557+
return $mergedCollection->take($limit);
558+
}
559+
560+
return $mergedCollection;
555561
}
556562
```
557563

src/HasComments.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ public function comment(string $body, ?Commenter $author): Comment
2626
return SaveComment::run($this, $author, $body);
2727
}
2828

29-
public function getComments(): Collection
29+
public function getComments(?int $limit = null): Collection
3030
{
31+
if ($limit) {
32+
return $this->commentsQuery()->limit($limit)->get();
33+
}
34+
3135
return $this->commentsQuery()->get();
3236
}
3337

src/Livewire/CommentList.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,7 @@ public function render()
2727
#[Computed]
2828
public function comments(): Collection
2929
{
30-
if (! $this->paginate) {
31-
return $this->record->getComments();
32-
}
33-
34-
return $this->record->getComments()->take($this->perPage);
30+
return $this->record->getComments($this->paginate ? $this->perPage : null);
3531
}
3632

3733
#[On('comment:saved')]

tests/Livewire/CommentListTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<?php
22

3+
use Kirschbaum\Commentions\Comment as CommentModel;
34
use Kirschbaum\Commentions\Livewire\CommentList;
5+
use Kirschbaum\Commentions\RenderableComment;
46
use Mockery;
57
use Tests\Models\Post;
8+
use Tests\Models\User;
69

710
use function Pest\Livewire\livewire;
811

@@ -38,3 +41,62 @@
3841

3942
$component->get('comments');
4043
});
44+
45+
test('CommentList can render non-Comment renderable items', function () {
46+
/** @var Post|Mockery\MockInterface $post */
47+
$post = Mockery::mock(Post::class)->makePartial();
48+
49+
$items = collect([
50+
new RenderableComment(id: 1, authorName: 'System', body: 'System notice 1'),
51+
new RenderableComment(id: 2, authorName: 'Bot', body: 'Automated message'),
52+
]);
53+
54+
$post->shouldReceive('getComments')
55+
->once()
56+
->andReturn($items);
57+
58+
livewire(CommentList::class, [
59+
'record' => $post,
60+
'paginate' => false,
61+
])
62+
->assertSee('System')
63+
->assertSee('System notice 1')
64+
->assertSee('Bot')
65+
->assertSee('Automated message');
66+
});
67+
68+
test('CommentList can render both Comment and RenderableComment items', function () {
69+
/** @var User $user */
70+
$user = User::factory()->create();
71+
/** @var Post $realPost */
72+
$realPost = Post::factory()->create();
73+
74+
/** @var CommentModel $comment */
75+
$comment = CommentModel::factory()
76+
->author($user)
77+
->commentable($realPost)
78+
->create([
79+
'body' => 'Real comment body',
80+
]);
81+
82+
$renderable = new RenderableComment(id: 99, authorName: 'System', body: 'System message');
83+
84+
$items = collect([$comment, $renderable]);
85+
86+
/** @var Post|Mockery\MockInterface $post */
87+
$post = Mockery::mock(Post::class)->makePartial();
88+
$post->shouldReceive('getComments')
89+
->once()
90+
->andReturn($items);
91+
92+
livewire(CommentList::class, [
93+
'record' => $post,
94+
'paginate' => false,
95+
])
96+
// From Eloquent Comment
97+
->assertSee('Real comment body')
98+
->assertSee($user->name)
99+
// From RenderableComment
100+
->assertSee('System')
101+
->assertSee('System message');
102+
});

0 commit comments

Comments
 (0)