Skip to content

Commit

Permalink
WEB-6757: Improving lesson and segment validation
Browse files Browse the repository at this point in the history
  • Loading branch information
sammyd committed Jan 10, 2024
1 parent 5bb4d43 commit a0d7ed2
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
1 change: 1 addition & 0 deletions app/models/lesson.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Lesson
attr_markdown :description, source: :description_md, file: false
attr_markdown :learning_objectives, source: :learning_objectives_md, file: false
validates :title, :ordinal, :ref, presence: true
validates :segments, length: { minimum: 1 }, allow_blank: false, segments: true

def initialize(attributes = {})
super
Expand Down
20 changes: 8 additions & 12 deletions app/models/lessons_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,20 @@ def check_correct_class(record, attribute, value)
def check_unique_refs(record, attribute, value)
return unless value.is_a?(Array)

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

non_unique_refs.each { |ref| record.errors.add(attribute, "(#{lesson.title}) segment ref #{ref} is not unique") }
end
non_unique_refs.each { |ref| record.errors.add(attribute, "=> ref '#{ref}' is not unique") }
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
title_counts = Hash.new(0)
value.each { |lesson| title_counts[lesson.title] += 1 }
non_unique_titles = title_counts.select { |_, count| count > 1 }.keys

non_unique_titles.each { |title| record.errors.add(attribute, "(#{lesson.title}) segment title #{title} is not unique") }
end
non_unique_titles.each { |title| record.errors.add(attribute, "=> Title '#{title}' is not unique") }
end
end
39 changes: 39 additions & 0 deletions app/models/segments_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

# A validator that will check an array of choices for uniqueness of ref
class SegmentsValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
return unless value.is_a?(Array)

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)
value.each do |segment|
record.errors.add(attribute, "segment #{segment} needs to be a quiz, video or text") unless [Assessment::Quiz, Video, Text].any? { |klass| segment.is_a?(klass) }
end
end

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

ref_counts = Hash.new(0)
value.each { |segment| ref_counts[segment.ref] += 1 }
non_unique_refs = ref_counts.select { |_, count| count > 1 }.keys

non_unique_refs.each { |ref| record.errors.add(attribute, "(#{record.title}) => Segment ref '#{ref}' is not unique") }
end

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

title_counts = Hash.new(0)

value.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, "(#{record.title}) => Segment title '#{title}' is not unique") }
end
end

0 comments on commit a0d7ed2

Please sign in to comment.