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
-
#validates_file_content_type(*attr_names) ⇒ Object
Places ActiveModel validations on the content type of the file assigned.
-
#validates_file_size(*attr_names) ⇒ Object
Places ActiveModel validations on the size of the file assigned.
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.tool: :file, :fastimage, :filemagic, :mimemagic, :marcel, :mime_types, :mini_mime You can choose a different built-in MIME type analyzer By default supplied content type is used to determine the MIME type This option have precedence over mode optionif: A lambda or name of an instance method. Validation will only be run is this lambda or method returns true.unless: Same asifbut validates if lambda or method returns false.
121 122 123 |
# File 'lib/file_validators/validators/file_content_type_validator.rb', line 121 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..optionsgreater_than_or_equal_to: equivalent to :in => options..Infinityless_than: less than a number in bytesgreater_than: greater than a number in bytesmessage: error message to display, use :min and :max as replacementsif: A lambda or name of an instance method. Validation will only be run if this lambda or method returns true.unless: Same asifbut validates if lambda or method returns false.
140 141 142 |
# File 'lib/file_validators/validators/file_size_validator.rb', line 140 def validates_file_size(*attr_names) validates_with FileSizeValidator, _merge_attributes(attr_names) end |