Skip to content

Commit

Permalink
WIP: Add UserUpdateTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul DobbinSchmaltz committed Nov 15, 2024
1 parent 2c5ae22 commit 77a7fe1
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 4 deletions.
42 changes: 38 additions & 4 deletions app/controllers/games/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ def edit

# :reek:TooManyStatements

def update # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
current_user.attributes = update_params

if current_user.save
def update # rubocop:disable Metrics/MethodLength
if UpdateUser.(current_user, attributes: update_params)
broadcast_update(game: @game)

respond_to do |format|
Expand Down Expand Up @@ -54,4 +52,40 @@ def broadcast_update(game:)
target: duty_roster_listing.dom_id,
html: duty_roster_listing.name)
end

# UpdateUser
class UpdateUser
include CallMethodBehaviors

def initialize(user, attributes:)
@user = user
@attributes = attributes
end

def call
user.attributes = attributes
user.user_update_transactions.build(change_set:)
user.save
end

private

attr_reader :user,
:attributes

def change_set
return unless username_changed?

{
username: {
old: username_was,
new: username,
}
}
end

def username_changed? = user.username_changed?
def username = user.username
def username_was = user.username_was
end
end
41 changes: 41 additions & 0 deletions app/models/transactions/user_update_transaction.rb
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
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class User < ApplicationRecord

include ConsoleBehaviors

has_many :user_update_transactions, dependent: :nullify

has_many :game_transactions, dependent: :nullify
has_many :game_create_transactions
has_many :game_start_transactions
Expand Down
14 changes: 14 additions & 0 deletions db/migrate/20241115023724_create_user_update_transactions.rb
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
69 changes: 69 additions & 0 deletions db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,37 @@ CREATE TABLE public.schema_migrations (
);


--
-- Name: user_update_transactions; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.user_update_transactions (
id bigint NOT NULL,
user_id uuid,
change_set jsonb NOT NULL,
created_at timestamp(6) with time zone NOT NULL
);


--
-- Name: user_update_transactions_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--

CREATE SEQUENCE public.user_update_transactions_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;


--
-- Name: user_update_transactions_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--

ALTER SEQUENCE public.user_update_transactions_id_seq OWNED BY public.user_update_transactions.id;


--
-- Name: users; Type: TABLE; Schema: public; Owner: -
--
Expand Down Expand Up @@ -303,6 +334,13 @@ ALTER TABLE ONLY public.games ALTER COLUMN id SET DEFAULT nextval('public.games_
ALTER TABLE ONLY public.patterns ALTER COLUMN id SET DEFAULT nextval('public.patterns_id_seq'::regclass);


--
-- Name: user_update_transactions id; Type: DEFAULT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.user_update_transactions ALTER COLUMN id SET DEFAULT nextval('public.user_update_transactions_id_seq'::regclass);


--
-- Name: ar_internal_metadata ar_internal_metadata_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
Expand Down Expand Up @@ -367,6 +405,14 @@ ALTER TABLE ONLY public.schema_migrations
ADD CONSTRAINT schema_migrations_pkey PRIMARY KEY (version);


--
-- Name: user_update_transactions user_update_transactions_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.user_update_transactions
ADD CONSTRAINT user_update_transactions_pkey PRIMARY KEY (id);


--
-- Name: users users_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
Expand Down Expand Up @@ -571,6 +617,20 @@ CREATE INDEX index_patterns_on_created_at ON public.patterns USING btree (create
CREATE UNIQUE INDEX index_patterns_on_name ON public.patterns USING btree (name);


--
-- Name: index_user_update_transactions_on_created_at; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX index_user_update_transactions_on_created_at ON public.user_update_transactions USING btree (created_at);


--
-- Name: index_user_update_transactions_on_user_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX index_user_update_transactions_on_user_id ON public.user_update_transactions USING btree (user_id);


--
-- Name: index_users_on_username; Type: INDEX; Schema: public; Owner: -
--
Expand Down Expand Up @@ -600,6 +660,14 @@ ALTER TABLE ONLY public.boards
ADD CONSTRAINT fk_rails_1f4dcdc327 FOREIGN KEY (game_id) REFERENCES public.games(id) ON DELETE CASCADE;


--
-- Name: user_update_transactions fk_rails_52bf7868db; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.user_update_transactions
ADD CONSTRAINT fk_rails_52bf7868db FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE;


--
-- Name: cell_transactions fk_rails_8ae22ea0ff; Type: FK CONSTRAINT; Schema: public; Owner: -
--
Expand Down Expand Up @@ -647,6 +715,7 @@ ALTER TABLE ONLY public.cells
SET search_path TO "$user", public;

INSERT INTO "schema_migrations" (version) VALUES
('20241115023724'),
('20241112041937'),
('20240927195322'),
('20240912030247'),
Expand Down

0 comments on commit 77a7fe1

Please sign in to comment.