Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WEB-6564: Complain at non-unique titles #226

Merged
merged 1 commit into from
Oct 13, 2023
Merged
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
13 changes: 13 additions & 0 deletions app/models/lessons_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def validate_each(record, attribute, value)

check_correct_class(record, attribute, value)
check_unique_refs(record, attribute, value)
check_unique_title(record, attribute, value)
end

def check_correct_class(record, attribute, value)
Expand All @@ -26,4 +27,16 @@ def check_unique_refs(record, attribute, value)
non_unique_refs.each { |ref| record.errors.add(attribute, "(#{lesson.title}) segment ref #{ref} is not unique") }
end
end

def check_unique_title(record, attribute, value)
return unless value.is_a?(Array)

value.each do |lesson|
title_counts = Hash.new(0)
lesson.segments.each { |segment| title_counts[segment.title] += 1 }
non_unique_titles = title_counts.select { |_, count| count > 1 }.keys

non_unique_titles.each { |title| record.errors.add(attribute, "(#{lesson}) segment title #{title} is not unique") }
end
end
end