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.
has_attached_file and validate_attachment_presence matchers
- Loading branch information
Jon Yurek
committed
Feb 7, 2009
1 parent
ba91710
commit b377053
Showing
7 changed files
with
174 additions
and
29 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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
require 'shoulda_macros/matchers/have_attached_file_matcher' | ||
require 'shoulda_macros/matchers/validate_attachment_presence_matcher' |
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
module Paperclip | ||
module Shoulda | ||
module Matchers | ||
def have_attached_file name | ||
HaveAttachedFileMatcher.new(name) | ||
end | ||
|
||
class HaveAttachedFileMatcher | ||
def initialize attachment_name | ||
@attachment_name = attachment_name | ||
end | ||
|
||
def matches? subject | ||
@subject = subject | ||
responds? && has_column? && included? | ||
end | ||
|
||
def failure_message | ||
"Should have an attachment named #{@attachment_name}" | ||
end | ||
|
||
def negative_failure_message | ||
"Should not have an attachment named #{@attachment_name}" | ||
end | ||
|
||
def description | ||
"have an attachment named #{@attachment_name}" | ||
end | ||
|
||
protected | ||
|
||
def responds? | ||
methods = @subject.instance_methods | ||
methods.include?("#{@attachment_name}") && | ||
methods.include?("#{@attachment_name}=") && | ||
methods.include?("#{@attachment_name}?") | ||
end | ||
|
||
def has_column? | ||
@subject.column_names.include?("#{@attachment_name}_file_name") | ||
end | ||
|
||
def included? | ||
@subject.ancestors.include?(Paperclip::InstanceMethods) | ||
end | ||
end | ||
end | ||
end | ||
end |
48 changes: 48 additions & 0 deletions
48
shoulda_macros/matchers/validate_attachment_presence_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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
module Paperclip | ||
module Shoulda | ||
module Matchers | ||
def validate_attachment_presence name | ||
ValidateAttachmentPresenceMatcher.new(name) | ||
end | ||
|
||
class ValidateAttachmentPresenceMatcher | ||
def initialize attachment_name | ||
@attachment_name = attachment_name | ||
end | ||
|
||
def matches? subject | ||
@subject = subject | ||
error_when_not_valid? && no_error_when_valid? | ||
end | ||
|
||
def failure_message | ||
"Attachment #{@attachment_name} should be required" | ||
end | ||
|
||
def negative_failure_message | ||
"Attachment #{@attachment_name} should not be required" | ||
end | ||
|
||
def description | ||
"require presence of attachment #{@attachment_name}" | ||
end | ||
|
||
protected | ||
|
||
def error_when_not_valid? | ||
@attachment = @subject.new.send(@attachment_name) | ||
@attachment.assign(nil) | ||
not @attachment.errors[:presence].nil? | ||
end | ||
|
||
def no_error_when_valid? | ||
@file = StringIO.new(".") | ||
@attachment = @subject.new.send(@attachment_name) | ||
@attachment.assign(@file) | ||
@attachment.errors[:presence].nil? | ||
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
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
require 'test/helper' | ||
|
||
class HaveAttachedFileMatcherTest < Test::Unit::TestCase | ||
context "have_attached_file" do | ||
setup do | ||
@dummy_class = reset_class "Dummy" | ||
reset_table "dummies" | ||
@matcher = have_attached_file(:avatar) | ||
end | ||
|
||
should "reject a class with no attachment" do | ||
assert_rejects @matcher, @dummy_class | ||
end | ||
|
||
should "accept a class with an attachment" do | ||
modify_table("dummies"){|d| d.string :avatar_file_name } | ||
@dummy_class.has_attached_file :avatar | ||
assert_accepts @matcher, @dummy_class | ||
end | ||
end | ||
end |
21 changes: 21 additions & 0 deletions
21
test/matchers/validate_attachment_presence_matcher_test.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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
require 'test/helper' | ||
|
||
class ValidateAttachmentPresenceMatcherTest < Test::Unit::TestCase | ||
context "validate_attachment_presence" do | ||
setup do | ||
reset_table("dummies"){|d| d.string :avatar_file_name } | ||
@dummy_class = reset_class "Dummy" | ||
@dummy_class.has_attached_file :avatar | ||
@matcher = validate_attachment_presence(:avatar) | ||
end | ||
|
||
should "reject a class with no validation" do | ||
assert_rejects @matcher, @dummy_class | ||
end | ||
|
||
should "accept a class with a validation" do | ||
@dummy_class.validates_attachment_presence :avatar | ||
assert_accepts @matcher, @dummy_class | ||
end | ||
end | ||
end |