Class: Attachs::Extensions::ActiveRecord::Validations::AttachmentValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/attachs/extensions/active_record/validations/attachment_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, record_attribute, attachment_or_collection) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/attachs/extensions/active_record/validations/attachment_validator.rb', line 9

def validate_each(record, record_attribute, attachment_or_collection)
  case attachment_or_collection
  when Collection
    attachment_or_collection.each do |attachment|
      validate_attachment record, attachment
      if attachment.errors.any?
        record.errors.add record_attribute
      end
    end
  when Attachment
    validate_attachment record, attachment_or_collection
    if attachment_or_collection.errors.any?
      attachment_or_collection.errors.each do |attachment_attribute, message|
        record.errors.add record_attribute, message
      end
    end
  end
end