Skip to content
This repository has been archived by the owner on Jun 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #53 from technologiestiftung/feat/games-with-dice-…
Browse files Browse the repository at this point in the history
…optional

feat: scope dice to game (for now optionally)
  • Loading branch information
dnsos authored Sep 1, 2023
2 parents 5d0f30f + 7ed5ecf commit fcb7406
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/models/die.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Die < ApplicationRecord
belongs_to :game, optional: true
has_many :sides, dependent: :destroy
has_many :rolls, through: :sides

Expand Down
5 changes: 5 additions & 0 deletions app/models/game.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Game < ApplicationRecord
has_many :dice, dependent: :destroy

validates :title, presence: true
end
9 changes: 9 additions & 0 deletions db/migrate/20230804130953_create_games.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions db/migrate/20230804132630_add_game_to_dice.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddGameToDice < ActiveRecord::Migration[7.0]
def change
add_reference :dice, :game, null: true, foreign_key: true
end
end
7 changes: 7 additions & 0 deletions db/migrate/20230901084121_remove_foreign_key_from_dice.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions db/migrate/20230901084435_make_game_nullable_on_dice.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class MakeGameNullableOnDice < ActiveRecord::Migration[7.0]
def change
change_column_null(:dice, :game_id, true)
end
end
11 changes: 9 additions & 2 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions test/fixtures/games.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
summer:
title: "My summer dice game"

autumn:
title: "My autumn dice game"
9 changes: 9 additions & 0 deletions test/models/game_test.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit fcb7406

Please sign in to comment.