Created
July 17, 2009 19:45
-
-
Save enricob/149244 to your computer and use it in GitHub Desktop.
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
From 2155a0a9b9dfb0dba10264f650727833fe69f76f Mon Sep 17 00:00:00 2001 | |
From: Enrico Bianco <enricob@gmail.com> | |
Date: Fri, 17 Jul 2009 15:43:42 -0400 | |
Subject: [PATCH] subject_class fix from have_attached_file_matcher applied to the other matchers | |
--- | |
.../validate_attachment_content_type_matcher.rb | 6 +++++- | |
.../validate_attachment_presence_matcher.rb | 8 ++++++-- | |
.../matchers/validate_attachment_size_matcher.rb | 6 +++++- | |
3 files changed, 16 insertions(+), 4 deletions(-) | |
diff --git a/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb b/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb | |
index b4e97fd..72f3a00 100644 | |
--- a/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb | |
+++ b/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb | |
@@ -41,12 +41,16 @@ module Paperclip | |
end | |
protected | |
+ | |
+ def subject_class | |
+ @subject.is_a?(Class) ? @subject : @subject.class | |
+ end | |
def allow_types?(types) | |
types.all? do |type| | |
file = StringIO.new(".") | |
file.content_type = type | |
- attachment = @subject.new.attachment_for(@attachment_name) | |
+ attachment = subject_class.new.attachment_for(@attachment_name) | |
attachment.assign(file) | |
attachment.errors[:content_type].nil? | |
end | |
diff --git a/lib/paperclip/matchers/validate_attachment_presence_matcher.rb b/lib/paperclip/matchers/validate_attachment_presence_matcher.rb | |
index 61dc0ea..7808b39 100644 | |
--- a/lib/paperclip/matchers/validate_attachment_presence_matcher.rb | |
+++ b/lib/paperclip/matchers/validate_attachment_presence_matcher.rb | |
@@ -29,15 +29,19 @@ module Paperclip | |
protected | |
+ def subject_class | |
+ @subject.is_a?(Class) ? @subject : @subject.class | |
+ end | |
+ | |
def error_when_not_valid? | |
- @attachment = @subject.new.send(@attachment_name) | |
+ @attachment = subject_class.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 = subject_class.new.send(@attachment_name) | |
@attachment.assign(@file) | |
@attachment.errors[:presence].nil? | |
end | |
diff --git a/lib/paperclip/matchers/validate_attachment_size_matcher.rb b/lib/paperclip/matchers/validate_attachment_size_matcher.rb | |
index f84c479..6a96386 100644 | |
--- a/lib/paperclip/matchers/validate_attachment_size_matcher.rb | |
+++ b/lib/paperclip/matchers/validate_attachment_size_matcher.rb | |
@@ -44,6 +44,10 @@ module Paperclip | |
end | |
protected | |
+ | |
+ def subject_class | |
+ @subject.is_a?(Class) ? @subject : @subject.class | |
+ end | |
def override_method object, method, &replacement | |
(class << object; self; end).class_eval do | |
@@ -54,7 +58,7 @@ module Paperclip | |
def passes_validation_with_size(new_size) | |
file = StringIO.new(".") | |
override_method(file, :size){ new_size } | |
- attachment = @subject.new.attachment_for(@attachment_name) | |
+ attachment = subject_class.new.attachment_for(@attachment_name) | |
attachment.assign(file) | |
attachment.errors[:size].nil? | |
end | |
-- | |
1.6.0.2 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment