From d31d19a45eff54f8b6a238a8eb243ee309bfa5ef Mon Sep 17 00:00:00 2001 From: Jason Zimdars Date: Wed, 18 Sep 2024 11:42:02 -0500 Subject: [PATCH] Clean this up --- app/helpers/comments_helper.rb | 110 ++++++++++++--------------------- 1 file changed, 40 insertions(+), 70 deletions(-) diff --git a/app/helpers/comments_helper.rb b/app/helpers/comments_helper.rb index 4bc84b524..fbe935e55 100644 --- a/app/helpers/comments_helper.rb +++ b/app/helpers/comments_helper.rb @@ -1,10 +1,6 @@ module CommentsHelper def render_comments_and_boosts(bubble) - combined_collection = ( - bubble.comments + - bubble.boosts + - bubble.assignments - ).sort_by(&:created_at) + combined_collection = combine_and_sort_items(bubble) safe_join([ render_creator_summary(bubble, combined_collection), @@ -13,98 +9,72 @@ 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) - ]) + def combine_and_sort_items(bubble) + (bubble.comments + bubble.boosts + bubble.assignments).sort_by(&:created_at) end def render_creator_summary(bubble, combined_collection) content_tag(:div, class: "comment--upvotes flex-inline flex-wrap 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" - - 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 + [ + creator_info(bubble), + initial_assignment_info(combined_collection), + render_initial_boosts(combined_collection) + ].compact.join(", ").html_safe end end + def creator_info(bubble) + "Added by #{bubble.creator.name} #{time_ago_in_words(bubble.created_at)} ago" + end + + def initial_assignment_info(combined_collection) + initial_assignment = combined_collection.find { |item| item.is_a?(Assignment) } + "assigned to #{initial_assignment.user.name}" if initial_assignment + end + def render_initial_boosts(combined_collection) - grouped_boosts = [] - combined_collection.each do |item| - break unless item.is_a?(Boost) - grouped_boosts << item - end + grouped_boosts = combined_collection.take_while { |item| item.is_a?(Boost) } + return if grouped_boosts.empty? - 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 + user_boosts = grouped_boosts.group_by(&:creator).transform_values(&:count) + boost_summaries = user_boosts.map { |user, count| "#{user.name} +#{count}" } + boost_summaries.to_sentence end def render_remaining_items(combined_collection) - 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 + initial_count = combined_collection.take_while { |item| !item.is_a?(Comment) }.count + items = combined_collection.drop(initial_count) + + safe_join(items.chunk_while { |i, j| grouped_item?(i) && grouped_item?(j) }.map do |chunk| + if chunk.first.is_a?(Comment) + render partial: "comments/comment", object: chunk.first + else + render_grouped_items(chunk) end - end.compact) + end) + end + + def grouped_item?(item) + item.is_a?(Boost) || item.is_a?(Assignment) 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 flex-wrap align-start gap fill-white border-radius center position-relative") do - combined_summaries = [ - render_grouped_boosts(boosts), - render_grouped_assignments(assignments) - ].flatten.compact - - combined_summaries.to_sentence.html_safe + [ + render_grouped_boosts(items.select { |item| item.is_a?(Boost) }), + render_grouped_assignments(items.select { |item| item.is_a?(Assignment) }) + ].flatten.compact.to_sentence.html_safe end end def render_grouped_boosts(boosts) return if boosts.empty? - user_boosts = boosts.group_by(&:creator).transform_values(&:count) - user_boosts.map { |user, count| "#{user.name} +#{count}" } + boosts.group_by(&:creator).map { |user, user_boosts| "#{user.name} +#{user_boosts.count}" } end def render_grouped_assignments(assignments) - return if assignments.empty? assignments.map { |assignment| "Assigned to #{assignment.user.name} #{time_ago_in_words(assignment.created_at)} ago" } 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) - combined_collection.take_while { |item| item.is_a?(Boost) }.count - end end