Skip to content

Commit

Permalink
Merge pull request #226 from razeware/WEB-6564
Browse files Browse the repository at this point in the history
WEB-6564: Complain at non-unique titles
  • Loading branch information
jellodiil authored Oct 13, 2023
2 parents 3bbcd31 + 4950878 commit 5006f15
Showing 1 changed file with 13 additions and 0 deletions.
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

0 comments on commit 5006f15

Please sign in to comment.