From 9573683e638b5f391f10765f494c9ab03e3e4eff Mon Sep 17 00:00:00 2001 From: James Smith Date: Tue, 28 May 2024 10:50:10 +0100 Subject: [PATCH] better grouping by finding groups with longest common prefixes --- app/helpers/models_helper.rb | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/app/helpers/models_helper.rb b/app/helpers/models_helper.rb index 9486a2ebc..f43b4900b 100644 --- a/app/helpers/models_helper.rb +++ b/app/helpers/models_helper.rb @@ -1,11 +1,22 @@ module ModelsHelper def group(files) - groups = files.group_by { |i| i.filename.split(/[\ _\-:.]/)[0] } - ungrouped = [] - groups.each_pair do |group, p| - ungrouped << groups.delete(group)[0] if p.count == 1 + sections = {} + min_section_size = 2 + min_prefix_length = 3 + names = files.map(&:basename) + slice = names.map(&:length).max + while slice > min_prefix_length + slice -= 1 + candidates = names.map { |x| x.slice(0, slice) } + groups = candidates.group_by { |x| x } + ready = groups.select { |k, v| v.count >= min_section_size }.map(&:first) + ready.each do |r| + names.reject! { |x| x.starts_with? r } + sections[r], files = files.partition { |x| x.basename.starts_with? r } + end end - groups.merge(nil => ungrouped) + # Sort and include empty set + { nil => files }.merge sections.sort_by {|k, v| k }.to_h end def status_badges(model)