782259a678
Previously when an item was `created`, we'd track the event, update the summary text, and broadcast the notifications. But now that we have a draft state, we shold do all of this when it's published instead.
41 lines
1.3 KiB
Ruby
41 lines
1.3 KiB
Ruby
class EventSummary < ApplicationRecord
|
|
include Messageable
|
|
|
|
has_many :events, -> { chronologically }, dependent: :delete_all, inverse_of: :summary
|
|
|
|
# FIXME: Consider persisting the body and compute at write time.
|
|
def body
|
|
"#{main_summary} #{boosts_summary}".squish
|
|
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 "published"
|
|
"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 "unassigned"
|
|
"Unassigned from #{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
|
|
if tally = events.boosts.group(:creator).count.presence
|
|
tally.map do |creator, count|
|
|
"#{creator.name} +#{count}"
|
|
end.to_sentence + "."
|
|
end
|
|
end
|
|
end
|