Class: MiniDefender::Rules::Image

Inherits:
MiniDefender::Rule show all
Defined in:
lib/mini_defender/rules/image.rb

Constant Summary collapse

MIMES =
%w[image/jpeg image/png image/gif image/bmp image/png image/svg+xml image/webp]

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from MiniDefender::Rule

#active?, available?, #bails?, #coerce, #default_value, #defaults?, #error_message, #excluded?, #force_coerce?, #implicit?, make, #priority, #stops?, #with_message

Class Method Details

.signatureObject



9
10
11
# File 'lib/mini_defender/rules/image.rb', line 9

def self.signature
  'image'
end

Instance Method Details

#message(attribute, value, validator) ⇒ Object



20
21
22
# File 'lib/mini_defender/rules/image.rb', line 20

def message(attribute, value, validator)
  "The field should be an image of type jpg, jpeg, png, bmp, gif, svg, or webp."
end

#passes?(attribute, value, validator) ⇒ Boolean

Returns:



13
14
15
16
17
18
# File 'lib/mini_defender/rules/image.rb', line 13

def passes?(attribute, value, validator)
  content_type = Marcel::MimeType.for(value.read)
  value.rewind

  value.is_a?(ActionDispatch::Http::UploadedFile) && MIMES.include?(content_type)
end