Class: Paperclip::Shoulda::Matchers::ValidateAttachmentContentTypeMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/paperclip/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
# File 'lib/paperclip/matchers/validate_attachment_content_type_matcher.rb', line 20

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

Instance Method Details

#allowing(*types) ⇒ Object



26
27
28
29
# File 'lib/paperclip/matchers/validate_attachment_content_type_matcher.rb', line 26

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

#descriptionObject



51
52
53
# File 'lib/paperclip/matchers/validate_attachment_content_type_matcher.rb', line 51

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

#failure_messageObject



43
44
45
46
47
48
49
# File 'lib/paperclip/matchers/validate_attachment_content_type_matcher.rb', line 43

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) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
# File 'lib/paperclip/matchers/validate_attachment_content_type_matcher.rb', line 36

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) ⇒ Object



31
32
33
34
# File 'lib/paperclip/matchers/validate_attachment_content_type_matcher.rb', line 31

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