-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add GameTrasactions (Create/Start/End)
This allows us to track which User: - created a Game - started a Game, - made the final winning/losing move to end a Game. To support this, we now have to pass a User object into the methods that create, start, and end Games. Note: For now, I'm keeping the Game#started_at and Game#ended_at attributes. Though, I may remove these soon since they're redundant at this point. Also, fix an inconsistency in the test fixtures where the Game with Status "Standing By" had a `started_at` value. Also, fix ConsoleObjectBehaviors mix-in to not try to include has_many associations as automatic Console objects. Because this gets in the way of operations performed on the collection, such as `clear`.
- Loading branch information
Paul DobbinSchmaltz
committed
Nov 14, 2024
1 parent
cb36ea7
commit 222444e
Showing
18 changed files
with
360 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
# GameCreateTransaction is a {GameTransaction} marking which {User} created the | ||
# associated {Game}. | ||
class GameCreateTransaction < GameTransaction | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
# GameEndTransaction is a {GameTransaction} marking which {User} made the final | ||
# move during the associated {Game}. | ||
class GameEndTransaction < GameTransaction | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
# GameStartTransaction is a {GameTransaction} marking which {User} started the | ||
# associated {Game}. | ||
class GameStartTransaction < GameTransaction | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# frozen_string_literal: true | ||
|
||
# GameTransaction records events transacted by {User}s on a {Game} and when they | ||
# occurred. | ||
# | ||
# @attr type [String] The Subclass name. | ||
# @attr user_id [Integer] References the {User} involved in this Transaction. | ||
# @attr game_id [Integer] References the {Game} involved in this Transaction. | ||
# @attr created_at [DateTime] When this Transaction occurred. | ||
class GameTransaction < ApplicationRecord | ||
self.implicit_order_column = "created_at" | ||
|
||
include AbstractBaseClassBehaviors | ||
include ConsoleBehaviors | ||
|
||
as_abstract_class | ||
|
||
belongs_to :user | ||
belongs_to :game | ||
|
||
scope :for_user, ->(user) { where(user:) } | ||
scope :for_game, ->(game) { where(game:) } | ||
|
||
validates :game, uniqueness: { scope: :type } | ||
|
||
def self.create_between(user:, game:) | ||
new(user:, game:).tap(&:save!) | ||
end | ||
|
||
def self.exists_between?(user:, game:) | ||
for_user(user).for_game(game).exists? | ||
end | ||
|
||
# GameTransaction::Console acts like a {GameTransaction} but otherwise handles | ||
# IRB Console-specific methods/logic. | ||
class Console | ||
include ConsoleObjectBehaviors | ||
|
||
private | ||
|
||
def inspect_info | ||
[ | ||
[user.inspect, game.inspect].join(" -> "), | ||
I18n.l(created_at, format: :debug), | ||
].join(" @ ") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
# Version: 20241112041937 | ||
class CreateGameTransactions < ActiveRecord::Migration[8.0] | ||
def change | ||
create_table(:game_transactions) do |t| | ||
t.string(:type, null: false, index: true) | ||
t.references( | ||
:user, type: :uuid, foreign_key: { on_delete: :nullify }, index: true) | ||
t.references( | ||
:game, null: false, foreign_key: { on_delete: :cascade }) | ||
|
||
t.datetime(:created_at, null: false, index: true) | ||
end | ||
|
||
add_index(:game_transactions, %i[game_id type], unique: true) | ||
end | ||
end |
Oops, something went wrong.