lib/paperclip.rb in paperclip-2.3.3 vs lib/paperclip.rb in paperclip-2.3.4

- old
+ new

@@ -24,22 +24,24 @@ # # => "/users/avatars/4/thumb_me.jpg" # # See the +has_attached_file+ documentation for more details. require 'erb' +require 'digest' require 'tempfile' require 'paperclip/version' require 'paperclip/upfile' require 'paperclip/iostream' require 'paperclip/geometry' require 'paperclip/processor' require 'paperclip/thumbnail' -require 'paperclip/storage' require 'paperclip/interpolations' require 'paperclip/style' require 'paperclip/attachment' +require 'paperclip/storage' require 'paperclip/callback_compatability' +require 'paperclip/command_line' require 'paperclip/railtie' if defined?(Rails.root) && Rails.root Dir.glob(File.join(File.expand_path(Rails.root), "lib", "paperclip_processors", "*.rb")).each do |processor| require processor end @@ -72,18 +74,10 @@ def configure yield(self) if block_given? end - def path_for_command command #:nodoc: - if options[:image_magick_path] - warn("[DEPRECATION] :image_magick_path is deprecated and will be removed. Use :command_path instead") - end - path = [options[:command_path] || options[:image_magick_path], command].compact - File.join(*path) - end - def interpolates key, &block Paperclip::Interpolations[key] = block end # The run method takes a command to execute and an array of parameters @@ -101,54 +95,17 @@ # # This method can log the command being run when # Paperclip.options[:log_command] is set to true (defaults to false). This # will only log if logging in general is set to true as well. def run cmd, *params - options = params.last.is_a?(Hash) ? params.pop : {} - expected_outcodes = options[:expected_outcodes] || [0] - params = quote_command_options(*params).join(" ") - - command = %Q[#{path_for_command(cmd)} #{params}] - command = "#{command} 2>#{bit_bucket}" if Paperclip.options[:swallow_stderr] - Paperclip.log(command) if Paperclip.options[:log_command] - - begin - output = `#{command}` - - raise CommandNotFoundError if $?.exitstatus == 127 - - unless expected_outcodes.include?($?.exitstatus) - raise PaperclipCommandLineError, - "Error while running #{cmd}. Expected return code to be #{expected_outcodes.join(", ")} but was #{$?.exitstatus}", - output - end - rescue Errno::ENOENT => e - raise CommandNotFoundError + if options[:image_magick_path] + Paperclip.log("[DEPRECATION] :image_magick_path is deprecated and will be removed. Use :command_path instead") end - - output + CommandLine.path = options[:command_path] || options[:image_magick_path] + CommandLine.new(cmd, *params).run end - def quote_command_options(*options) - options.map do |option| - option.split("'").map{|m| "'#{m}'" }.join("\\'") - end - end - - def bit_bucket #:nodoc: - File.exists?("/dev/null") ? "/dev/null" : "NUL" - end - - def included base #:nodoc: - base.extend ClassMethods - if base.respond_to?("set_callback") - base.send :include, Paperclip::CallbackCompatability::Rails3 - else - base.send :include, Paperclip::CallbackCompatability::Rails21 - end - end - def processor name #:nodoc: name = name.to_s.camelize processor = Paperclip.const_get(name) unless processor.ancestors.include?(Paperclip::Processor) raise PaperclipError.new("Processor #{name} was not found") @@ -180,19 +137,33 @@ super(msg) @output = output end end + class StorageMethodNotFound < PaperclipError + end + class CommandNotFoundError < PaperclipError end class NotIdentifiedByImageMagickError < PaperclipError #:nodoc: end class InfiniteInterpolationError < PaperclipError #:nodoc: end + module Glue + def self.included base #:nodoc: + base.extend ClassMethods + if base.respond_to?("set_callback") + base.send :include, Paperclip::CallbackCompatability::Rails3 + else + base.send :include, Paperclip::CallbackCompatability::Rails21 + end + end + 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 @@ -300,14 +271,15 @@ range = (min..max) message = options[:message] || "file size must be between :min and :max bytes." 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. def validates_attachment_thumbnails name, options = {} warn('[DEPRECATION] validates_attachment_thumbnail is deprecated. ' + @@ -322,13 +294,13 @@ # be run is this lambda or method returns true. # * +unless+: Same as +if+ but validates if lambda or method returns false. 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 # assigned. The possible options are: # * +content_type+: Allowed content types. Can be a single content type @@ -344,14 +316,15 @@ # * +unless+: Same as +if+ but validates if lambda or method returns false. # NOTE: If you do not specify an [attachment]_content_type field on your # 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.nil? || value.blank?) 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) end end