From 4c9e6ae08ebd5def894a9fd7eb3716bc8fcca470 Mon Sep 17 00:00:00 2001 From: Derek Yuan <87529592+derekyuan1000@users.noreply.github.com> Date: Tue, 13 Oct 2026 22:49:24 +0100 Subject: [PATCH 1/2] feat: Add Event model with validations, scopes and seed data Co-Authored-By: Claude Opus 5.5 --- app/models/event.rb | 16 ++++++++++ db/migrate/20261013214731_create_events.rb | 15 +++++++++ db/schema.rb | 14 +++++++- db/seeds.rb | 37 ++++++++++++++++++++++ test/fixtures/events.yml | 23 ++++++++++++++ test/models/event_test.rb | 33 +++++++++++++++++++ 6 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 app/models/event.rb create mode 100644 db/migrate/20261013214731_create_events.rb create mode 100644 test/fixtures/events.yml create mode 100644 test/models/event_test.rb diff --git a/app/models/event.rb b/app/models/event.rb new file mode 100644 index 0000000..24b19cb --- /dev/null +++ b/app/models/event.rb @@ -0,0 +1,16 @@ +class Event < ApplicationRecord + scope :upcoming, -> { where(starts_at: Time.current..).order(:starts_at) } + scope :past, -> { where(starts_at: ...Time.current).order(starts_at: :desc) } + + validates :title, presence: true + validates :starts_at, presence: true + validate :ends_after_start + + private + + def ends_after_start + return if ends_at.blank? || starts_at.blank? + + errors.add(:ends_at, "must be after the start time") if ends_at <= starts_at + end +end diff --git a/db/migrate/20261013214731_create_events.rb b/db/migrate/20261013214731_create_events.rb new file mode 100644 index 0000000..23c7968 --- /dev/null +++ b/db/migrate/20261013214731_create_events.rb @@ -0,0 +1,15 @@ +class CreateEvents < ActiveRecord::Migration[8.1] + def change + create_table :events do |t| + t.string :title, null: false + t.text :description + t.datetime :starts_at, null: false + t.datetime :ends_at + t.string :location + t.boolean :virtual, null: false, default: false + + t.timestamps + end + add_index :events, :starts_at + end +end diff --git a/db/schema.rb b/db/schema.rb index 5414b0e..072f897 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,11 +10,23 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.1].define(version: 2026_10_09_180202) do +ActiveRecord::Schema[8.1].define(version: 2026_10_13_214731) do # These are extensions that must be enabled in order to support this database enable_extension "citext" enable_extension "pg_catalog.plpgsql" + create_table "events", force: :cascade do |t| + t.string "title", null: false + t.text "description" + t.datetime "starts_at", null: false + t.datetime "ends_at" + t.string "location" + t.boolean "virtual", default: false, null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["starts_at"], name: "index_events_on_starts_at" + end + create_table "users", force: :cascade do |t| t.citext "email", null: false t.citext "username", null: false diff --git a/db/seeds.rb b/db/seeds.rb index 4fbd6ed..f846ff5 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -7,3 +7,40 @@ # ["Action", "Comedy", "Drama", "Horror"].each do |genre_name| # MovieGenre.find_or_create_by!(name: genre_name) # end + +# Sample events, relative to today so there are always some upcoming ones. +# Keyed by title so re-running the seeds doesn't create duplicates. +today = Time.current.beginning_of_day + +[ + { title: "Rails Study Group", days: 3, hour: 18, length: 2, location: "Online", virtual: true, + description: "Weekly session working through the Rails guides together. Bring questions." }, + { title: "Board Game Night", days: 5, hour: 19, length: 3, location: "The Draughts Café, London", virtual: false, + description: "Casual games night. Beginners welcome, plenty of games provided." }, + { title: "Intro to Hotwire", days: 8, hour: 17, length: 1, location: "Online", virtual: true, + description: "A short talk on Turbo and Stimulus, followed by Q&A." }, + { title: "Park Run Meetup", days: 10, hour: 9, length: 1, location: "Hyde Park, London", virtual: false, + description: "5k at your own pace, coffee afterwards." }, + { title: "Design Critique Session", days: 14, hour: 18, length: 2, location: "Online", virtual: true, + description: "Share something you're working on and get friendly feedback." }, + { title: "Hackathon Weekend", days: 21, hour: 10, length: 8, location: "Imperial College, London", virtual: false, + description: "Build something in a weekend. Teams of up to four." }, + { title: "Photography Walk", days: 28, hour: 14, length: 3, location: "South Bank, London", virtual: false, + description: "Explore the river with other photographers. Any camera is fine." }, + { title: "Open Source Sprint", days: 35, hour: 11, length: 5, location: "Online", virtual: true, + description: "Pick an issue and make your first contribution, with mentors on hand." }, + { title: "Welcome Social", days: -7, hour: 19, length: 2, location: "Student Union Bar", virtual: false, + description: "Kick-off social for new members." }, + { title: "Git Basics Workshop", days: -3, hour: 17, length: 2, location: "Online", virtual: true, + description: "Branches, commits and pull requests from scratch." } +].each do |attrs| + starts_at = today + attrs[:days].days + attrs[:hour].hours + + Event.find_or_create_by!(title: attrs[:title]) do |event| + event.description = attrs[:description] + event.starts_at = starts_at + event.ends_at = starts_at + attrs[:length].hours + event.location = attrs[:location] + event.virtual = attrs[:virtual] + end +end diff --git a/test/fixtures/events.yml b/test/fixtures/events.yml new file mode 100644 index 0000000..8044d39 --- /dev/null +++ b/test/fixtures/events.yml @@ -0,0 +1,23 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +upcoming_soon: + title: Upcoming soon + description: Starts tomorrow. + starts_at: <%= 1.day.from_now %> + ends_at: <%= 1.day.from_now + 2.hours %> + location: London + virtual: false + +upcoming_later: + title: Upcoming later + description: Starts next week. + starts_at: <%= 1.week.from_now %> + virtual: true + +past: + title: Already happened + description: Was last week. + starts_at: <%= 1.week.ago %> + ends_at: <%= 1.week.ago + 1.hour %> + location: Manchester + virtual: false diff --git a/test/models/event_test.rb b/test/models/event_test.rb new file mode 100644 index 0000000..8b4a6f1 --- /dev/null +++ b/test/models/event_test.rb @@ -0,0 +1,33 @@ +require "test_helper" + +class EventTest < ActiveSupport::TestCase + test "valid with title and starts_at" do + assert Event.new(title: "Meetup", starts_at: 1.day.from_now).valid? + end + + test "requires title and starts_at" do + event = Event.new + assert_not event.valid? + assert event.errors.of_kind?(:title, :blank) + assert event.errors.of_kind?(:starts_at, :blank) + end + + test "ends_at must be after starts_at" do + start = 1.day.from_now + assert_not Event.new(title: "Meetup", starts_at: start, ends_at: start).valid? + assert_not Event.new(title: "Meetup", starts_at: start, ends_at: start - 1.hour).valid? + assert Event.new(title: "Meetup", starts_at: start, ends_at: start + 1.hour).valid? + end + + test "virtual defaults to false" do + assert_equal false, Event.new.virtual + end + + test "upcoming returns future events soonest first" do + assert_equal [ events(:upcoming_soon), events(:upcoming_later) ], Event.upcoming.to_a + end + + test "past returns past events most recent first" do + assert_equal [ events(:past) ], Event.past.to_a + end +end From 86feb34dc7d3ccce344aed04deb250db56b50eba Mon Sep 17 00:00:00 2001 From: Derek Yuan <87529592+derekyuan1000@users.noreply.github.com> Date: Tue, 13 Oct 2026 22:54:55 +0100 Subject: [PATCH 2/2] Rename seed events to Event 1-10 --- db/seeds.rb | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/db/seeds.rb b/db/seeds.rb index f846ff5..7d02064 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -9,34 +9,34 @@ # end # Sample events, relative to today so there are always some upcoming ones. -# Keyed by title so re-running the seeds doesn't create duplicates. +# Named "Event 1", "Event 2", ... and keyed by title so re-running the seeds doesn't create duplicates. today = Time.current.beginning_of_day [ - { title: "Rails Study Group", days: 3, hour: 18, length: 2, location: "Online", virtual: true, + { days: 3, hour: 18, length: 2, location: "Online", virtual: true, description: "Weekly session working through the Rails guides together. Bring questions." }, - { title: "Board Game Night", days: 5, hour: 19, length: 3, location: "The Draughts Café, London", virtual: false, + { days: 5, hour: 19, length: 3, location: "The Draughts Café, London", virtual: false, description: "Casual games night. Beginners welcome, plenty of games provided." }, - { title: "Intro to Hotwire", days: 8, hour: 17, length: 1, location: "Online", virtual: true, + { days: 8, hour: 17, length: 1, location: "Online", virtual: true, description: "A short talk on Turbo and Stimulus, followed by Q&A." }, - { title: "Park Run Meetup", days: 10, hour: 9, length: 1, location: "Hyde Park, London", virtual: false, + { days: 10, hour: 9, length: 1, location: "Hyde Park, London", virtual: false, description: "5k at your own pace, coffee afterwards." }, - { title: "Design Critique Session", days: 14, hour: 18, length: 2, location: "Online", virtual: true, + { days: 14, hour: 18, length: 2, location: "Online", virtual: true, description: "Share something you're working on and get friendly feedback." }, - { title: "Hackathon Weekend", days: 21, hour: 10, length: 8, location: "Imperial College, London", virtual: false, + { days: 21, hour: 10, length: 8, location: "Imperial College, London", virtual: false, description: "Build something in a weekend. Teams of up to four." }, - { title: "Photography Walk", days: 28, hour: 14, length: 3, location: "South Bank, London", virtual: false, + { days: 28, hour: 14, length: 3, location: "South Bank, London", virtual: false, description: "Explore the river with other photographers. Any camera is fine." }, - { title: "Open Source Sprint", days: 35, hour: 11, length: 5, location: "Online", virtual: true, + { days: 35, hour: 11, length: 5, location: "Online", virtual: true, description: "Pick an issue and make your first contribution, with mentors on hand." }, - { title: "Welcome Social", days: -7, hour: 19, length: 2, location: "Student Union Bar", virtual: false, + { days: -7, hour: 19, length: 2, location: "Student Union Bar", virtual: false, description: "Kick-off social for new members." }, - { title: "Git Basics Workshop", days: -3, hour: 17, length: 2, location: "Online", virtual: true, + { days: -3, hour: 17, length: 2, location: "Online", virtual: true, description: "Branches, commits and pull requests from scratch." } -].each do |attrs| +].each.with_index(1) do |attrs, i| starts_at = today + attrs[:days].days + attrs[:hour].hours - Event.find_or_create_by!(title: attrs[:title]) do |event| + Event.find_or_create_by!(title: "Event #{i}") do |event| event.description = attrs[:description] event.starts_at = starts_at event.ends_at = starts_at + attrs[:length].hours