A notifications page next to workspace branding
Ruby · Rails · advanced · modification
Adds a notifications page to the workspace settings, next to the existing branding page. `config/routes.rb` adds a singular `resource :notifications` under the `settings` namespace. `Workspace` gains `digest_frequency` and `digest_day` as `store_accessor` keys on its `settings` JSON column, with inclusion validations. The admin check, the JSON rendering and the save-and-respond logic that `BrandingController` had inline move into a new `Settings::BaseController`; `BrandingController` and the new `NotificationsController` inherit from it and only declare their keys and strong params. `notifications_controller_test.rb` adds integration tests for reading and saving the digest settings, a save on each page leaving the other page's keys in place, validation, the missing-parameter 400 and the admin check.
Workspace app on Ruby 4.0 and Rails 8.1, SQLite in development, test and production (one database file; production runs Puma with several threads per process), Minitest integration tests. `settings` is a single `json` column on `workspaces` (`null: false`, default `{}`) and holds every page's keys; the app reads them only through the `store_accessor` methods. Existing and unchanged (not shown): `ApplicationController` includes the `Authentication` concern, whose `before_action :require_authentication` sets `Current.user` to the signed-in `User` or redirects to `new_session_path`; `Current` is an `ActiveSupport::CurrentAttributes` with `user`, and `Current.workspace` returns `Current.user.workspace`. A `User` belongs to one `Workspace` and has a string enum `role` (`member` / `admin`). The settings pages are part of the admin front end, which always nests the body under the page's key (`{"branding": {...}}`, `{"notifications": {...}}`) and sends JSON with string values and the CSRF token from `csrf_meta_tags`; forgery protection is on outside tests. The digest mailer (separate, unchanged) treats a `null` `digest_frequency` as `weekly` and a `null` `digest_day` as `monday`. The test helper `sign_in_as(user)` signs the integration session in.
Requirements
- `GET /settings/notifications` (new) returns the workspace's digest settings as JSON, `{"digest_frequency": ..., "digest_day": ...}`, with `null` for a key that was never saved. `PATCH /settings/notifications` (new) saves the submitted subset of those two keys and answers 200 with the same JSON; a key that is not submitted keeps its value. `digest_frequency` is one of `off`, `daily`, `weekly`; `digest_day` is a lowercase English weekday name.
- `GET` and `PATCH /settings/branding` (existing) keep their behaviour for `accent_color` (a `#rrggbb` hex colour) and `welcome_message` (at most 200 characters): same JSON shape, same validations.
- On either page, an invalid value answers 422 with `{"errors": {...}}` keyed by field and saves nothing; a JSON body whose `notifications` or `branding` object is missing or has none of its fields answers 400. Only workspace admins may use either page; a signed-in non-admin gets 403.
- A workspace can have several admins, and two of them can save the two pages at the same moment. Each save changes only its own page's keys: a branding save must never revert a digest key that a notifications save stored, and the other way round, however the two requests interleave. Two saves of the same page may overwrite each other (the later save of a key wins).
Files touched
- config/routes.rb
- app/models/workspace.rb
- app/controllers/settings/base_controller.rb
- app/controllers/settings/branding_controller.rb
- app/controllers/settings/notifications_controller.rb
- test/controllers/settings/notifications_controller_test.rb