Sha256: 45567a21a5e757f71f4552fe5e9dda8b8ebfdc021be67ff2bb43b1da9e5f0c84

Contents?: true

Size: 1.72 KB

Versions: 79

Compression:

Stored size: 1.72 KB

Contents

module Paperclip
  module Shoulda
    module Matchers
      def validate_attachment_content_type name
        ValidateAttachmentContentTypeMatcher.new(name)
      end

      class ValidateAttachmentContentTypeMatcher
        def initialize attachment_name
          @attachment_name = attachment_name
        end

        def allowing *types
          @allowed_types = types.flatten
          self
        end

        def rejecting *types
          @rejected_types = types.flatten
          self
        end

        def matches? subject
          @subject = subject
          @allowed_types && @rejected_types &&
          allowed_types_allowed? && rejected_types_rejected?
        end

        def failure_message
          "Content types #{@allowed_types.join(", ")} should be accepted" +
          " and #{@rejected_types.join(", ")} rejected by #{@attachment_name}"
        end

        def negative_failure_message
          "Content types #{@allowed_types.join(", ")} should be rejected" + 
          " and #{@rejected_types.join(", ")} accepted by #{@attachment_name}"
        end

        def description
          "validate the content types allowed on attachment #{@attachment_name}"
        end

        protected

        def allow_types?(types)
          types.all? do |type|
            file = StringIO.new(".")
            file.content_type = type
            attachment = @subject.new.attachment_for(@attachment_name)
            attachment.assign(file)
            attachment.errors[:content_type].nil?
          end
        end

        def allowed_types_allowed?
          allow_types?(@allowed_types)
        end

        def rejected_types_rejected?
          not allow_types?(@rejected_types)
        end
      end
    end
  end
end

Version data entries

79 entries across 79 versions & 28 rubygems

Version Path
area17-paperclip-2.2.9.1.1 lib/paperclip/matchers/validate_attachment_content_type_matcher.rb
area17-paperclip-2.2.9.2.1 lib/paperclip/matchers/validate_attachment_content_type_matcher.rb
area17-paperclip-2.2.9.2.2 lib/paperclip/matchers/validate_attachment_content_type_matcher.rb
area17-paperclip-2.2.9.2.3 lib/paperclip/matchers/validate_attachment_content_type_matcher.rb
area17-paperclip-2.2.9.2.4 lib/paperclip/matchers/validate_attachment_content_type_matcher.rb
area17-paperclip-2.3.1 lib/paperclip/matchers/validate_attachment_content_type_matcher.rb
beaucollins-paperclip-2.2.7 lib/paperclip/matchers/validate_attachment_content_type_matcher.rb
betelgeuse-paperclip-2.2.8.1 lib/paperclip/matchers/validate_attachment_content_type_matcher.rb
dancroak-paperclip-2.3.1 lib/paperclip/matchers/validate_attachment_content_type_matcher.rb
dejanstrbac-paperclip-2.2.9.2 lib/paperclip/matchers/validate_attachment_content_type_matcher.rb
doxo-paperclip-2.3.1 lib/paperclip/matchers/validate_attachment_content_type_matcher.rb
fermion-paperclip-2.2.8 lib/paperclip/matchers/validate_attachment_content_type_matcher.rb
heythisisnate-paperclip-2.3.0.1 lib/paperclip/matchers/validate_attachment_content_type_matcher.rb
heythisisnate-paperclip-2.3.0.2 lib/paperclip/matchers/validate_attachment_content_type_matcher.rb
heythisisnate-paperclip-2.3.0.5 lib/paperclip/matchers/validate_attachment_content_type_matcher.rb
joshpuetz-paperclip-2.3.0.1 lib/paperclip/matchers/validate_attachment_content_type_matcher.rb
joshpuetz-paperclip-2.3.0.2 lib/paperclip/matchers/validate_attachment_content_type_matcher.rb
joshpuetz-paperclip-2.3.0 lib/paperclip/matchers/validate_attachment_content_type_matcher.rb
korobkov-paperclip-2.3.1.20090921 lib/paperclip/matchers/validate_attachment_content_type_matcher.rb
korobkov-paperclip-2.3.1.200909221442 lib/paperclip/matchers/validate_attachment_content_type_matcher.rb