Class: MiniPaperclip::Shoulda::Matchers::ValidateAttachmentSizeMatcher
- Inherits:
-
Object
- Object
- MiniPaperclip::Shoulda::Matchers::ValidateAttachmentSizeMatcher
- Defined in:
- lib/mini_paperclip/shoulda/matchers/validate_attachment_size_matcher.rb
Instance Method Summary collapse
- #description ⇒ Object
- #failure_message ⇒ Object
- #failure_message_when_negated ⇒ Object (also: #negative_failure_message)
- #human_size ⇒ Object
-
#initialize(attachment_name) ⇒ ValidateAttachmentSizeMatcher
constructor
A new instance of ValidateAttachmentSizeMatcher.
- #less_than(less_than_size) ⇒ Object
- #matches?(subject) ⇒ Boolean
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() = .to_sym @less_than_size = nil end |
Instance Method Details
#description ⇒ Object
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_message ⇒ Object
43 44 45 |
# File 'lib/mini_paperclip/shoulda/matchers/validate_attachment_size_matcher.rb', line 43 def "Attachment :#{@attachment_name} should be less than #{human_size}" end |
#failure_message_when_negated ⇒ Object Also known as: negative_failure_message
47 48 49 |
# File 'lib/mini_paperclip/shoulda/matchers/validate_attachment_size_matcher.rb', line 47 def "Attachment :#{@attachment_name} should not be less than #{human_size}" end |
#human_size ⇒ Object
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
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 |