class Paperclip::Shoulda::Matchers::ValidateAttachmentContentTypeMatcher

Public Class Methods

new(attachment_name) click to toggle source
# File lib/paperclip/matchers/validate_attachment_content_type_matcher.rb, line 18
def initialize attachment_name
  @attachment_name = attachment_name
  @allowed_types = []
  @rejected_types = []
end

Public Instance Methods

allowing(*types) click to toggle source
# File lib/paperclip/matchers/validate_attachment_content_type_matcher.rb, line 24
def allowing *types
  @allowed_types = types.flatten
  self
end
description() click to toggle source
# File lib/paperclip/matchers/validate_attachment_content_type_matcher.rb, line 49
def description
  "validate the content types allowed on attachment #{@attachment_name}"
end
failure_message() click to toggle source
# File lib/paperclip/matchers/validate_attachment_content_type_matcher.rb, line 41
def failure_message
  "#{expected_attachment}\n".tap do |message|
    message << accepted_types_and_failures.to_s
    message << "\n\n" if @allowed_types.present? && @rejected_types.present?
    message << rejected_types_and_failures.to_s
  end
end
matches?(subject) click to toggle source
# File lib/paperclip/matchers/validate_attachment_content_type_matcher.rb, line 34
def matches? subject
  @subject = subject
  @subject = @subject.new if @subject.class == Class
  @allowed_types && @rejected_types &&
  allowed_types_allowed? && rejected_types_rejected?
end
rejecting(*types) click to toggle source
# File lib/paperclip/matchers/validate_attachment_content_type_matcher.rb, line 29
def rejecting *types
  @rejected_types = types.flatten
  self
end

Protected Instance Methods

accepted_types_and_failures() click to toggle source
# File lib/paperclip/matchers/validate_attachment_content_type_matcher.rb, line 55
def accepted_types_and_failures
  if @allowed_types.present?
    "Accept content types: #{@allowed_types.join(", ")}\n".tap do |message|
      if @missing_allowed_types.present?
        message << "  #{@missing_allowed_types.join(", ")} were rejected."
      else
        message << "  All were accepted successfully."
      end
    end
  end
end
allowed_types_allowed?() click to toggle source
# File lib/paperclip/matchers/validate_attachment_content_type_matcher.rb, line 88
def allowed_types_allowed?
  @missing_allowed_types ||= @allowed_types.reject { |type| type_allowed?(type) }
  @missing_allowed_types.none?
end
expected_attachment() click to toggle source
# File lib/paperclip/matchers/validate_attachment_content_type_matcher.rb, line 78
def expected_attachment
  "Expected #{@attachment_name}:\n"
end
rejected_types_and_failures() click to toggle source
# File lib/paperclip/matchers/validate_attachment_content_type_matcher.rb, line 66
def rejected_types_and_failures
  if @rejected_types.present?
    "Reject content types: #{@rejected_types.join(", ")}\n".tap do |message|
      if @missing_rejected_types.present?
        message << "  #{@missing_rejected_types.join(", ")} were accepted."
      else
        message << "  All were rejected successfully."
      end
    end
  end
end
rejected_types_rejected?() click to toggle source
# File lib/paperclip/matchers/validate_attachment_content_type_matcher.rb, line 93
def rejected_types_rejected?
  @missing_rejected_types ||= @rejected_types.select { |type| type_allowed?(type) }
  @missing_rejected_types.none?
end
type_allowed?(type) click to toggle source
# File lib/paperclip/matchers/validate_attachment_content_type_matcher.rb, line 82
def type_allowed?(type)
  @subject.send("#{@attachment_name}_content_type=", type)
  @subject.valid?
  @subject.errors[:"#{@attachment_name}_content_type"].blank?
end