Commit 3b3f8eae by Jon Yurek

Fixed size and content type validations to not care about presence.

parent e7410db7
...@@ -305,7 +305,8 @@ module Paperclip ...@@ -305,7 +305,8 @@ module Paperclip
:in => range, :in => range,
:message => message, :message => message,
:if => options[:if], :if => options[:if],
:unless => options[:unless] :unless => options[:unless],
:allow_nil => true
end end
# Adds errors if thumbnail creation fails. The same as specifying :whiny_thumbnails => true. # Adds errors if thumbnail creation fails. The same as specifying :whiny_thumbnails => true.
...@@ -346,11 +347,12 @@ module Paperclip ...@@ -346,11 +347,12 @@ module Paperclip
# model, content_type validation will work _ONLY upon assignment_ and # model, content_type validation will work _ONLY upon assignment_ and
# re-validation after the instance has been reloaded will always succeed. # re-validation after the instance has been reloaded will always succeed.
def validates_attachment_content_type name, options = {} def validates_attachment_content_type name, options = {}
types = [options.delete(:content_type)].flatten validation_options = options.dup
validates_each(:"#{name}_content_type", options) do |record, attr, value| allowed_types = [validation_options[:content_type]].flatten
unless types.any?{|t| t === value } validates_each(:"#{name}_content_type", validation_options) do |record, attr, value|
if !allowed_types.any?{|t| t === value } && value.present?
if record.errors.method(:add).arity == -2 if record.errors.method(:add).arity == -2
message = options[:message] || "is not one of #{types.join(", ")}" message = options[:message] || "is not one of #{allowed_types.join(", ")}"
record.errors.add(:"#{name}_content_type", message) record.errors.add(:"#{name}_content_type", message)
else else
record.errors.add(:"#{name}_content_type", :inclusion, :default => options[:message], :value => value) record.errors.add(:"#{name}_content_type", :inclusion, :default => options[:message], :value => value)
......
...@@ -245,6 +245,7 @@ class PaperclipTest < Test::Unit::TestCase ...@@ -245,6 +245,7 @@ class PaperclipTest < Test::Unit::TestCase
def self.should_validate validation, options, valid_file, invalid_file def self.should_validate validation, options, valid_file, invalid_file
context "with #{validation} validation and #{options.inspect} options" do context "with #{validation} validation and #{options.inspect} options" do
setup do setup do
rebuild_class
Dummy.send(:"validates_attachment_#{validation}", :avatar, options) Dummy.send(:"validates_attachment_#{validation}", :avatar, options)
@dummy = Dummy.new @dummy = Dummy.new
end end
...@@ -259,7 +260,7 @@ class PaperclipTest < Test::Unit::TestCase ...@@ -259,7 +260,7 @@ class PaperclipTest < Test::Unit::TestCase
end end
else else
should "not have an error on the attachment" do should "not have an error on the attachment" do
assert @dummy.errors[:avatar_file_name].blank?, @dummy.errors.full_messages.join(", ") assert @dummy.errors.blank?, @dummy.errors.full_messages.join(", ")
end end
end end
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment