Include assignments in thread

This commit is contained in:
Jason Zimdars
2024-09-18 10:44:15 -05:00
parent 6c77f2ea1c
commit a21ccf049d
+61 -11
View File
@@ -1,6 +1,11 @@
module CommentsHelper
def render_comments_and_boosts(bubble)
combined_collection = (bubble.comments + bubble.boosts).sort_by(&:updated_at)
combined_collection = (
bubble.comments +
bubble.boosts +
bubble.assignments
).sort_by(&:created_at)
safe_join([
render_creator_summary(bubble, combined_collection),
render_remaining_items(combined_collection)
@@ -8,10 +13,27 @@ module CommentsHelper
end
private
def render_comments_and_boosts(bubble)
combined_collection = (
bubble.comments +
bubble.boosts +
bubble.assignments
).sort_by(&:created_at)
safe_join([
render_creator_summary(bubble, combined_collection),
render_remaining_items(combined_collection)
])
end
def render_creator_summary(bubble, combined_collection)
content_tag(:div, class: "comment--upvotes flex-inline align-start gap fill-white border-radius center position-relative") do
summary = "Added by #{bubble.creator.name} #{time_ago_in_words(bubble.created_at)} ago"
summary += render_initial_boosts(combined_collection) if combined_collection.first.is_a?(Boost)
initial_assignment = combined_collection.find { |item| item.is_a?(Assignment) }
summary += ", assigned to #{initial_assignment.user.name}" if initial_assignment
summary += render_initial_boosts(combined_collection)
summary.html_safe
end
end
@@ -33,25 +55,53 @@ module CommentsHelper
end
def render_remaining_items(combined_collection)
grouped_boosts = []
safe_join(combined_collection.drop(initial_boosts_count(combined_collection)).map do |item|
if item.is_a?(Boost)
grouped_boosts << item
next if combined_collection[combined_collection.index(item) + 1].is_a?(Boost)
render_grouped_boosts(grouped_boosts.dup).tap { grouped_boosts.clear }
else
grouped_items = []
safe_join(combined_collection.drop(initial_items_count(combined_collection)).map do |item|
case item
when Boost, Assignment
grouped_items << item
next if combined_collection[combined_collection.index(item) + 1].is_a?(Boost) || combined_collection[combined_collection.index(item) + 1].is_a?(Assignment)
render_grouped_items(grouped_items.dup).tap { grouped_items.clear }
when Comment
render partial: "comments/comment", object: item
end
end.compact)
end
def render_grouped_items(items)
return if items.empty?
boosts = items.select { |item| item.is_a?(Boost) }
assignments = items.select { |item| item.is_a?(Assignment) }
content_tag(:div, class: "comment--upvotes flex-inline align-start gap fill-white border-radius center position-relative") do
safe_join([
render_grouped_boosts(boosts),
render_grouped_assignments(assignments)
].compact)
end
end
def render_grouped_boosts(boosts)
return if boosts.empty?
user_boosts = boosts.group_by(&:creator).transform_values(&:count)
boost_summaries = user_boosts.map { |user, count| "#{user.name} +#{count}" }
content_tag(:div, class: "comment--upvotes flex-inline align-start gap fill-white border-radius center position-relative") do
boost_summaries.to_sentence.html_safe
content_tag(:div, boost_summaries.to_sentence.html_safe)
end
def render_grouped_assignments(assignments)
return if assignments.empty?
assignment_summaries = assignments.map { |assignment| "Assigned to #{assignment.user.name} #{time_ago_in_words(assignment.created_at)} ago" }
content_tag(:div, assignment_summaries.join(", ").html_safe)
end
def initial_items_count(combined_collection)
count = 0
combined_collection.each do |item|
break if item.is_a?(Comment)
count += 1
end
count
end
def initial_boosts_count(combined_collection)