Sha256: 037bdf34df1e458fc2c682d4c230bf6fcdf9ceba7eead8324aade49b4bfafb61

Contents?: true

Size: 406 Bytes

Versions: 1

Compression:

Stored size: 406 Bytes

Contents

module ExtraValidations
  class ExistenceValidator < ActiveModel::EachValidator
    def check_validity!
      unless options[:with].is_a?(Proc)
        fail ArgumentError, ':with must be a Proc'
      end
    end

    def validate_each(record, attribute, id)
      return if id.blank?

      success = options[:with].call(id)
      record.errors.add(attribute, :not_found) unless success
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
extra_validations-0.1.0 lib/extra_validations/existence_validator.rb