Module: Paperclip::Validators::HelperMethods

Defined in:
lib/paperclip/validators/attachment_size_validator.rb,
lib/paperclip/validators/attachment_presence_validator.rb,
lib/paperclip/validators/attachment_file_name_validator.rb,
lib/paperclip/validators/attachment_content_type_validator.rb,
lib/paperclip/validators/media_type_spoof_detection_validator.rb,
lib/paperclip/validators/attachment_file_type_ignorance_validator.rb

Instance Method Summary collapse

Instance Method Details

#do_not_validate_attachment_file_type(*attr_names) ⇒ Object

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.



22
23
24
25
# File 'lib/paperclip/validators/attachment_file_type_ignorance_validator.rb', line 22

def do_not_validate_attachment_file_type(*attr_names)
  options = _merge_attributes(attr_names)
  validates_with AttachmentFileTypeIgnoranceValidator, options.dup
end

#validates_attachment_content_type(*attr_names) ⇒ Object

Places ActiveModel validations on the content type of the file assigned. The possible options are:

  • content_type: Allowed content types. Can be a single content type or an array. Each type can be a String or a Regexp. It should be noted that Internet Explorer uploads files with content_types that you may not expect. For example, JPEG images are given image/pjpeg and PNGs are image/x-png, so keep that in mind when determining how you match. Allows all by default.

  • not: Forbidden content types.

  • message: The message to display when the uploaded file has an invalid content type.

  • if: A lambda or name of an instance method. Validation will only be run is this lambda or method returns true.

  • unless: Same as if but validates if lambda or method returns false.

NOTE: If you do not specify an [attachment]_content_type field on your model, content_type validation will work _ONLY upon assignment_ and re-validation after the instance has been reloaded will always succeed. You'll still need to have a virtual attribute (created by attr_accessor) name [attachment]_content_type to be able to use this validator.



81
82
83
84
85
# File 'lib/paperclip/validators/attachment_content_type_validator.rb', line 81

def validates_attachment_content_type(*attr_names)
  options = _merge_attributes(attr_names)
  validates_with AttachmentContentTypeValidator, options.dup
  validate_before_processing AttachmentContentTypeValidator, options.dup
end

#validates_attachment_file_name(*attr_names) ⇒ Object

Places ActiveModel validations on the name of the file assigned. The possible options are:

  • matches: Allowed filename patterns as Regexps. Can be a single one or an array.

  • not: Forbidden file name patterns, specified the same was as matches.

  • message: The message to display when the uploaded file has an invalid name.

  • if: A lambda or name of an instance method. Validation will only be run is this lambda or method returns true.

  • unless: Same as if but validates if lambda or method returns false.



72
73
74
75
76
# File 'lib/paperclip/validators/attachment_file_name_validator.rb', line 72

def validates_attachment_file_name(*attr_names)
  options = _merge_attributes(attr_names)
  validates_with AttachmentFileNameValidator, options.dup
  validate_before_processing AttachmentFileNameValidator, options.dup
end

#validates_attachment_presence(*attr_names) ⇒ Object

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.



23
24
25
26
27
# File 'lib/paperclip/validators/attachment_presence_validator.rb', line 23

def validates_attachment_presence(*attr_names)
  options = _merge_attributes(attr_names)
  validates_with AttachmentPresenceValidator, options.dup
  validate_before_processing AttachmentPresenceValidator, options.dup
end

#validates_attachment_size(*attr_names) ⇒ Object

Places ActiveModel validations on the size of the file assigned. The possible options are:

  • in: a Range of bytes (i.e. 1..1.megabyte),

  • less_than: equivalent to :in => 0..options

  • greater_than: equivalent to :in => options..Infinity

  • message: error message to display, use :min and :max as replacements

  • 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.



108
109
110
111
112
# File 'lib/paperclip/validators/attachment_size_validator.rb', line 108

def validates_attachment_size(*attr_names)
  options = _merge_attributes(attr_names)
  validates_with AttachmentSizeValidator, options.dup
  validate_before_processing AttachmentSizeValidator, options.dup
end

#validates_media_type_spoof_detection(*attr_names) ⇒ Object

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.



20
21
22
23
24
# File 'lib/paperclip/validators/media_type_spoof_detection_validator.rb', line 20

def validates_media_type_spoof_detection(*attr_names)
  options = _merge_attributes(attr_names)
  validates_with MediaTypeSpoofDetectionValidator, options.dup
  validate_before_processing MediaTypeSpoofDetectionValidator, options.dup
end