Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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

  • Loading branch information...
commit 3b3f8eaea1941104a0c35bcde1c24d7f6dd528b9 1 parent e7410db
@jyurek jyurek authored
Showing with 15 additions and 12 deletions.
  1. +13 −11 lib/paperclip.rb
  2. +2 −1  test/paperclip_test.rb
View
24 lib/paperclip.rb
@@ -302,10 +302,11 @@ def validates_attachment_size name, options = {}
message = message.gsub(/:min/, min.to_s).gsub(/:max/, max.to_s)
validates_inclusion_of :"#{name}_file_size",
- :in => range,
- :message => message,
- :if => options[:if],
- :unless => options[:unless]
+ :in => range,
+ :message => message,
+ :if => options[:if],
+ :unless => options[:unless],
+ :allow_nil => true
end
# Adds errors if thumbnail creation fails. The same as specifying :whiny_thumbnails => true.
@@ -324,9 +325,9 @@ def validates_attachment_thumbnails name, options = {}
def validates_attachment_presence name, options = {}
message = options[:message] || "must be set."
validates_presence_of :"#{name}_file_name",
- :message => message,
- :if => options[:if],
- :unless => options[:unless]
+ :message => message,
+ :if => options[:if],
+ :unless => options[:unless]
end
# Places ActiveRecord-style validations on the content type of the file
@@ -346,11 +347,12 @@ def validates_attachment_presence name, options = {}
# model, content_type validation will work _ONLY upon assignment_ and
# re-validation after the instance has been reloaded will always succeed.
def validates_attachment_content_type name, options = {}
- types = [options.delete(:content_type)].flatten
- validates_each(:"#{name}_content_type", options) do |record, attr, value|
- unless types.any?{|t| t === value }
+ validation_options = options.dup
+ allowed_types = [validation_options[:content_type]].flatten
+ 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
- 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)
else
record.errors.add(:"#{name}_content_type", :inclusion, :default => options[:message], :value => value)
View
3  test/paperclip_test.rb
@@ -245,6 +245,7 @@ class ::SubDummy < Dummy; end
def self.should_validate validation, options, valid_file, invalid_file
context "with #{validation} validation and #{options.inspect} options" do
setup do
+ rebuild_class
Dummy.send(:"validates_attachment_#{validation}", :avatar, options)
@dummy = Dummy.new
end
@@ -259,7 +260,7 @@ def self.should_validate validation, options, valid_file, invalid_file
end
else
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
Please sign in to comment.
Something went wrong with that request. Please try again.