From e3e475b4df2fadb9d3c07eb35146d493d067c348 Mon Sep 17 00:00:00 2001 From: taitus Date: Tue, 16 Sep 2025 15:19:11 +0200 Subject: [PATCH] Add votes_for(option) method and simplify results template Move the summing logic from the template into the component. Introduce a votes_for(option) method that looks up grouped partial results and returns the total amount or 0. --- .../admin/poll/results/question_component.html.erb | 2 +- .../admin/poll/results/question_component.rb | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/app/components/admin/poll/results/question_component.html.erb b/app/components/admin/poll/results/question_component.html.erb index aac83af2f..8a3d6633a 100644 --- a/app/components/admin/poll/results/question_component.html.erb +++ b/app/components/admin/poll/results/question_component.html.erb @@ -11,7 +11,7 @@ <% question.question_options.each_with_index do |option, i| %> <%= option.title %> - <%= by_answer[option.title].present? ? by_answer[option.title].sum(&:amount) : 0 %> + <%= votes_for(option) %> <% end %> diff --git a/app/components/admin/poll/results/question_component.rb b/app/components/admin/poll/results/question_component.rb index c12d9d344..8836a6b91 100644 --- a/app/components/admin/poll/results/question_component.rb +++ b/app/components/admin/poll/results/question_component.rb @@ -6,7 +6,14 @@ class Admin::Poll::Results::QuestionComponent < ApplicationComponent @partial_results = partial_results end - def by_answer - @by_answer ||= partial_results.where(question: question).group_by(&:answer) + def votes_for(option) + grouped = by_answer[option.title] || [] + grouped.sum(&:amount) end + + private + + def by_answer + @by_answer ||= partial_results.where(question: question).group_by(&:answer) + end end