Create system comments on events

This commit is contained in:
Jorge Manrubia
2025-04-29 20:30:05 +02:00
parent e03f1f7f9b
commit 4ff55df2e0
6 changed files with 79 additions and 4 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
module CommentsHelper
def new_comment_placeholder(card)
if card.creator == Current.user && card.messages.comments.empty?
if card.creator == Current.user && card.comments.empty?
"Next, add some notes, context, pictures, or video about this…"
else
"Type your comment…"
+5 -1
View File
@@ -12,7 +12,7 @@ module Card::Eventable
def event_was_created(event)
transaction do
# find_or_capture_event_summary.events << event
create_system_comment_for(event)
touch(:last_active_at)
end
end
@@ -43,4 +43,8 @@ module Card::Eventable
track_event "card_title_changed", particulars: { old_title: title_before_last_save, new_title: title }
end
end
def create_system_comment_for(event)
SystemCommenter.new(self, event).comment
end
end
@@ -0,0 +1,63 @@
class Card::Eventable::SystemCommenter
attr_reader :card, :event
def initialize(card, event)
@card, @event = card, event
end
def comment
return unless comment_body.present?
if comment = find_replaceable_system_comment
comment.update! body: comment_body
else
card.comments.create! creator: User.system, body: comment_body
end
end
private
REPLACEABLE_EVENTS = [
%w(card_assigned card_unassigned),
%w(card_staged card_unstaged),
%w(card_due_date_added card_due_date_changed card_due_date_removed)
]
def comment_body
case event.action
when "card_assigned"
"Assigned to #{card.assignees.pluck(:name).to_sentence}."
when "card_unassigned"
if card.assignees.empty?
"Unassigned from #{event.assignees.pluck(:name).to_sentence}."
else
"Assigned to #{card.assignees.pluck(:name).to_sentence}."
end
when "card_staged"
"#{event.creator.name} moved this to '#{event.stage_name}'."
when "card_closed"
"Closed by #{ event.creator.name }"
when "card_unstaged"
"#{event.creator.name} removed this from '#{event.stage_name}'."
when "card_due_date_added"
"#{event.creator.name} set due date to #{event.particulars.dig('particulars', 'due_date').to_date.strftime('%B %-d')}."
when "card_due_date_changed"
"#{event.creator.name} changed due date to #{event.particulars.dig('particulars', 'due_date').to_date.strftime('%B %-d')}."
when "card_due_date_removed"
"#{event.creator.name} removed the date."
when "card_title_changed"
"#{event.creator.name} changed title from '#{event.particulars.dig('particulars', 'old_title')}' to '#{event.particulars.dig('particulars', 'new_title')}'."
end
end
def find_replaceable_system_comment
previous_comment, previous_event = card.comments.last, card.events[-2]
if previous_comment&.creator&.system? && replaceable_event?(previous_event, event)
previous_comment
end
end
def replaceable_event?(candidate_event, replacement_event)
candidate_event && replacement_event &&
(candidate_event.action == replacement_event.action || REPLACEABLE_EVENTS.find { |related_actions| related_actions.include?(candidate_event.action) && related_actions.include?(replacement_event.action) })
end
end
+4
View File
@@ -12,6 +12,10 @@ module Comment::Eventable
end
private
def should_track_event?
!creator.system?
end
def track_creation
track_event("created", collection: card.collection)
end
+2 -2
View File
@@ -1,6 +1,6 @@
<% if card.messages.comments.any? %>
<div class="card__detail card__comments">
<span aria-hidden="true"><%= card.messages.comments.count %></span>
<span class="for-screen-reader"><%= pluralize(card.messages.comments.count, "comment") %></span>
<span aria-hidden="true"><%= card.comments.count %></span>
<span class="for-screen-reader"><%= pluralize(card.comments.count, "comment") %></span>
</div>
<% end %>
+4
View File
@@ -1,6 +1,10 @@
require "test_helper"
class Card::CloseableTest < ActiveSupport::TestCase
setup do
Current.session = sessions(:david)
end
test "closed scope" do
assert_equal [ cards(:shipping) ], Card.closed
assert_not_includes Card.open, cards(:shipping)