class Paperclip::Shoulda::Matchers::ValidateAttachmentContentTypeMatcher

def accepted_types_and_failures

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

def allowed_types_allowed?

def allowed_types_allowed?
  @missing_allowed_types ||= @allowed_types.reject { |type| type_allowed?(type) }
  @missing_allowed_types.none?
end

def allowing *types

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

def description

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

def expected_attachment

def expected_attachment
  "Expected #{@attachment_name}:\n"
end

def failure_message

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

def initialize attachment_name

def initialize attachment_name
  @attachment_name = attachment_name
  @allowed_types = []
  @rejected_types = []
end

def matches? subject

def matches? subject
  @subject = subject
  @subject = @subject.new if @subject.class == Class
  @allowed_types && @rejected_types &&
  allowed_types_allowed? && rejected_types_rejected?
end

def rejected_types_and_failures

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

def rejected_types_rejected?

def rejected_types_rejected?
  @missing_rejected_types ||= @rejected_types.select { |type| type_allowed?(type) }
  @missing_rejected_types.none?
end

def rejecting *types

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

def type_allowed?(type)

def type_allowed?(type)
  @subject.send("#{@attachment_name}_content_type=", type)
  @subject.valid?
  @subject.errors[:"#{@attachment_name}_content_type"].blank?
end