Close workshop sign-up after the closing date

Ruby · Rails · beginner · modification

Enforces the sign-up closing date that the workshop page already shows. `Workshop#signup_open?` (new, `app/models/workshop.rb`) compares today's date in the app's time zone with `signup_closes_on`. `SignupsController#create` (`app/controllers/signups_controller.rb`) checks it before creating the sign-up and answers 422 `sign-up closed` when sign-up is over (also for a member already signed up). The 401 and 404 paths are unchanged, and while sign-up is open so are the 201 and 409 paths. `test/models/workshop_test.rb` (new) adds Minitest model tests for `signup_open?` using `travel_to`.

Ruby 4.0, Rails 8.1 JSON API (`ApplicationController < ActionController::API`), SQLite in development and test, PostgreSQL 16 in production; nothing in this change depends on the database. Existing and unchanged: `config.time_zone = "Europe/Berlin"`, so `Date.current` is today's date in Berlin; `ApplicationController` includes the app's `Authentication` concern, whose `before_action :require_authentication` answers 401 when signed out and otherwise sets `Current.user`; `config/routes.rb` nests `resource :signup, only: :create` under `resources :workshops, only: []`. Every signed-in member may sign up for any workshop. The `workshops` table has `title`, `starts_on` and `signup_closes_on`, all `null: false`, and `signup_closes_on` is a `date` column set by the organizer form (unchanged). `signups` has a unique index on `[workshop_id, user_id]`; `Signup` belongs to `workshop` and `user`. The workshop page (shipped separately) shows the closing date and does not change here.

Requirements

Files touched

--- app/models/workshop.rb
 
   validates :title, :starts_on, :signup_closes_on, presence: true
+
+  def signup_open?
+    Date.current < signup_closes_on
+  end
 end
 
--- app/controllers/signups_controller.rb
   def create
     workshop = Workshop.find(params[:workshop_id])
+    unless workshop.signup_open?
+      render json: { error: "sign-up closed" }, status: :unprocessable_content
+      return
+    end
+
     signup = workshop.signups.create!(user: Current.user)
     render json: { workshop_id: workshop.id, signed_up_at: signup.created_at }, status: :created
--- test/models/workshop_test.rb
+require "test_helper"
+
+class WorkshopTest < ActiveSupport::TestCase
+  setup do
+    @workshop = Workshop.new(
+      title: "Intro to pottery",
+      starts_on: Date.new(2026, 11, 21),
+      signup_closes_on: Date.new(2026, 11, 14)
+    )
+  end

Review this PR

Ruby practice