Skip to content

Add an opt-in star rating for comments#111

Open
ItsMalikJones wants to merge 3 commits into
mainfrom
feat/29-comment-ratings
Open

Add an opt-in star rating for comments#111
ItsMalikJones wants to merge 3 commits into
mainfrom
feat/29-comment-ratings

Conversation

@ItsMalikJones
Copy link
Copy Markdown
Contributor

Closes #29

Summary

Lets commenters attach a star rating to a comment, review-style. The feature is disabled by default and is fully opt-in — existing installs see no behavioral change until they enable it.

Ratings can be turned on globally via config, or per-component on CommentsEntry, CommentsAction, and CommentsTableAction, with a configurable maximum.

What's included

  • Storage — an additive migration adds a nullable rating column (unsignedTinyInteger) to the comments table. The Comment model adds rating to $fillable and casts it to an integer. getCacheKey() now incorporates the rating so polling correctly detects rating changes.
  • Configuration — a new commentions.ratings config block (enabled, max), backed by COMMENTIONS_RATINGS_ENABLED / COMMENTIONS_RATINGS_MAX env vars and exposed through Config::ratingsAreEnabled() / Config::getMaxRating().
  • Per-component overrides — a HasRatings concern adds enableRatings(bool|Closure), disableRatings(), and maxRating(int|Closure). Component settings override the global config and thread through to the Livewire layer.
  • UI — when enabled, the comment editor shows an interactive star input (create and edit); each rated comment renders its score as filled/empty stars. The rating is validated (nullable|integer|min:1|max:N) on both create and edit, and persisted via SaveComment.
  • Accessibility & i18n — the input uses role="group" with per-star aria-pressed/aria-label, the display uses role="img" with an aria-label, and star labels use trans_choice pluralization. New translation keys added to resources/lang/en/comments.php.

Usage

Enable globally in config/commentions.php:

'ratings' => [
    'enabled' => env('COMMENTIONS_RATINGS_ENABLED', false),
    'max'     => (int) env('COMMENTIONS_RATINGS_MAX', 5),
],

Or per component (overrides the global config):

CommentsEntry::make('comments')
    ->enableRatings()
    ->maxRating(10);

After upgrading, publish and run the new migration:

php artisan vendor:publish --tag="commentions-migrations"
php artisan migrate

Testing

  • Added tests/Livewire/CommentRatingTest.php (15 tests, 36 assertions) covering: attaching a rating, ignoring it when disabled, min/max bounds rejection, per-component enable/disable and maxRating overrides (including disabling when globally enabled), star rendering against per-component max, and editing a rating round-trip.
  • Registered the new migration stub in the test harness.

Lets a user attach a star rating to a comment, review-style.

- New nullable `rating` column via an additive migration; the Comment model
  casts it to an integer.
- Opt-in: a `commentions.ratings` config block (disabled by default) plus
  CommentsEntry::make()->enableRatings() / ->maxRating(), threaded through
  the entry and actions to the Comments component.
- The Comments editor shows an interactive star input when enabled; the
  rating is validated (1..max) and persisted via SaveComment.
- Each comment renders its rating as filled/empty stars.
@ItsMalikJones ItsMalikJones self-assigned this Jun 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add rate to comments linked

1 participant