Updated/Fixed Item form and item show
Some checks failed
CI / scan_ruby (push) Has been cancelled
CI / scan_js (push) Has been cancelled
CI / lint (push) Has been cancelled
CI / test (push) Has been cancelled
CI / system-test (push) Has been cancelled

This commit is contained in:
2026-05-23 03:18:56 +02:00
parent ae05cb53b6
commit a19cc6984f
4 changed files with 216 additions and 143 deletions

View File

@@ -2,6 +2,10 @@ require "rqrcode"
require "csv"
class Item < ApplicationRecord
# NEU: Erlaubt es Rails, diese Formularfelder temporär im Speicher zu halten,
# ohne dass dafür Spalten in der Datenbank existieren müssen.
attr_accessor :user_name, :room_name
belongs_to :category
belongs_to :user, optional: true # Optional, falls im Raum oder Lager
belongs_to :room, optional: true # Optional, falls beim User oder Lager
@@ -76,15 +80,19 @@ class Item < ApplicationRecord
end
def track_assignment_changes
# 1. Altes Log-Buch schließen, falls es eine vorherige Zuweisung gab
if user_id_was.present? || room_id_was.present?
last_log = assignment_logs.find_by(user_id: user_id_was, room_id: room_id_was, returned_at: nil)
last_log&.update(returned_at: Time.current)
end
# 1. Altes Log-Buch schließen (Egal ob es bei einem User oder in einem Raum war)
# Wir suchen nach dem Log, das noch kein Rückgabedatum hat (returned_at: nil)
active_log = assignment_logs.find_by(returned_at: nil)
active_log&.update(returned_at: Time.current)
# 2. Neues Log-Buch öffnen für den neuen Inhaber oder den neuen Raum
# 2. Neues Log-Buch öffnen
if user_id.present? || room_id.present?
# Zustand A: Zuweisung an Mitarbeiter oder Raum
assignment_logs.build(user_id: user_id, room_id: room_id, assigned_at: Time.current)
else
# Zustand B: Weder User noch Raum hinterlegt -> Das Gerät wandert ins Hauptlager!
# Wir lassen user_id und room_id auf nil, was im Logbuch "Hauptlager" bedeutet
assignment_logs.build(assigned_at: Time.current)
end
end
end