Class: Labimotion::UserSetting
- Inherits:
-
ApplicationRecord
- Object
- ApplicationRecord
- Labimotion::UserSetting
- Defined in:
- lib/labimotion/models/user_setting.rb
Overview
Per-user, klass-independent settings — one row per (user, key), each with its
own settings jsonb.
The sibling table user_klass_settings holds what a user configures ABOUT a klass (toolbar, linked-element attributes); this one holds what belongs to the user themselves, whatever klass they are looking at: their AI credentials today, their own layers next.
Split by row rather than one settings blob per user on purpose. The keys have nothing to do with each other and differ wildly in size — a stored credential is a few hundred bytes, a personal layer library can be a lot more — so sharing a row would mean every save rewrites the lot, and two features saving at once would clobber each other. A unique index on (user_id, key) keeps one row per feature per user; see Labimotion::Constants::UserSettingKey for the keys in use.
Values are read and written through a per-key wrapper that owns the shape
(Labimotion::UserAiSettings for AI), not by reaching into settings from
callers — the same division the klass-settings API keeps.
Class Method Summary collapse
-
.settings_for(user_id, key) ⇒ Object
The stored hash for one key, or {} when the user has never saved any.
-
.update_settings(user_id, key) ⇒ Object
Read-modify-write of one key's hash.
-
.write_settings(user_id, key, settings) ⇒ Object
Replace the stored hash for one key.
Class Method Details
.settings_for(user_id, key) ⇒ Object
The stored hash for one key, or {} when the user has never saved any. Never nil, so callers can read straight through it.
28 29 30 31 32 |
# File 'lib/labimotion/models/user_setting.rb', line 28 def self.settings_for(user_id, key) return {} if user_id.blank? || key.blank? find_by(user_id: user_id, key: key)&.settings || {} end |
.update_settings(user_id, key) ⇒ Object
Read-modify-write of one key's hash. The block is handed a mutable copy and whatever it returns is stored, so a caller changing one field cannot drop the others by accident.
55 56 57 58 |
# File 'lib/labimotion/models/user_setting.rb', line 55 def self.update_settings(user_id, key) current = settings_for(user_id, key).dup write_settings(user_id, key, yield(current)) end |
.write_settings(user_id, key, settings) ⇒ Object
Replace the stored hash for one key. Blank-out (an empty hash) deletes the row rather than leaving an empty one behind, so "has this user configured anything?" stays answerable by the row's existence.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/labimotion/models/user_setting.rb', line 37 def self.write_settings(user_id, key, settings) raise ArgumentError, 'user_id is required' if user_id.blank? raise ArgumentError, 'key is required' if key.blank? row = find_or_initialize_by(user_id: user_id, key: key) if settings.blank? row.destroy! if row.persisted? return {} end row.settings = settings row.save! row.settings end |