diff --git a/app/controllers/dashboards_controller.rb b/app/controllers/dashboards_controller.rb index 2e1dd7a..86ddff8 100644 --- a/app/controllers/dashboards_controller.rb +++ b/app/controllers/dashboards_controller.rb @@ -3,41 +3,30 @@ class DashboardsController < ApplicationController @spent_minutes_matrix = {} @remaining_minutes_matrix = {} - # Alle Kombinationen holen - @time_by_typ_art = current_user.entries - .group(:praktikums_typ, :entry_art) - .select(:praktikums_typ, :entry_art) - .group_by(&:praktikums_typ) - .transform_values { |entries| entries.map(&:entry_art).uniq } + # Alle Kombinationen aus Typ & Art + @typ_art_combinations = User::PRAKTIKUMSTYPEN.product(User::ENTRY_ARTEN) - # Matrix-Daten aufbauen (muss zuerst passieren!) - @time_by_typ_art.each do |typ, arts| + @spent_minutes_matrix = {} + @remaining_minutes_matrix = {} + + @typ_art_combinations.each do |typ, art| + spent_minutes = current_user.entries + .where(praktikums_typ: typ, entry_art: art) + .sum { |e| e.total_minutes.to_i } + + target = current_user.required_hours_matrix.dig(typ, art).to_i * 60 + remaining = [target - spent_minutes, 0].max + @spent_minutes_matrix[typ] ||= {} @remaining_minutes_matrix[typ] ||= {} - arts.each do |art| - spent_minutes = current_user.entries - .where(praktikums_typ: typ, entry_art: art) - .sum { |e| e.total_minutes.to_i } - - target = current_user.required_hours_matrix.dig(typ, art).to_i * 60 - remaining = [target - spent_minutes, 0].max - - @spent_minutes_matrix[typ][art] = spent_minutes - @remaining_minutes_matrix[typ][art] = remaining - end + @spent_minutes_matrix[typ][art] = spent_minutes + @remaining_minutes_matrix[typ][art] = remaining end - # Jetzt können wir sauber summieren! - @total_minutes = 0 - @remaining_total = 0 - - @spent_minutes_matrix.each do |typ, arts| - arts.each do |art, spent| - @total_minutes += spent.to_i - @remaining_total += @remaining_minutes_matrix.dig(typ, art).to_i - end - end + # Danach: Gesamtsummen & Prozent + @total_minutes = @spent_minutes_matrix.values.flat_map(&:values).sum + @remaining_total = @remaining_minutes_matrix.values.flat_map(&:values).sum @completed_percent = if (@total_minutes + @remaining_total) > 0 ((@total_minutes.to_f / (@total_minutes + @remaining_total)) * 100).round(1) diff --git a/app/views/dashboards/show.html.erb b/app/views/dashboards/show.html.erb index 148b648..e85e7c5 100644 --- a/app/views/dashboards/show.html.erb +++ b/app/views/dashboards/show.html.erb @@ -19,10 +19,12 @@
- <% @spent_minutes_matrix.each do |typ, arts| %> - <% arts.each do |art, spent| %> + <% User::PRAKTIKUMSTYPEN.each do |typ| %> + <% User::ENTRY_ARTEN.each do |art| %> + <% spent = @spent_minutes_matrix.dig(typ, art).to_i %> <% target = current_user.required_hours_matrix.dig(typ, art).to_i * 60 %> <% percent = target > 0 ? ((spent.to_f / target) * 100).round : 0 %> +