Skip to content

Commit

Permalink
296 Inline error unique titles and weighted scores
Browse files Browse the repository at this point in the history
  • Loading branch information
cpreisinger committed Dec 17, 2024
1 parent bfd87c5 commit 44ef23f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/helpers/form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def input_error_class(form, field)
def inline_error(form, field)
object = form.object
field_id = (form.object_name + "_#{field}_error").gsub(/[\[\]]/, "_").squeeze('_')
error = object.errors[field].present? ? object.errors[field].first : ""
error = object.errors[field].present? ? object.errors[field].join(", ") : ""

tag.span(error, class: "text-secondary font-body-2xs", id: field_id)
end
Expand Down
23 changes: 20 additions & 3 deletions app/models/evaluation_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,37 @@ class EvaluationForm < ApplicationRecord
private

def validate_unique_criteria_titles
titles = evaluation_criteria.reject(&:marked_for_destruction?).map(&:title)
current_criteria = evaluation_criteria.reject(&:marked_for_destruction?)

return unless titles.uniq.length != titles.length
titles = current_criteria.map(&:title)
duplicate_titles = titles.select { |title| titles.count(title) > 1 }.uniq

return if duplicate_titles.empty?

duplicate_titles.each do |duplicate_title|
current_criteria.each do |criterion|
if criterion.title == duplicate_title
criterion.errors.add(:title, I18n.t("evaluation_criteria.duplicate_title_error"))
end
end
end

errors.add(:base, I18n.t("evaluation_criterion_unique_title_in_form_error"))
end

def criteria_weights_must_sum_to_one_hundred
total_weight = evaluation_criteria.reject(&:marked_for_destruction?).sum do |criteria|
current_criteria = evaluation_criteria.reject(&:marked_for_destruction?)

total_weight = current_criteria.sum do |criteria|
criteria.points_or_weight.to_i
end

return unless weighted_scoring? && total_weight != 100

current_criteria.each_with_index do |criteria, _index|
criteria.errors.add("points_or_weight", I18n.t("evaluation_criteria.must_sum_to_100_error"))
end

errors.add(:base, I18n.t("evaluation_form_criteria_weight_total_error"))
end
end
3 changes: 3 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ en:
login_error: "There was an issue with logging in. Please try again."
please_try_again: "Please try again."
session_expired_alert: "Your session has expired. Please log in again."
evaluation_criteria:
duplicate_title_error: "must be unique"
must_sum_to_100_error: "must sum to 100"
evaluation_criterion_unique_title_in_form: "must be unique within the same form."
evaluators:
index:
Expand Down

0 comments on commit 44ef23f

Please sign in to comment.