Let EventSummary do the work to summarize events

This commit is contained in:
Jose Farias
2024-10-27 17:41:48 -06:00
parent 0727414017
commit 287a956364
3 changed files with 35 additions and 29 deletions
-20
View File
@@ -1,20 +0,0 @@
module EventSummariesHelper
def event_description_for(event)
case event.action
when "created"
"Added by #{event.creator.name} #{time_ago_in_words(event.created_at)} ago."
when "assigned"
"Assigned to #{event.assignees.pluck(:name).to_sentence} #{time_ago_in_words(event.created_at)} ago."
when "staged"
"#{event.creator.name} moved this to '#{event.stage_name}'."
when "unstaged"
"#{event.creator.name} removed this from '#{event.stage_name}'."
end
end
def tallied_boosts_for(event_summary)
if (tally = event_summary.events.tallied_boosts.presence)
tally.map { |creator, count| "#{creator.name} +#{count}" }.to_sentence + "."
end
end
end
+34 -4
View File
@@ -1,9 +1,39 @@
class EventSummary < ApplicationRecord
include Messageable
has_many :events, -> { chronologically }, dependent: :delete_all, inverse_of: :summary do
def tallied_boosts
boosts.group(:creator).count
end
has_many :events, -> { chronologically }, dependent: :delete_all, inverse_of: :summary
def body
"#{main_summary} #{boosts_summary}"
end
private
delegate :time_ago_in_words, to: "ApplicationController.helpers"
def main_summary
events.non_boosts.map { |event| summarize(event) }.join(" ")
end
def summarize(event)
case event.action
when "created"
"Added by #{event.creator.name} #{time_ago_in_words(event.created_at)} ago."
when "assigned"
"Assigned to #{event.assignees.pluck(:name).to_sentence} #{time_ago_in_words(event.created_at)} ago."
when "staged"
"#{event.creator.name} moved this to '#{event.stage_name}'."
when "unstaged"
"#{event.creator.name} removed this from '#{event.stage_name}'."
end
end
def boosts_summary
tallied_boosts.map do |creator, count|
"#{creator.name} +#{count}"
end.to_sentence + "."
end
def tallied_boosts
events.boosts.group(:creator).count
end
end
@@ -1,7 +1,3 @@
<div class="event-summary flex-inline flex-wrap align-start fill-white border-radius center position-relative" data-controller="event-summary">
<% event_summary.events.non_boosts.each do |event| %>
<%= event_description_for event %>
<% end %>
<%= tallied_boosts_for event_summary %>
<%= event_summary.body %>
</div>