Sha256: baafed65df9d3c31f8aa41375ed22b110c7b983255d791c9b4bd88c1a98941c2

Contents?: true

Size: 1.16 KB

Versions: 3

Compression:

Stored size: 1.16 KB

Contents

module Attachs
  module Extensions
    module ActiveRecord
      module Validations
        extend ActiveSupport::Concern

        class AttachmentContentTypeValidator < AttachmentValidator

          def validate_one(record, attribute, attachment)
            unless attachment.blank?
              if options.has_key?(:with)
                if options[:with] !~ attachment.content_type
                  record.errors.add attribute, :invalid
                  attachment.errors.add :content_type, :not_allowed
                end
              elsif options.has_key?(:in) || options.has_key?(:within)
                list = (options[:in] || options[:within])
                if list.exclude?(attachment.content_type)
                  record.errors.add attribute, :invalid
                  attachment.errors.add :content_type, :not_listed, list: list.to_sentence
                end
              end
            end
          end

        end
        module ClassMethods

          def validates_attachment_content_type_of(*attr_names)
            validates_with AttachmentContentTypeValidator, _merge_attributes(attr_names)
          end

        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
attachs-4.0.0.2 lib/attachs/extensions/active_record/validations/attachment_content_type_validator.rb
attachs-4.0.0.1 lib/attachs/extensions/active_record/validations/attachment_content_type_validator.rb
attachs-4.0.0.0 lib/attachs/extensions/active_record/validations/attachment_content_type_validator.rb