Class: CarrierWaveDirect::Validations::ActiveModel::UniqueFilenameValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/carrierwave_direct/validations/active_model.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/carrierwave_direct/validations/active_model.rb', line 13

def validate_each(record, attribute, value)
  if record.errors[attribute].empty? && (record.send("has_#{attribute}_upload?") || record.send("has_remote_#{attribute}_net_url?"))
    column = record.class.uploader_options[attribute].fetch(:mount_on, attribute)
    if record.class.where(column => record.send(attribute).filename).exists?
      record.errors.add(attribute, :carrierwave_direct_filename_taken)
    end
  end
end