From 5e7909e61695d002d45ce58fa010502c5eb8da51 Mon Sep 17 00:00:00 2001 From: David Gay Date: Mon, 30 Oct 2023 01:06:05 -0400 Subject: Game, Run, Checkpoint models --- app/models/checkpoint.rb | 4 +++ app/models/game.rb | 5 ++++ app/models/run.rb | 7 +++++ db/migrate/20231030045243_create_games.rb | 9 +++++++ db/migrate/20231030045347_create_runs.rb | 12 +++++++++ db/migrate/20231030045720_create_checkpoints.rb | 12 +++++++++ db/schema.rb | 34 ++++++++++++++++++++++++- test/fixtures/checkpoints.yml | 13 ++++++++++ test/fixtures/games.yml | 7 +++++ test/fixtures/runs.yml | 13 ++++++++++ test/models/checkpoint_test.rb | 7 +++++ test/models/game_test.rb | 7 +++++ test/models/run_test.rb | 7 +++++ 13 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 app/models/checkpoint.rb create mode 100644 app/models/game.rb create mode 100644 app/models/run.rb create mode 100644 db/migrate/20231030045243_create_games.rb create mode 100644 db/migrate/20231030045347_create_runs.rb create mode 100644 db/migrate/20231030045720_create_checkpoints.rb create mode 100644 test/fixtures/checkpoints.yml create mode 100644 test/fixtures/games.yml create mode 100644 test/fixtures/runs.yml create mode 100644 test/models/checkpoint_test.rb create mode 100644 test/models/game_test.rb create mode 100644 test/models/run_test.rb diff --git a/app/models/checkpoint.rb b/app/models/checkpoint.rb new file mode 100644 index 0000000..2a89128 --- /dev/null +++ b/app/models/checkpoint.rb @@ -0,0 +1,4 @@ +class Checkpoint < ApplicationRecord + belongs_to :run + belongs_to :user +end diff --git a/app/models/game.rb b/app/models/game.rb new file mode 100644 index 0000000..bdf72ba --- /dev/null +++ b/app/models/game.rb @@ -0,0 +1,5 @@ +class Game < ApplicationRecord + has_many :runs, dependent: :restrict_with_error + + validate :presence, [:title] +end diff --git a/app/models/run.rb b/app/models/run.rb new file mode 100644 index 0000000..91090b4 --- /dev/null +++ b/app/models/run.rb @@ -0,0 +1,7 @@ +class Run < ApplicationRecord + belongs_to :game + belongs_to :user + has_many :checkpoints, dependent: :destroy + + validate :presence, [:title] +end diff --git a/db/migrate/20231030045243_create_games.rb b/db/migrate/20231030045243_create_games.rb new file mode 100644 index 0000000..a46c0cc --- /dev/null +++ b/db/migrate/20231030045243_create_games.rb @@ -0,0 +1,9 @@ +class CreateGames < ActiveRecord::Migration[7.1] + def change + create_table :games do |t| + t.string :title, null: false + + t.timestamps + end + end +end diff --git a/db/migrate/20231030045347_create_runs.rb b/db/migrate/20231030045347_create_runs.rb new file mode 100644 index 0000000..a0aaba1 --- /dev/null +++ b/db/migrate/20231030045347_create_runs.rb @@ -0,0 +1,12 @@ +class CreateRuns < ActiveRecord::Migration[7.1] + def change + create_table :runs do |t| + t.string :title, null: false + t.text :description + t.references :game, null: false, foreign_key: true + t.references :user, null: false, foreign_key: true + + t.timestamps + end + end +end diff --git a/db/migrate/20231030045720_create_checkpoints.rb b/db/migrate/20231030045720_create_checkpoints.rb new file mode 100644 index 0000000..21ec35c --- /dev/null +++ b/db/migrate/20231030045720_create_checkpoints.rb @@ -0,0 +1,12 @@ +class CreateCheckpoints < ActiveRecord::Migration[7.1] + def change + create_table :checkpoints do |t| + t.string :title + t.text :description + t.references :run, null: false, foreign_key: true + t.references :user, null: false, foreign_key: true + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 5f2f919..af7e6a0 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,10 +10,38 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2023_10_30_040016) do +ActiveRecord::Schema[7.1].define(version: 2023_10_30_045720) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" + create_table "checkpoints", force: :cascade do |t| + t.string "title" + t.text "description" + t.bigint "run_id", null: false + t.bigint "user_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["run_id"], name: "index_checkpoints_on_run_id" + t.index ["user_id"], name: "index_checkpoints_on_user_id" + end + + create_table "games", force: :cascade do |t| + t.string "title", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "runs", force: :cascade do |t| + t.string "title", null: false + t.text "description" + t.bigint "game_id", null: false + t.bigint "user_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["game_id"], name: "index_runs_on_game_id" + t.index ["user_id"], name: "index_runs_on_user_id" + end + create_table "users", force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false @@ -23,4 +51,8 @@ ActiveRecord::Schema[7.1].define(version: 2023_10_30_040016) do t.index ["email"], name: "index_users_on_email", unique: true end + add_foreign_key "checkpoints", "runs" + add_foreign_key "checkpoints", "users" + add_foreign_key "runs", "games" + add_foreign_key "runs", "users" end diff --git a/test/fixtures/checkpoints.yml b/test/fixtures/checkpoints.yml new file mode 100644 index 0000000..bb83377 --- /dev/null +++ b/test/fixtures/checkpoints.yml @@ -0,0 +1,13 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + title: MyString + description: MyText + run: one + user: one + +two: + title: MyString + description: MyText + run: two + user: two diff --git a/test/fixtures/games.yml b/test/fixtures/games.yml new file mode 100644 index 0000000..64d88ef --- /dev/null +++ b/test/fixtures/games.yml @@ -0,0 +1,7 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + title: MyString + +two: + title: MyString diff --git a/test/fixtures/runs.yml b/test/fixtures/runs.yml new file mode 100644 index 0000000..df37963 --- /dev/null +++ b/test/fixtures/runs.yml @@ -0,0 +1,13 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + title: MyString + description: MyText + game: one + user: one + +two: + title: MyString + description: MyText + game: two + user: two diff --git a/test/models/checkpoint_test.rb b/test/models/checkpoint_test.rb new file mode 100644 index 0000000..e3f9598 --- /dev/null +++ b/test/models/checkpoint_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class CheckpointTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/models/game_test.rb b/test/models/game_test.rb new file mode 100644 index 0000000..6628fae --- /dev/null +++ b/test/models/game_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class GameTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/models/run_test.rb b/test/models/run_test.rb new file mode 100644 index 0000000..52be8e2 --- /dev/null +++ b/test/models/run_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class RunTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end -- cgit v1.2.3