summaryrefslogtreecommitdiff
path: root/app/validators/variable_duplicates_validator.rb
blob: 72660be6c43fd458b26211089465c06981179e2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# VariableDuplicatesValidator
#
# This validator is designed for especially the following condition
# - Use `accepts_nested_attributes_for :xxx` in a parent model
# - Use `validates :xxx, uniqueness: { scope: :xxx_id }` in a child model
class VariableDuplicatesValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    return if record.errors.include?(:"#{attribute}.key")

    if options[:scope]
      scoped = value.group_by do |variable|
        Array(options[:scope]).map { |attr| variable.send(attr) } # rubocop:disable GitlabSecurity/PublicSend
      end
      scoped.each_value { |scope| validate_duplicates(record, attribute, scope) }
    else
      validate_duplicates(record, attribute, value)
    end
  end

  private

  def validate_duplicates(record, attribute, values)
    duplicates = values.reject(&:marked_for_destruction?).group_by(&:key).select { |_, v| v.many? }.map(&:first)
    if duplicates.any?
      error_message = "have duplicate values (#{duplicates.join(", ")})"
      error_message += " for #{values.first.send(options[:scope])} scope" if options[:scope] # rubocop:disable GitlabSecurity/PublicSend
      record.errors.add(attribute, error_message)
    end
  end
end