class MiniPaperclip::Shoulda::Matchers::ValidateAttachmentContentTypeMatcher

Public Class Methods

new(attachment_name) click to toggle source
# File lib/mini_paperclip/shoulda/matchers/validate_attachment_content_type_matcher.rb, line 20
def initialize(attachment_name)
  @attachment_name = attachment_name.to_sym
  @allowings = []
  @rejectings = []
  @fails = []
end

Public Instance Methods

allowing(*allowings) click to toggle source
# File lib/mini_paperclip/shoulda/matchers/validate_attachment_content_type_matcher.rb, line 27
def allowing(*allowings)
  @allowings.concat(allowings)
  self
end
description() click to toggle source
# File lib/mini_paperclip/shoulda/matchers/validate_attachment_content_type_matcher.rb, line 78
def description
  "validate the content types allowed on attachment :#{@attachment_name}"
end
failure_message() click to toggle source
# File lib/mini_paperclip/shoulda/matchers/validate_attachment_content_type_matcher.rb, line 59
def failure_message
  [
    "Attachment :#{@attachment_name} expected to",
    "  allowing #{@allowings}",
    "  rejecting #{@rejectings}",
    "  but failed #{@fails}"
  ].join("\n")
end
failure_message_when_negated() click to toggle source
# File lib/mini_paperclip/shoulda/matchers/validate_attachment_content_type_matcher.rb, line 68
def failure_message_when_negated
  [
    "Attachment :#{@attachment_name} NOT expected to",
    "  allowing #{@allowings}",
    "  rejecting #{@rejectings}",
    "  but failed #{@fails}"
  ].join("\n")
end
Also aliased as: negative_failure_message
matches?(subject) click to toggle source
# File lib/mini_paperclip/shoulda/matchers/validate_attachment_content_type_matcher.rb, line 37
def matches?(subject)
  @subject = subject.class == Class ? subject.new : subject

  begin
    fails = @allowings.reject do |allowing|
      @subject.write_attribute("#{@attachment_name}_content_type", allowing)
      @subject.write_attribute("#{@attachment_name}_updated_at", Time.now)
      @subject.valid?
      @subject.errors[:"#{@attachment_name}_content_type"].empty?
    end
    @fails.concat(fails).empty?
  end && begin
    fails = @rejectings.reject do |rejecting|
      @subject.write_attribute("#{@attachment_name}_content_type", rejecting)
      @subject.write_attribute("#{@attachment_name}_updated_at", Time.now)
      @subject.valid?
      @subject.errors[:"#{@attachment_name}_content_type"].present?
    end
    @fails.concat(fails).empty?
  end
end
negative_failure_message()
rejecting(*rejectings) click to toggle source
# File lib/mini_paperclip/shoulda/matchers/validate_attachment_content_type_matcher.rb, line 32
def rejecting(*rejectings)
  @rejectings.concat(rejectings)
  self
end