Class: NestedAttributesDuplicatesValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- NestedAttributesDuplicatesValidator
- Defined in:
- app/validators/nested_attributes_duplicates_validator.rb
Overview
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
Instance Method Summary collapse
Instance Method Details
#validate_each(record, attribute, value) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'app/validators/nested_attributes_duplicates_validator.rb', line 9 def validate_each(record, attribute, value) return if child_attributes.any? { |child_attribute| record.errors.include?(:"#{attribute}.#{child_attribute}") } if [:scope] scoped = value.group_by do |variable| Array([: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 |