Class: UploadDocumentTool::AttachmentContentTypeValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- UploadDocumentTool::AttachmentContentTypeValidator
- Defined in:
- lib/upload_documents_tool/validators/attachment_content_type_validator.rb
Class Method Summary collapse
Instance Method Summary collapse
- #allowed_types ⇒ Object
- #check_validity! ⇒ Object
- #forbidden_types ⇒ Object
-
#initialize(options) ⇒ AttachmentContentTypeValidator
constructor
A new instance of AttachmentContentTypeValidator.
- #mark_invalid(record, attribute, types) ⇒ Object
- #validate_blacklist(record, attribute, value) ⇒ Object
- #validate_each(record, attribute, value) ⇒ Object
- #validate_whitelist(record, attribute, value) ⇒ Object
Constructor Details
#initialize(options) ⇒ AttachmentContentTypeValidator
Returns a new instance of AttachmentContentTypeValidator.
3 4 5 6 |
# File 'lib/upload_documents_tool/validators/attachment_content_type_validator.rb', line 3 def initialize() [:allow_nil] = true unless .has_key?(:allow_nil) super end |
Class Method Details
.helper_method_name ⇒ Object
8 9 10 |
# File 'lib/upload_documents_tool/validators/attachment_content_type_validator.rb', line 8 def self.helper_method_name :validates_attachment_content_type end |
Instance Method Details
#allowed_types ⇒ Object
45 46 47 |
# File 'lib/upload_documents_tool/validators/attachment_content_type_validator.rb', line 45 def allowed_types [[:content_type]].flatten.compact end |
#check_validity! ⇒ Object
53 54 55 56 57 |
# File 'lib/upload_documents_tool/validators/attachment_content_type_validator.rb', line 53 def check_validity! unless .has_key?(:content_type) || .has_key?(:not) raise ArgumentError, "You must pass in either :content_type or :not to the validator" end end |
#forbidden_types ⇒ Object
49 50 51 |
# File 'lib/upload_documents_tool/validators/attachment_content_type_validator.rb', line 49 def forbidden_types [[:not]].flatten.compact end |
#mark_invalid(record, attribute, types) ⇒ Object
41 42 43 |
# File 'lib/upload_documents_tool/validators/attachment_content_type_validator.rb', line 41 def mark_invalid(record, attribute, types) record.errors.add attribute, :invalid, .merge(:types => types.join(', ')) end |
#validate_blacklist(record, attribute, value) ⇒ Object
35 36 37 38 39 |
# File 'lib/upload_documents_tool/validators/attachment_content_type_validator.rb', line 35 def validate_blacklist(record, attribute, value) if forbidden_types.present? && forbidden_types.any? { |type| type === value } mark_invalid record, attribute, forbidden_types end end |
#validate_each(record, attribute, value) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/upload_documents_tool/validators/attachment_content_type_validator.rb', line 12 def validate_each(record, attribute, value) base_attribute = attribute.to_sym attribute = "#{attribute}_content_type".to_sym value = record.send :read_attribute_for_validation, attribute return if (value.nil? && [:allow_nil]) || (value.blank? && [:allow_blank]) validate_whitelist(record, attribute, value) validate_blacklist(record, attribute, value) if record.errors.include? attribute record.errors[attribute].each do |error| record.errors.add base_attribute, error end end end |
#validate_whitelist(record, attribute, value) ⇒ Object
29 30 31 32 33 |
# File 'lib/upload_documents_tool/validators/attachment_content_type_validator.rb', line 29 def validate_whitelist(record, attribute, value) if allowed_types.present? && allowed_types.none? { |type| type === value } mark_invalid record, attribute, allowed_types end end |