Let volunteers sign up for food bank shifts in the app

Ruby · Rails · intermediate · modification

Volunteers can now pick a shift in the mobile app instead of emailing the coordinators. `config/routes.rb` nests a singular `signup` resource under `shifts`, so `POST /api/shifts/:shift_id/signup` reaches the new `Api::SignupsController#create`. The rules live in a new `Shift#sign_up!`, which raises `Shift::Closed`, `Shift::AlreadySignedUp` or `Shift::Full` and otherwise creates the sign-up; the controller turns each of those into its 409 body, and also answers "already signed up" if the unique index on sign-ups rejects the insert. `shift_signups_test.rb` adds integration tests for the endpoint's responses.

Ruby 4.0, Rails 8.1 (`config.load_defaults 8.1`, default UTC time zone, so `starts_at` is a UTC instant and encodes as ISO 8601 in UTC). SQLite in development, test and production: the app runs the Rails 8 default SQLite setup with `timeout: 5000` in `database.yml`, on one server, as Puma with 2 worker processes of 3 threads each, all on the same database file. Tests are Minitest integration tests (`ActionDispatch::IntegrationTest`). `Api::BaseController` (existing, unchanged) is an `ActionController::API` (no CSRF, no cookies): a `before_action` reads the `Authorization: Bearer <token>` header, sets `Current.user` to the `User` with that `api_token` and answers 401 otherwise. `Current` is an `ActiveSupport::CurrentAttributes` with `attribute :user`. The schema comes from earlier migrations of this feature: `shifts` (`title`, `starts_at` and `ends_at` datetimes, `spots` integer NOT NULL with a check constraint 1 to 30), `signups` (`shift_id` and `user_id`, both NOT NULL with foreign keys, a unique index on `[shift_id, user_id]`), and `users` (`name`, `api_token` from `has_secure_token`). `Signup` (`belongs_to :shift`, `belongs_to :user`) and `User` (`has_many :signups`) are unchanged. The food bank has one location and every volunteer may join any shift. Coordinators publish shifts in the admin tool and do not change `spots` or delete a shift after publishing; they can remove a sign-up there, and this endpoint is the only code that creates sign-ups. Next month's shifts are published on the last Tuesday of the month at 18:00, and the Saturday shifts before the holidays usually fill within minutes. `ActiveRecord::RecordNotFound` answers 404 (Rails' default exception mapping). The app screen ships separately.

Requirements

Files touched

Review this PR

Ruby practice