From 352fd609e3e42b5b129e0ecea3a6061f5525d717 Mon Sep 17 00:00:00 2001 From: Jason Zimdars Date: Fri, 13 Sep 2024 08:59:07 -0500 Subject: [PATCH] Include `Added by...` event in comments thread, move to helper --- app/helpers/bubbles_helper.rb | 59 +++++++++++++++++++++++++++++++++ app/views/bubbles/show.html.erb | 18 +--------- 2 files changed, 60 insertions(+), 17 deletions(-) diff --git a/app/helpers/bubbles_helper.rb b/app/helpers/bubbles_helper.rb index b6f54058c..b552b5d2b 100644 --- a/app/helpers/bubbles_helper.rb +++ b/app/helpers/bubbles_helper.rb @@ -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 diff --git a/app/views/bubbles/show.html.erb b/app/views/bubbles/show.html.erb index 488765931..1cfaa83d4 100644 --- a/app/views/bubbles/show.html.erb +++ b/app/views/bubbles/show.html.erb @@ -25,22 +25,6 @@
- <% 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) %> -
- <%= user_boosts.map { |user, count| "#{user.name} +#{count}" }.to_sentence %> -
- <% boosts.clear %> - <% end %> - <% else %> - <%= render partial: "comments/comment", object: comment %> - <% end %> - <% end %> + <%= render_comments_and_boosts(@bubble) %> <%= render "comments/new" %>