Include Added by... event in comments thread, move to helper

This commit is contained in:
Jason Zimdars
2024-09-13 08:59:07 -05:00
parent cc69d84c96
commit 352fd609e3
2 changed files with 60 additions and 17 deletions
+59
View File
@@ -15,4 +15,63 @@ module BubblesHelper
"--bubble-size: #{value}cqi;"
end
def render_comments_and_boosts(bubble)
combined_collection = (bubble.comments + bubble.boosts).sort_by(&:updated_at)
safe_join([
render_creator_summary(bubble, combined_collection),
render_remaining_items(combined_collection)
])
end
private
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)
summary.html_safe
end
end
def render_initial_boosts(combined_collection)
grouped_boosts = []
combined_collection.each do |item|
break unless item.is_a?(Boost)
grouped_boosts << item
end
if grouped_boosts.any?
user_boosts = grouped_boosts.group_by(&:creator).transform_values(&:count)
boost_summaries = user_boosts.map { |user, count| "#{user.name} +#{count}" }
", #{boost_summaries.to_sentence}"
else
""
end
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
render partial: "comments/comment", object: item
end
end.compact)
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
end
end
def initial_boosts_count(combined_collection)
combined_collection.take_while { |item| item.is_a?(Boost) }.count
end
end
+1 -17
View File
@@ -25,22 +25,6 @@
</div>
<section class="comments align-center center borderless margin flex flex-column gap-half" style="--bubble-color: <%= @bubble.color %>;">
<% comments = (@bubble.comments + @bubble.boosts).sort_by(&:updated_at) %>
<% boosts = [] %>
<% comments.each_with_index do |comment, index| %>
<% if comment.is_a?(Boost) %>
<% boosts << comment %>
<% if comments[index + 1].nil? || !comments[index + 1].is_a?(Boost) %>
<% user_boosts = boosts.group_by(&:creator).transform_values(&:count) %>
<div class="comment--upvotes flex-inline align-start gap fill-white border-radius center position-relative">
<%= user_boosts.map { |user, count| "#{user.name} +#{count}" }.to_sentence %>
</div>
<% boosts.clear %>
<% end %>
<% else %>
<%= render partial: "comments/comment", object: comment %>
<% end %>
<% end %>
<%= render_comments_and_boosts(@bubble) %>
<%= render "comments/new" %>
</section>