lib/paperclip.rb in paperclip-2.1.0 vs lib/paperclip.rb in paperclip-2.1.2

- old
+ new

@@ -34,11 +34,11 @@ require 'paperclip/attachment' # The base module that gets included in ActiveRecord::Base. module Paperclip - VERSION = "2.1.0" + VERSION = "2.1.2" class << self # Provides configurability to Paperclip. There are a number of options available, such as: # * whiny_thumbnails: Will raise an error if Paperclip cannot process thumbnails of # an uploaded image. Defaults to true. @@ -63,10 +63,13 @@ end class PaperclipError < StandardError #:nodoc: end + class NotIdentifiedByImageMagickError < PaperclipError #:nodoc: + end + module ClassMethods # +has_attached_file+ gives the class it is called on an attribute that maps to a file. This # is typically a file stored somewhere on the filesystem and has been uploaded by a user. # The attribute returns a Paperclip::Attachment object which handles the management of # that file. The intent is to make the attachment as much like a normal attribute. The @@ -129,11 +132,11 @@ define_method "#{name}=" do |file| attachment_for(name).assign(file) end define_method "#{name}?" do - ! attachment_for(name).file.nil? + ! attachment_for(name).original_filename.blank? end validates_each(name) do |record, attr, value| value.send(:flush_errors) end @@ -142,28 +145,60 @@ # Places ActiveRecord-style validations on the size of the file assigned. The # possible options are: # * +in+: a Range of bytes (i.e. +1..1.megabyte+), # * +less_than+: equivalent to :in => 0..options[:less_than] # * +greater_than+: equivalent to :in => options[:greater_than]..Infinity + # * +message+: error message to display, use :min and :max as replacements def validates_attachment_size name, options = {} attachment_definitions[name][:validations] << lambda do |attachment, instance| unless options[:greater_than].nil? options[:in] = (options[:greater_than]..(1/0)) # 1/0 => Infinity end unless options[:less_than].nil? options[:in] = (0..options[:less_than]) end - unless options[:in].include? instance[:"#{name}_file_size"].to_i - "file size is not between #{options[:in].first} and #{options[:in].last} bytes." + unless attachment.original_filename.blank? || options[:in].include?(instance[:"#{name}_file_size"].to_i) + min = options[:in].first + max = options[:in].last + + if options[:message] + options[:message].gsub(/:min/, min.to_s).gsub(/:max/, max.to_s) + else + "file size is not between #{min} and #{max} bytes." + end end end end + # Adds errors if thumbnail creation fails. The same as specifying :whiny_thumbnails => true. + def validates_attachment_thumbnails name, options = {} + attachment_definitions[name][:whiny_thumbnails] = true + end + # Places ActiveRecord-style validations on the presence of a file. - def validates_attachment_presence name + def validates_attachment_presence name, options = {} attachment_definitions[name][:validations] << lambda do |attachment, instance| - if attachment.file.nil? || !File.exist?(attachment.file.path) - "must be set." + if attachment.original_filename.blank? + options[:message] || "must be set." + end + end + end + + # Places ActiveRecord-style validations on the content type of the file assigned. The + # possible options are: + # * +content_type+: Allowed content types. Can be a single content type or an array. Allows all by default. + # * +message+: The message to display when the uploaded file has an invalid content type. + def validates_attachment_content_type name, options = {} + attachment_definitions[name][:validations] << lambda do |attachment, instance| + valid_types = [options[:content_type]].flatten + + unless attachment.original_filename.nil? + unless options[:content_type].blank? + content_type = instance[:"#{name}_content_type"] + unless valid_types.any?{|t| t === content_type } + options[:message] || ActiveRecord::Errors.default_error_messages[:inclusion] + end + end end end end # Returns the attachment definitions defined by each call to has_attached_file.