Class: Paperclip::Validators::AttachmentContentTypeValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- Paperclip::Validators::AttachmentContentTypeValidator
- Defined in:
- lib/paperclip/validators/attachment_content_type_validator.rb
Instance Method Summary collapse
- #check_validity! ⇒ Object
-
#initialize(options) ⇒ AttachmentContentTypeValidator
constructor
A new instance of AttachmentContentTypeValidator.
- #validate_each(record, attribute, value) ⇒ Object
Constructor Details
#initialize(options) ⇒ AttachmentContentTypeValidator
Returns a new instance of AttachmentContentTypeValidator.
4 5 6 7 |
# File 'lib/paperclip/validators/attachment_content_type_validator.rb', line 4 def initialize() [:allow_nil] = true unless .has_key?(:allow_nil) super end |
Instance Method Details
#check_validity! ⇒ Object
23 24 25 26 27 |
# File 'lib/paperclip/validators/attachment_content_type_validator.rb', line 23 def check_validity! unless .has_key?(:content_type) raise ArgumentError, "You must pass in :content_type to the validator" end end |
#validate_each(record, attribute, value) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/paperclip/validators/attachment_content_type_validator.rb', line 9 def validate_each(record, attribute, value) attribute = "#{attribute}_content_type".to_sym value = record.send(:read_attribute_for_validation, attribute) allowed_types = [[:content_type]].flatten return if (value.nil? && [:allow_nil]) || (value.blank? && [:allow_blank]) if allowed_types.none? { |type| type === value } record.errors.add(attribute, :invalid, .merge( :types => allowed_types.join(', ') )) end end |