From a5199f46baefc7647b897f78024520e71c5bf905 Mon Sep 17 00:00:00 2001 From: Dennis Ostendorf Date: Fri, 4 Aug 2023 15:14:43 +0200 Subject: [PATCH 1/4] feat: introduce games concept --- app/models/game.rb | 5 +++++ db/migrate/20230804130953_create_games.rb | 9 +++++++++ db/schema.rb | 8 +++++++- test/fixtures/games.yml | 5 +++++ test/models/game_test.rb | 9 +++++++++ 5 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 app/models/game.rb create mode 100644 db/migrate/20230804130953_create_games.rb create mode 100644 test/fixtures/games.yml create mode 100644 test/models/game_test.rb diff --git a/app/models/game.rb b/app/models/game.rb new file mode 100644 index 0000000..8920a7b --- /dev/null +++ b/app/models/game.rb @@ -0,0 +1,5 @@ +class Game < ApplicationRecord + has_many :dice + + validates :title, presence: true +end diff --git a/db/migrate/20230804130953_create_games.rb b/db/migrate/20230804130953_create_games.rb new file mode 100644 index 0000000..a29bfcc --- /dev/null +++ b/db/migrate/20230804130953_create_games.rb @@ -0,0 +1,9 @@ +class CreateGames < ActiveRecord::Migration[7.0] + def change + create_table :games do |t| + t.string :title, null: false + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 5118664..27e9a48 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2023_06_27_115856) do +ActiveRecord::Schema[7.0].define(version: 2023_08_04_130953) do create_table "dice", force: :cascade do |t| t.datetime "created_at", null: false t.datetime "updated_at", null: false @@ -19,6 +19,12 @@ t.index ["shortcode"], name: "index_dice_on_shortcode", unique: true 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 "ideas", force: :cascade do |t| t.string "description", null: false t.datetime "created_at", null: false diff --git a/test/fixtures/games.yml b/test/fixtures/games.yml new file mode 100644 index 0000000..295372a --- /dev/null +++ b/test/fixtures/games.yml @@ -0,0 +1,5 @@ +summer: + title: "My summer dice game" + +autumn: + title: "My autumn dice game" diff --git a/test/models/game_test.rb b/test/models/game_test.rb new file mode 100644 index 0000000..bac8613 --- /dev/null +++ b/test/models/game_test.rb @@ -0,0 +1,9 @@ +require "test_helper" + +class GameTest < ActiveSupport::TestCase + test "accepts game with valid attributes" do + game = Game.new title: "My game" + assert game.valid? + assert_empty game.errors + end +end From 4f4efaf5980cdefc3f9b4e2b6d43527317e170ef Mon Sep 17 00:00:00 2001 From: Dennis Ostendorf Date: Fri, 4 Aug 2023 15:20:06 +0200 Subject: [PATCH 2/4] feat: associate dice with a game For now, this is optional, because we need to migrate dice that do not yet belong to a game. --- app/models/die.rb | 1 + app/models/game.rb | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/app/models/die.rb b/app/models/die.rb index 483f5ab..f05cbbc 100644 --- a/app/models/die.rb +++ b/app/models/die.rb @@ -1,4 +1,5 @@ class Die < ApplicationRecord + belongs_to :game, optional: true has_many :sides, dependent: :destroy has_many :rolls, through: :sides diff --git a/app/models/game.rb b/app/models/game.rb index 8920a7b..5973f85 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -1,5 +1,5 @@ class Game < ApplicationRecord - has_many :dice + has_many :dice, dependent: :destroy validates :title, presence: true end From 5605a05072ae51411ef8e976f8fadb820fb45fc1 Mon Sep 17 00:00:00 2001 From: Dennis Ostendorf Date: Fri, 4 Aug 2023 15:31:32 +0200 Subject: [PATCH 3/4] feat: add games association in database For now optional as well. --- db/migrate/20230804132630_add_game_to_dice.rb | 5 +++++ db/schema.rb | 5 ++++- 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20230804132630_add_game_to_dice.rb diff --git a/db/migrate/20230804132630_add_game_to_dice.rb b/db/migrate/20230804132630_add_game_to_dice.rb new file mode 100644 index 0000000..5bdffeb --- /dev/null +++ b/db/migrate/20230804132630_add_game_to_dice.rb @@ -0,0 +1,5 @@ +class AddGameToDice < ActiveRecord::Migration[7.0] + def change + add_reference :dice, :game, null: true, foreign_key: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 27e9a48..e3a6902 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,12 +10,14 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2023_08_04_130953) do +ActiveRecord::Schema[7.0].define(version: 2023_08_04_132630) do create_table "dice", force: :cascade do |t| t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "shortcode", null: false t.integer "title" + t.integer "game_id" + t.index ["game_id"], name: "index_dice_on_game_id" t.index ["shortcode"], name: "index_dice_on_shortcode", unique: true end @@ -54,6 +56,7 @@ t.index ["shortcode", "die_id"], name: "index_sides_on_shortcode_and_die_id", unique: true end + add_foreign_key "dice", "games" add_foreign_key "rolls", "sides" add_foreign_key "sides", "dice" end From 7ed5ecfa901b80d5abe06b2c88914104b21e7ea2 Mon Sep 17 00:00:00 2001 From: Dennis Ostendorf Date: Fri, 1 Sep 2023 10:48:33 +0200 Subject: [PATCH 4/4] fix: allow null values for game_id on dice --- db/migrate/20230901084121_remove_foreign_key_from_dice.rb | 7 +++++++ db/migrate/20230901084435_make_game_nullable_on_dice.rb | 5 +++++ db/schema.rb | 6 ++---- 3 files changed, 14 insertions(+), 4 deletions(-) create mode 100644 db/migrate/20230901084121_remove_foreign_key_from_dice.rb create mode 100644 db/migrate/20230901084435_make_game_nullable_on_dice.rb diff --git a/db/migrate/20230901084121_remove_foreign_key_from_dice.rb b/db/migrate/20230901084121_remove_foreign_key_from_dice.rb new file mode 100644 index 0000000..7ab278e --- /dev/null +++ b/db/migrate/20230901084121_remove_foreign_key_from_dice.rb @@ -0,0 +1,7 @@ +class RemoveForeignKeyFromDice < ActiveRecord::Migration[7.0] + def change + if foreign_key_exists?(:dice, :games) + remove_foreign_key :dice, :games + end + end +end diff --git a/db/migrate/20230901084435_make_game_nullable_on_dice.rb b/db/migrate/20230901084435_make_game_nullable_on_dice.rb new file mode 100644 index 0000000..0b33229 --- /dev/null +++ b/db/migrate/20230901084435_make_game_nullable_on_dice.rb @@ -0,0 +1,5 @@ +class MakeGameNullableOnDice < ActiveRecord::Migration[7.0] + def change + change_column_null(:dice, :game_id, true) + end +end diff --git a/db/schema.rb b/db/schema.rb index e3a6902..338062f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,15 +10,14 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2023_08_04_132630) do +ActiveRecord::Schema[7.0].define(version: 2023_09_01_084435) do create_table "dice", force: :cascade do |t| t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "shortcode", null: false t.integer "title" t.integer "game_id" - t.index ["game_id"], name: "index_dice_on_game_id" - t.index ["shortcode"], name: "index_dice_on_shortcode", unique: true + t.index ["shortcode", "game_id"], name: "index_dice_on_shortcode_and_game_id", unique: true end create_table "games", force: :cascade do |t| @@ -56,7 +55,6 @@ t.index ["shortcode", "die_id"], name: "index_sides_on_shortcode_and_die_id", unique: true end - add_foreign_key "dice", "games" add_foreign_key "rolls", "sides" add_foreign_key "sides", "dice" end