Class: AssetBoxLengthValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/validators/asset_box_length_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



2
3
4
5
6
7
8
9
10
# File 'lib/validators/asset_box_length_validator.rb', line 2

def validate_each(record, attribute, value)
  num_in_box = (record.attachments || []).select { |attachment| attachment.box == attribute.to_s.pluralize && attachment.marked_for_destruction? == false }.size

  if options[:with]
    record.errors[attribute] << "requires at least #{options[:with]} #{attribute.to_s.pluralize}" if num_in_box < options[:with]
  elsif options[:in]
    record.errors[attribute] << "requires #{options[:in].min} to #{options[:in].max} #{attribute.to_s.pluralize}" if options[:in].include?(num_in_box) == false
  end
end