Class: MiniPaperclip::Shoulda::Matchers::ValidateAttachmentSizeMatcher

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

Instance Method Summary collapse

Constructor Details

#initialize(attachment_name) ⇒ ValidateAttachmentSizeMatcher

Returns a new instance of ValidateAttachmentSizeMatcher.



17
18
19
20
# File 'lib/mini_paperclip/shoulda/matchers/validate_attachment_size_matcher.rb', line 17

def initialize(attachment_name)
  @attachment_name = attachment_name.to_sym
  @less_than_size = nil
end

Instance Method Details

#descriptionObject



52
53
54
# File 'lib/mini_paperclip/shoulda/matchers/validate_attachment_size_matcher.rb', line 52

def description
  "validate the size of attachment :#{@attachment_name}"
end

#failure_messageObject



43
44
45
# File 'lib/mini_paperclip/shoulda/matchers/validate_attachment_size_matcher.rb', line 43

def failure_message
  "Attachment :#{@attachment_name} should be less than #{human_size}"
end

#failure_message_when_negatedObject Also known as: negative_failure_message



47
48
49
# File 'lib/mini_paperclip/shoulda/matchers/validate_attachment_size_matcher.rb', line 47

def failure_message_when_negated
  "Attachment :#{@attachment_name} should not be less than #{human_size}"
end

#human_sizeObject



56
57
58
# File 'lib/mini_paperclip/shoulda/matchers/validate_attachment_size_matcher.rb', line 56

def human_size
  ActiveSupport::NumberHelper.number_to_human_size(@less_than_size)
end

#less_than(less_than_size) ⇒ Object



22
23
24
25
# File 'lib/mini_paperclip/shoulda/matchers/validate_attachment_size_matcher.rb', line 22

def less_than(less_than_size)
  @less_than_size = less_than_size
  self
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/mini_paperclip/shoulda/matchers/validate_attachment_size_matcher.rb', line 27

def matches?(subject)
  @subject = subject.class == Class ? subject.new : subject

  begin
    @subject.write_attribute("#{@attachment_name}_file_size", @less_than_size - 1)
    @subject.write_attribute("#{@attachment_name}_updated_at", Time.now)
    @subject.valid?
    @subject.errors[:"#{@attachment_name}_file_size"].empty?
  end && begin
    @subject.write_attribute("#{@attachment_name}_file_size", @less_than_size)
    @subject.write_attribute("#{@attachment_name}_updated_at", Time.now)
    @subject.valid?
    @subject.errors[:"#{@attachment_name}_file_size"].present?
  end
end