Added CSV export for items
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-22 04:47:04 +02:00
parent e4bbb329b5
commit ae05cb53b6
5 changed files with 55 additions and 1 deletions

View File

@@ -4,6 +4,19 @@ class ItemsController < ApplicationController
# GET /items or /items.json
def index
@items = Item.all.includes(:category, :user, :room).order(created_at: :desc)
respond_to do |format|
format.html # Rendert ganz normal deine Bestandsliste im Browser
format.csv do
# Dateiname generieren, z.B. "inventar_export_2026-05-22.csv"
filename = "inventar_export_#{Time.current.strftime('%Y-%m-%d')}.csv"
# Daten generieren und als Download an den Browser senden
send_data Item.to_csv,
filename: filename,
type: "text/csv; charset=utf-8; header=present"
end
end
end
# GET /items/1 or /items/1.json

View File

@@ -1,4 +1,5 @@
require "rqrcode"
require "csv"
class Item < ApplicationRecord
belongs_to :category
@@ -31,6 +32,41 @@ class Item < ApplicationRecord
).html_safe # Sagt Rails, dass das HTML unbedenklich ausgegeben werden darf
end
def self.to_csv
# Die Spaltenüberschriften, die in der Excel-Datei erscheinen sollen
headers = [ "ID", "Artikelname", "SKU", "Seriennummer", "Sticker_ID", "Einkaufspreis", "Kategorie", "Aktueller_Standort", "Notizen", "Registriert_am" ]
CSV.generate(headers: true, col_sep: ";", encoding: "UTF-8") do |csv|
# 1. Kopfzeile schreiben
csv << headers
# 2. Datenzeilen schreiben (includes verhindert langsame N+1 Datenbankabfragen)
all.includes(:category, :user, :room).each do |item|
# Dynamischen Standort-Text ermitteln
current_location = if item.user.present?
"👤 #{item.user.name}"
elsif item.room.present?
"📍 #{item.room.name_with_building}"
else
"📦 Im Hauptlager"
end
csv << [
item.id,
item.name,
item.sku,
item.serial_number,
item.sticker_id,
item.price,
item.category&.name,
current_location,
item.notes,
item.created_at.strftime("%d.%m.%Y %H:%M")
]
end
end
end
private
def either_user_or_room

View File

@@ -16,7 +16,7 @@
<%= link_to items_path(format: :csv), class: "py-2 px-3 border border-gray-300 rounded-lg text-sm font-medium bg-white text-gray-700 hover:bg-gray-50 flex items-center gap-1.5 transition shadow-sm", title: "Liste als Excel/CSV exportieren" do %>
<!-- Heroicon: arrow-down-tray -->
<svg class="h-4 w-4 text-gray-500" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M3 16.5v2.25A2.25 2.25 0 0 0 5.25 21h13.5A2.25 2.25 0 0 0 21 18.75V16.5M16.5 12 12 16.5m0 0L7.5 12m4.5 4.5V3" /></svg>
<span>Daten exportieren</span>
<span>CSV</span>
<% end %>
<% end %>