# File lib/paperclip/matchers/validate_attachment_size_matcher.rb, line 19 def initialize attachment_name @attachment_name = attachment_name end
# File lib/paperclip/matchers/validate_attachment_size_matcher.rb, line 52 def description "validate the size of attachment #{@attachment_name}" end
# File lib/paperclip/matchers/validate_attachment_size_matcher.rb, line 44 def failure_message "Attachment #{@attachment_name} must be between #{@low} and #{@high} bytes" end
# File lib/paperclip/matchers/validate_attachment_size_matcher.rb, line 28 def greater_than size @low = size self end
# File lib/paperclip/matchers/validate_attachment_size_matcher.rb, line 33 def in range @low, @high = range.first, range.last self end
# File lib/paperclip/matchers/validate_attachment_size_matcher.rb, line 23 def less_than size @high = size self end
# File lib/paperclip/matchers/validate_attachment_size_matcher.rb, line 38 def matches? subject @subject = subject @subject = @subject.new if @subject.class == Class lower_than_low? && higher_than_low? && lower_than_high? && higher_than_high? end
# File lib/paperclip/matchers/validate_attachment_size_matcher.rb, line 48 def negative_failure_message "Attachment #{@attachment_name} cannot be between #{@low} and #{@high} bytes" end
# File lib/paperclip/matchers/validate_attachment_size_matcher.rb, line 89 def higher_than_high? @high.nil? || @high == Float::INFINITY || !passes_validation_with_size(@high + 1) end
# File lib/paperclip/matchers/validate_attachment_size_matcher.rb, line 81 def higher_than_low? @low.nil? || passes_validation_with_size(@low + 1) end
# File lib/paperclip/matchers/validate_attachment_size_matcher.rb, line 85 def lower_than_high? @high.nil? || @high == Float::INFINITY || passes_validation_with_size(@high - 1) end
# File lib/paperclip/matchers/validate_attachment_size_matcher.rb, line 77 def lower_than_low? @low.nil? || !passes_validation_with_size(@low - 1) end
# File lib/paperclip/matchers/validate_attachment_size_matcher.rb, line 58 def override_method object, method, &replacement (class << object; self; end).class_eval do define_method(method, &replacement) end end
# File lib/paperclip/matchers/validate_attachment_size_matcher.rb, line 64 def passes_validation_with_size(new_size) file = StringIO.new(".") override_method(file, :size){ new_size } override_method(file, :to_tempfile){ file } @subject.send(@attachment_name).post_processing = false @subject.send(@attachment_name).assign(file) @subject.valid? @subject.errors[:"#{@attachment_name}_file_size"].blank? ensure @subject.send(@attachment_name).post_processing = true end