Team-shared saved replies for the support desk

Ruby · Rails · intermediate · modification

Lets support agents share saved replies with their team. A migration adds a `shared` boolean column to `saved_replies` (`NOT NULL`, default `false`), and `SavedReply` validates that it is `true` or `false`, so a `null` from the client is a 422 rather than a database error. In `Api::SavedRepliesController`, `index` now lists the agent's own replies together with the shared ones, newest first, and each item gains `shared` and `mine` so the ticket composer can label them; `shared` is added to the permitted parameters of `create` and `update`, which keep looking replies up as before. `saved_replies_controller_test.rb` extends the integration tests for sharing, unsharing and the `null` flag.

Ruby 4.0, Rails 8.1 JSON API (SQLite in development and test, PostgreSQL 16 in production; nothing here depends on the database: the query's `WHERE` clause is built by Active Record). `Api::BaseController` (existing, unchanged) inherits `ActionController::API`, reads the `Authorization: Bearer <token>` header with `authenticate_with_http_token`, answers 401 when no `User` has that `api_token`, and otherwise sets `Current.user` and `Current.account` (`ActiveSupport::CurrentAttributes`) to the agent and the agent's account. Every `User` belongs to exactly one `Account` and never moves. `Account has_many :saved_replies`; `User has_many :saved_replies` through `author_id`; `SavedReply belongs_to :account` and `belongs_to :author` (a `User`), with `title` (string) and `body` (text), all `NOT NULL` with foreign keys. A reply's account is always its author's account: `create` builds it on `Current.account` and neither action lets a client change `account_id` or `author_id`. An account has at most 300 saved replies, so the list is not paginated. Clients send `shared` as a JSON boolean. In production `ActiveRecord::RecordNotFound` gets Rails' default 404 response. Tests are Minitest integration tests with inline records (`auth_headers(user)` and `create_reply(author, title, **attrs)` are helpers in the existing test file). The ticket composer's UI ships separately.

Requirements

Files touched

Review this PR

Ruby practice