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 new file mode 100644 index 0000000..5973f85 --- /dev/null +++ b/app/models/game.rb @@ -0,0 +1,5 @@ +class Game < ApplicationRecord + has_many :dice, dependent: :destroy + + 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/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/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 5118664..338062f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,13 +10,20 @@ # # 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_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.index ["shortcode"], name: "index_dice_on_shortcode", unique: true + t.integer "game_id" + t.index ["shortcode", "game_id"], name: "index_dice_on_shortcode_and_game_id", 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| 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