| Class | Paperclip::Shoulda::Matchers::ValidateAttachmentSizeMatcher |
| In: |
lib/paperclip/matchers/validate_attachment_size_matcher.rb
|
| Parent: | Object |
# File lib/paperclip/matchers/validate_attachment_size_matcher.rb, line 19
19: def initialize attachment_name
20: @attachment_name = attachment_name
21: @low, @high = 0, (1.0/0)
22: end
# File lib/paperclip/matchers/validate_attachment_size_matcher.rb, line 53
53: def description
54: "validate the size of attachment #{@attachment_name}"
55: end
# File lib/paperclip/matchers/validate_attachment_size_matcher.rb, line 45
45: def failure_message
46: "Attachment #{@attachment_name} must be between #{@low} and #{@high} bytes"
47: end
# File lib/paperclip/matchers/validate_attachment_size_matcher.rb, line 29
29: def greater_than size
30: @low = size
31: self
32: end
# File lib/paperclip/matchers/validate_attachment_size_matcher.rb, line 34
34: def in range
35: @low, @high = range.first, range.last
36: self
37: end
# File lib/paperclip/matchers/validate_attachment_size_matcher.rb, line 24
24: def less_than size
25: @high = size
26: self
27: end
# File lib/paperclip/matchers/validate_attachment_size_matcher.rb, line 39
39: def matches? subject
40: @subject = subject
41: @subject = @subject.class unless Class === @subject
42: lower_than_low? && higher_than_low? && lower_than_high? && higher_than_high?
43: end
# File lib/paperclip/matchers/validate_attachment_size_matcher.rb, line 49
49: def negative_failure_message
50: "Attachment #{@attachment_name} cannot be between #{@low} and #{@high} bytes"
51: end
# File lib/paperclip/matchers/validate_attachment_size_matcher.rb, line 88
88: def higher_than_high?
89: return true if @high == (1.0/0)
90: not passes_validation_with_size(@high + 1)
91: end
# File lib/paperclip/matchers/validate_attachment_size_matcher.rb, line 79
79: def higher_than_low?
80: passes_validation_with_size(@low + 1)
81: end
# File lib/paperclip/matchers/validate_attachment_size_matcher.rb, line 83
83: def lower_than_high?
84: return true if @high == (1.0/0)
85: passes_validation_with_size(@high - 1)
86: end
# File lib/paperclip/matchers/validate_attachment_size_matcher.rb, line 75
75: def lower_than_low?
76: not passes_validation_with_size(@low - 1)
77: end
# File lib/paperclip/matchers/validate_attachment_size_matcher.rb, line 59
59: def override_method object, method, &replacement
60: (class << object; self; end).class_eval do
61: define_method(method, &replacement)
62: end
63: end
# File lib/paperclip/matchers/validate_attachment_size_matcher.rb, line 65
65: def passes_validation_with_size(new_size)
66: file = StringIO.new(".")
67: override_method(file, :size){ new_size }
68: override_method(file, :to_tempfile){ file }
69:
70: (subject = @subject.new).send(@attachment_name).assign(file)
71: subject.valid?
72: subject.errors["#{@attachment_name}_file_size""#{@attachment_name}_file_size"].blank?
73: end