Class: MiniPaperclip::Shoulda::Matchers::ValidateAttachmentContentTypeMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/mini_paperclip/shoulda/matchers/validate_attachment_content_type_matcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(attachment_name) ⇒ ValidateAttachmentContentTypeMatcher

Returns a new instance of ValidateAttachmentContentTypeMatcher.



20
21
22
23
24
25
# 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

Instance Method Details

#allowing(*allowings) ⇒ Object



27
28
29
30
# File 'lib/mini_paperclip/shoulda/matchers/validate_attachment_content_type_matcher.rb', line 27

def allowing(*allowings)
  @allowings.concat(allowings)
  self
end

#descriptionObject



78
79
80
# 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_messageObject



59
60
61
62
63
64
65
66
# 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_negatedObject Also known as: negative_failure_message



68
69
70
71
72
73
74
75
# 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

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 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

#rejecting(*rejectings) ⇒ Object



32
33
34
35
# File 'lib/mini_paperclip/shoulda/matchers/validate_attachment_content_type_matcher.rb', line 32

def rejecting(*rejectings)
  @rejectings.concat(rejectings)
  self
end