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.
This commit is contained in:
@@ -11,7 +11,7 @@
|
||||
<% question.question_options.each_with_index do |option, i| %>
|
||||
<tr id="question_<%= question.id %>_<%= i %>_result">
|
||||
<td><%= option.title %></td>
|
||||
<td class="text-center"><%= by_answer[option.title].present? ? by_answer[option.title].sum(&:amount) : 0 %></td>
|
||||
<td class="text-center"><%= votes_for(option) %></td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user