Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions app/models/workshop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 26 additions & 0 deletions spec/models/workshop_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down