Module: ActiveModel::Validations::HelperMethods

Defined in:
lib/file_validators/validators/file_size_validator.rb,
lib/file_validators/validators/file_content_type_validator.rb

Instance Method Summary collapse

Instance Method Details

#validates_file_content_type(*attr_names) ⇒ Object

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

  • allow: Allowed content types. Can be a single content type or an array. Each type can be a String or a Regexp. It can also be a proc/lambda. 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.

  • exclude: Forbidden content types.

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

  • mode: :strict or :relaxed. :strict mode validates the content type based on the actual contents of the files. Thus it can detect media type spoofing. :relaxed validates the content type based on the file name using the mime-types gem. It’s only for sanity check. If mode is not set then it uses form supplied 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.



129
130
131
# File 'lib/file_validators/validators/file_content_type_validator.rb', line 129

def validates_file_content_type(*attr_names)
  validates_with FileContentTypeValidator, _merge_attributes(attr_names)
end

#validates_file_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_or_equal_to: equivalent to :in => 0..options

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

  • less_than: less than a number in bytes

  • greater_than: greater than a number in bytes

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



115
116
117
# File 'lib/file_validators/validators/file_size_validator.rb', line 115

def validates_file_size(*attr_names)
  validates_with FileSizeValidator, _merge_attributes(attr_names)
end