2017-07-05 11:59:45 -04:00
|
|
|
# VariableDuplicatesValidator
|
|
|
|
#
|
2018-02-13 08:51:44 -05:00
|
|
|
# This validator is designed for especially the following condition
|
2017-07-05 11:59:45 -04:00
|
|
|
# - 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)
|
2018-02-18 15:54:55 -05:00
|
|
|
return if record.errors.include?(:"#{attribute}.key")
|
|
|
|
|
2018-02-13 08:51:44 -05:00
|
|
|
if options[:scope]
|
2018-02-13 11:59:08 -05:00
|
|
|
scoped = value.group_by do |variable|
|
|
|
|
Array(options[:scope]).map { |attr| variable.send(attr) } # rubocop:disable GitlabSecurity/PublicSend
|
|
|
|
end
|
2018-02-13 08:51:44 -05:00
|
|
|
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)
|
2017-07-05 11:59:45 -04:00
|
|
|
if duplicates.any?
|
2018-02-14 16:51:04 -05:00
|
|
|
error_message = "have duplicate values (#{duplicates.join(", ")})"
|
2018-02-14 14:26:54 -05:00
|
|
|
error_message += " for #{values.first.send(options[:scope])} scope" if options[:scope] # rubocop:disable GitlabSecurity/PublicSend
|
2018-02-13 13:46:02 -05:00
|
|
|
record.errors.add(attribute, error_message)
|
2017-07-05 11:59:45 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|