Skip to content

Commit

Permalink
Test for title uniqueness at validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin De Pelseneer committed Sep 28, 2023
1 parent d3ff2f9 commit 7950832
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions app/models/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ def validate_template_attributes
end
end

return if test_input_occurence

errors.add(:base, '[Template attribute]: You are not allowed to have more than one Input attribute.')
test_input_occurence
test_attribute_title_uniqueness
end

private
Expand All @@ -67,7 +66,19 @@ def test_tag_occurences
end

def test_input_occurence
template_attributes.map(&:title).count('Input') <= 1
return if template_attributes.map(&:title).map(&:downcase).compact.count('input') <= 1

errors.add(:base, '[Template attribute]: You are not allowed to have more than one Input attribute.')
end

def test_attribute_title_uniqueness
template_attribute_titles = template_attributes.map(&:title).uniq
duplicate_attributes = template_attribute_titles.map do |tat|
if template_attributes.select { |ta| ta.title.downcase == tat.downcase }.map(&:title).count > 1
errors.add(:template_attributes, "Attribute names must be unique, there are duplicates of #{tat}")
return tat
end
end
end

def isa_tag_white_list(template_level)
Expand Down

0 comments on commit 7950832

Please sign in to comment.