File: nested_attributes_duplicates_validator.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (40 lines) | stat: -rw-r--r-- 1,562 bytes parent folder | download
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
31
32
33
34
35
36
37
38
39
40
# frozen_string_literal: true

# NestedAttributesDuplicates
#
# 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 NestedAttributesDuplicatesValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    return if child_attributes.any? { |child_attribute| record.errors.include?(:"#{attribute}.#{child_attribute}") }

    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

  # rubocop: disable CodeReuse/ActiveRecord
  def validate_duplicates(record, attribute, values)
    child_attributes.each do |child_attribute|
      duplicates = values.reject(&:marked_for_destruction?).group_by(&:"#{child_attribute}").select { |_, v| v.many? }.map(&:first)
      next unless 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
  # rubocop: enable CodeReuse/ActiveRecord

  def child_attributes
    options[:child_attributes] || %i[key]
  end
end