Forum members pick a public username

Ruby · Rails · intermediate · modification

First step towards public profiles: members can pick a username, shown next to their posts. The migration adds a nullable `username` column to `members` with a unique index. `Member` trims the value (an empty one becomes `nil`) and validates the format and uniqueness, leaving members without a username alone. `UsernamesController#update` (new, `PATCH /username`, routed as a singular resource) saves the signed-in member's username through strong params and answers JSON: the saved value, or the validation errors with 422. `usernames_controller_test.rb` (new) adds integration tests for the endpoint. Profile pages come in the next PR.

Cooking community forum on Ruby 4.0 and Rails 8.1, SQLite in development, test and production (one database file on a persistent volume), Minitest integration tests. Existing and unchanged (not shown): `ApplicationController` includes the `Authentication` concern, whose `before_action :require_authentication` sets `Current.member` to the signed-in `Member` or redirects to `new_session_path`; `Current` is an `ActiveSupport::CurrentAttributes` with `member`. `Member` has `name` (required, at most 80 characters) and `email` (normalized with `strip.downcase`, unique by validation and a unique index); the table has about 40,000 rows, none with a username yet. The forum's pages call `PATCH /username` with `fetch` and send the `X-CSRF-Token` header from `csrf_meta_tags`; forgery protection is on outside tests. Two members claiming the same username at the same instant is out of scope. This PR and its migration have not been deployed yet. `db/schema.rb` is regenerated from the migration and not shown. Profile pages (`/@username`, next PR) will look a username up regardless of letter case, per requirement 2. The test helper `sign_in_as(member)` signs the integration session in as that member.

Requirements

Files touched

Review this PR

Ruby practice