Added some models
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

User
Item
Department
Categorie
AssignmentLog
Room
This commit is contained in:
2026-05-21 15:36:23 +02:00
parent 7b02520b6c
commit b7f0c35378
27 changed files with 387 additions and 2 deletions

View File

@@ -0,0 +1,7 @@
class AssignmentLog < ApplicationRecord
belongs_to :item
belongs_to :user, optional: true
belongs_to :room, optional: true
validates :assigned_at, presence: true
end

View File

@@ -0,0 +1,5 @@
class AssignmentLog2 < ApplicationRecord
belongs_to :item
belongs_to :user
belongs_to :room
end

2
app/models/category.rb Normal file
View File

@@ -0,0 +1,2 @@
class Category < ApplicationRecord
end

6
app/models/department.rb Normal file
View File

@@ -0,0 +1,6 @@
class Department < ApplicationRecord
has_many :users, dependent: :nullify
has_many :items, through: :users
validates :name, presence: true, uniqueness: true
end

36
app/models/item.rb Normal file
View File

@@ -0,0 +1,36 @@
class Item < ApplicationRecord
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
has_many :assignment_logs, dependent: :destroy
validates :name, :sku, presence: true
validates :sticker_id, :serial_number, presence: true, uniqueness: true
# Validierung: Darf nicht gleichzeitig einem User UND einem Raum gehören
validate :either_user_or_room
# Überwacht Besitzer- oder Raumwechsel für die Historie
before_save :track_assignment_changes, if: -> { will_save_change_to_user_id? || will_save_change_to_room_id? }
private
def either_user_or_room
if user_id.present? && room_id.present?
errors.add(:base, "Ein Artikel kann nicht gleichzeitig einem Benutzer und einem Raum zugewiesen sein.")
end
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
# 2. Neues Log-Buch öffnen für den neuen Inhaber oder den neuen Raum
if user_id.present? || room_id.present?
assignment_logs.build(user_id: user_id, room_id: room_id, assigned_at: Time.current)
end
end
end

11
app/models/room.rb Normal file
View File

@@ -0,0 +1,11 @@
class Room < ApplicationRecord
has_many :items, dependent: :nullify
has_many :assignment_logs, dependent: :destroy
validates :name, presence: true, uniqueness: true
# Für das Raum-Auswahlfeld im Formular
def name_with_building
building.present? ? "#{name} (Gebäude #{building})" : name
end
end

View File

@@ -10,13 +10,18 @@ class User < ApplicationRecord
password_salt.last(10)
end
has_many :sessions, dependent: :destroy
has_many :events, dependent: :destroy
validates :email, presence: true, uniqueness: true, format: { with: URI::MailTo::EMAIL_REGEXP }
validates :password, allow_nil: true, length: { minimum: @@min_length_password }
belongs_to :department, optional: true
has_many :items, dependent: :nullify
has_many :assignment_logs, dependent: :destroy
validates :first_name, :last_name, presence: true, on: :update
normalizes :email, with: -> { _1.strip.downcase }
before_validation if: :email_changed?, on: :update do
@@ -39,6 +44,20 @@ class User < ApplicationRecord
events.create! action: "email_verified"
end
# Gibt den vollen Namen zurück
def name
if first_name.present? && last_name.present?
"#{first_name} #{last_name}"
else
email
end
end
# Für das Besitzer-Auswahlfeld im Formular
def name_with_department
department.present? ? "#{name} (#{department.name})" : name
end
def self.min_length_password
@@min_length_password
end