diff --git a/app/models/workshop.rb b/app/models/workshop.rb index c683b1ece..3b1c188a9 100644 --- a/app/models/workshop.rb +++ b/app/models/workshop.rb @@ -336,8 +336,9 @@ def remote_search_label end def self.resolve_duplicate_labels(labels) - dupes = labels.group_by { |l| l[:label] }.select { |_, v| v.size > 1 }.keys.to_set - labels.each { |l| l[:label] = "#{l[:label]} ##{l[:id]}" if dupes.include?(l[:label]) } + normalize = ->(s) { s.squish.downcase } + dupes = labels.group_by { |l| normalize[l[:label]] }.select { |_, v| v.size > 1 }.flat_map(&:last).to_set + labels.each { |l| l[:label] = "#{l[:label]} ##{l[:id]}" if dupes.include?(l) } labels end diff --git a/spec/models/workshop_spec.rb b/spec/models/workshop_spec.rb index 0bc1b8ed8..713cdfd58 100644 --- a/spec/models/workshop_spec.rb +++ b/spec/models/workshop_spec.rb @@ -133,6 +133,32 @@ { id: w3.id, label: "Music Therapy (ADULT)" } ) end + + it "treats labels as duplicates regardless of case" do + wt = create(:windows_type, :adult) + w1 = create(:workshop, title: "Art Therapy", windows_type: wt) + w2 = create(:workshop, title: "art therapy", windows_type: wt) + + labels = Workshop.resolve_duplicate_labels([ w1, w2 ].map(&:remote_search_label)) + + expect(labels).to contain_exactly( + { id: w1.id, label: "Art Therapy (ADULT) ##{w1.id}" }, + { id: w2.id, label: "art therapy (ADULT) ##{w2.id}" } + ) + end + + it "treats labels as duplicates regardless of extra whitespace" do + wt = create(:windows_type, :adult) + w1 = create(:workshop, title: "Art Therapy", windows_type: wt) + w2 = create(:workshop, title: "Art Therapy", windows_type: wt) + + labels = Workshop.resolve_duplicate_labels([ w1, w2 ].map(&:remote_search_label)) + + expect(labels).to contain_exactly( + { id: w1.id, label: "Art Therapy (ADULT) ##{w1.id}" }, + { id: w2.id, label: "Art Therapy (ADULT) ##{w2.id}" } + ) + end end describe ".remote_search" do