From f3116c96cbc75f41d587e1e410a9fbffede61d3e Mon Sep 17 00:00:00 2001 From: Kapil Sachdev Date: Wed, 4 Oct 2023 22:45:45 +0530 Subject: [PATCH] WEB-6508: Validate presence of ref in text lesson --- app/models/lessons_validator.rb | 2 +- app/models/text.rb | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/app/models/lessons_validator.rb b/app/models/lessons_validator.rb index f7ed239..1c068c4 100644 --- a/app/models/lessons_validator.rb +++ b/app/models/lessons_validator.rb @@ -23,7 +23,7 @@ def check_unique_refs(record, attribute, value) lesson.segments.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, "segment ref #{ref} is not unique") } + non_unique_refs.each { |ref| record.errors.add(attribute, "(#{lesson.title}) segment ref #{ref} is not unique") } end end end diff --git a/app/models/text.rb b/app/models/text.rb index 0a79177..edc9e98 100644 --- a/app/models/text.rb +++ b/app/models/text.rb @@ -12,7 +12,11 @@ class Text attr_accessor :title, :ordinal, :ref, :description, :authors, :markdown_file, :root_path, :free, :kind attr_markdown :body, source: :markdown_file, file: true, wrapper_class: :wrapper_class - validates :title, :ordinal, :markdown_file, presence: true + validates :title, :ordinal, :ref, :markdown_file, presence: true + validate do |text| + # Check the ref is a string + errors.add(:ref, 'must be a string') unless text.ref.is_a?(String) + end def initialize(attributes = {}) super