Sha256: 44a4cfc13ba3bd71bda0687ce35bc8c29badb9613530dceb6e5d53c48d895728

Contents?: true

Size: 997 Bytes

Versions: 6

Compression:

Stored size: 997 Bytes

Contents

require "active_model/validations/presence"

module Paperclip
  module Validators
    class AttachmentPresenceValidator < ActiveModel::EachValidator
      def validate_each(record, attribute, _value)
        record.errors.add(attribute, :blank, options) if record.send("#{attribute}_file_name").blank?
      end

      def self.helper_method_name
        :validates_attachment_presence
      end
    end

    module HelperMethods
      # Places ActiveModel validations on the presence of a file.
      # Options:
      # * +if+: A lambda or name of an instance method. Validation will only
      #   be run if this lambda or method returns true.
      # * +unless+: Same as +if+ but validates if lambda or method returns false.
      def validates_attachment_presence(*attr_names)
        options = _merge_attributes(attr_names)
        validates_with AttachmentPresenceValidator, options.dup
        validate_before_processing AttachmentPresenceValidator, options.dup
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
kt-paperclip-6.4.1 lib/paperclip/validators/attachment_presence_validator.rb
kt-paperclip-6.4.0 lib/paperclip/validators/attachment_presence_validator.rb
kt-paperclip-6.3.0 lib/paperclip/validators/attachment_presence_validator.rb
kt-paperclip-6.2.2 lib/paperclip/validators/attachment_presence_validator.rb
kt-paperclip-6.2.1 lib/paperclip/validators/attachment_presence_validator.rb
kt-paperclip-6.2.0 lib/paperclip/validators/attachment_presence_validator.rb