class UploadDocumentTool::AttachmentContentTypeValidator

Public Class Methods

helper_method_name() click to toggle source
# File lib/upload_documents_tool/validators/attachment_content_type_validator.rb, line 8
def self.helper_method_name
  :validates_attachment_content_type
end
new(options) click to toggle source
Calls superclass method
# File lib/upload_documents_tool/validators/attachment_content_type_validator.rb, line 3
def initialize(options)
  options[:allow_nil] = true unless options.has_key?(:allow_nil)
  super
end

Public Instance Methods

allowed_types() click to toggle source
# File lib/upload_documents_tool/validators/attachment_content_type_validator.rb, line 45
def allowed_types
  [options[:content_type]].flatten.compact
end
check_validity!() click to toggle source
# File lib/upload_documents_tool/validators/attachment_content_type_validator.rb, line 53
def check_validity!
  unless options.has_key?(:content_type) || options.has_key?(:not)
    raise ArgumentError, "You must pass in either :content_type or :not to the validator"
  end
end
forbidden_types() click to toggle source
# File lib/upload_documents_tool/validators/attachment_content_type_validator.rb, line 49
def forbidden_types
  [options[:not]].flatten.compact
end
mark_invalid(record, attribute, types) click to toggle source
# File lib/upload_documents_tool/validators/attachment_content_type_validator.rb, line 41
def mark_invalid(record, attribute, types)
  record.errors.add attribute, :invalid, options.merge(:types => types.join(', '))
end
validate_blacklist(record, attribute, value) click to toggle source
# 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) click to toggle source
# 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? && options[:allow_nil]) || (value.blank? && options[: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) click to toggle source
# 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