-
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.
- Loading branch information
Paul DobbinSchmaltz
committed
Nov 15, 2024
1 parent
2c5ae22
commit 77a7fe1
Showing
5 changed files
with
164 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# frozen_string_literal: true | ||
|
||
# UserUpdateTransaction records update events on a {User}'s "profile" record, | ||
# and when they occurred. Currently, this just includes updates to | ||
# {User#username}, but can dynamically expand by using | ||
# {UserUpdateTransaction#change_set}. | ||
# | ||
# @attr user_id [Integer] References the {User} involved in this Transaction. | ||
# @attr change_set [Hash] A Hash of old/new values for changes to this record. | ||
# @attr created_at [DateTime] When this Transaction occurred. | ||
class UserUpdateTransaction < ApplicationRecord | ||
self.implicit_order_column = "created_at" | ||
|
||
include ConsoleBehaviors | ||
|
||
belongs_to :user | ||
|
||
scope :for_user, ->(user) { where(user:) } | ||
|
||
validates :change_set, presence: true | ||
|
||
# :reek:UnusedParams | ||
def self.create_for(user:, change_set:) | ||
user.user_update_transactions.create!(change_set:) | ||
end | ||
|
||
# UserUpdateTransaction::Console acts like a {UserUpdateTransaction} but | ||
# otherwise handles IRB Console-specific methods/logic. | ||
class Console | ||
include ConsoleObjectBehaviors | ||
|
||
private | ||
|
||
def inspect_info | ||
[ | ||
[user.inspect, change_set.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
14 changes: 14 additions & 0 deletions
14
db/migrate/20241115023724_create_user_update_transactions.rb
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,14 @@ | ||
# frozen_string_literal: true | ||
|
||
# Version: 20241115023724 | ||
class CreateUserUpdateTransactions < ActiveRecord::Migration[8.0] | ||
def change | ||
create_table(:user_update_transactions) do |t| | ||
t.references( | ||
:user, type: :uuid, foreign_key: { on_delete: :cascade }, index: true) | ||
t.jsonb(:change_set, null: false) | ||
|
||
t.datetime(:created_at, null: false, index: true) | ||
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