forked from thoughtbot/paperclip
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jon Yurek
committed
Feb 8, 2009
1 parent
9e73929
commit f78528a
Showing
4 changed files
with
137 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,3 +1,4 @@ | |||
require 'shoulda_macros/matchers/have_attached_file_matcher' | require 'shoulda_macros/matchers/have_attached_file_matcher' | ||
require 'shoulda_macros/matchers/validate_attachment_presence_matcher' | require 'shoulda_macros/matchers/validate_attachment_presence_matcher' | ||
require 'shoulda_macros/matchers/validate_attachment_content_type_matcher' | require 'shoulda_macros/matchers/validate_attachment_content_type_matcher' | ||
require 'shoulda_macros/matchers/validate_attachment_size_matcher' |
83 changes: 83 additions & 0 deletions
83
shoulda_macros/matchers/validate_attachment_size_matcher.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,83 @@ | |||
module Paperclip | |||
module Shoulda | |||
module Matchers | |||
def validate_attachment_size name | |||
ValidateAttachmentSizeMatcher.new(name) | |||
end | |||
|
|||
class ValidateAttachmentSizeMatcher | |||
def initialize attachment_name | |||
@attachment_name = attachment_name | |||
@low, @high = 0, (1.0/0) | |||
end | |||
|
|||
def less_than size | |||
@high = size | |||
self | |||
end | |||
|
|||
def greater_than size | |||
@low = size | |||
self | |||
end | |||
|
|||
def in range | |||
@low, @high = range.first, range.last | |||
self | |||
end | |||
|
|||
def matches? subject | |||
@subject = subject | |||
lower_than_low? && higher_than_low? && lower_than_high? && higher_than_high? | |||
end | |||
|
|||
def failure_message | |||
"Attachment #{@attachment_name} must be between #{@low} and #{@high} bytes" | |||
end | |||
|
|||
def negative_failure_message | |||
"Attachment #{@attachment_name} cannot be between #{@low} and #{@high} bytes" | |||
end | |||
|
|||
def description | |||
"validate the size of attachment #{@attachment_name}" | |||
end | |||
|
|||
protected | |||
|
|||
def override_method object, method, &replacement | |||
(class << object; self; end).class_eval do | |||
define_method(method, &replacement) | |||
end | |||
end | |||
|
|||
def passes_validation_with_size(new_size) | |||
file = StringIO.new(".") | |||
override_method(file, :size){ new_size } | |||
attachment = @subject.new.attachment_for(@attachment_name) | |||
attachment.assign(file) | |||
attachment.errors[:size].nil? | |||
end | |||
|
|||
def lower_than_low? | |||
not passes_validation_with_size(@low - 1) | |||
end | |||
|
|||
def higher_than_low? | |||
passes_validation_with_size(@low + 1) | |||
end | |||
|
|||
def lower_than_high? | |||
return true if @high == (1.0/0) | |||
passes_validation_with_size(@high - 1) | |||
end | |||
|
|||
def higher_than_high? | |||
return true if @high == (1.0/0) | |||
not passes_validation_with_size(@high + 1) | |||
end | |||
end | |||
end | |||
end | |||
end | |||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,50 @@ | |||
require 'test/helper' | |||
|
|||
class ValidateAttachmentSizeMatcherTest < Test::Unit::TestCase | |||
context "validate_attachment_size" do | |||
setup do | |||
reset_table("dummies") do |d| | |||
d.string :avatar_file_name | |||
end | |||
@dummy_class = reset_class "Dummy" | |||
@dummy_class.has_attached_file :avatar | |||
end | |||
|
|||
context "of limited size" do | |||
setup{ @matcher = validate_attachment_size(:avatar).in(256..1024) } | |||
|
|||
should "reject a class with no validation" do | |||
assert_rejects @matcher, @dummy_class | |||
end | |||
|
|||
should "reject a class with a validation that's too high" do | |||
@dummy_class.validates_attachment_size :avatar, :in => 256..2048 | |||
assert_rejects @matcher, @dummy_class | |||
end | |||
|
|||
should "reject a class with a validation that's too low" do | |||
@dummy_class.validates_attachment_size :avatar, :in => 0..1024 | |||
assert_rejects @matcher, @dummy_class | |||
end | |||
|
|||
should "accept a class with a validation that matches" do | |||
@dummy_class.validates_attachment_size :avatar, :in => 256..1024 | |||
assert_accepts @matcher, @dummy_class | |||
end | |||
end | |||
|
|||
context "validates_attachment_size with infinite range" do | |||
setup{ @matcher = validate_attachment_size(:avatar) } | |||
|
|||
should "accept a class with an upper limit" do | |||
@dummy_class.validates_attachment_size :avatar, :less_than => 1 | |||
assert_accepts @matcher, @dummy_class | |||
end | |||
|
|||
should "accept a class with no upper limit" do | |||
@dummy_class.validates_attachment_size :avatar, :greater_than => 1 | |||
assert_accepts @matcher, @dummy_class | |||
end | |||
end | |||
end | |||
end |