lib/paperclip.rb in paperclip-2.2.2 vs lib/paperclip.rb in paperclip-2.2.3
- old
+ new
@@ -41,11 +41,11 @@
# The base module that gets included in ActiveRecord::Base. See the
# documentation for Paperclip::ClassMethods for more useful information.
module Paperclip
- VERSION = "2.2.2"
+ VERSION = "2.2.3"
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.
@@ -58,35 +58,40 @@
def options
@options ||= {
:whiny_thumbnails => true,
:image_magick_path => nil,
:command_path => nil,
- :log => true
+ :log => true,
+ :swallow_stderr => true
}
end
def path_for_command command #:nodoc:
if options[:image_magick_path]
- ActiveSupport::Deprecation.warn(":image_magick_path is deprecated and "+
- "will be removed. Use :command_path "+
- "instead")
+ warn("[DEPRECATION] :image_magick_path is deprecated and will be removed. Use :command_path instead")
end
- path = [options[:image_magick_path] || options[:command_path], command].compact
+ path = [options[:command_path] || options[:image_magick_path], command].compact
File.join(*path)
end
+ def interpolates key, &block
+ Paperclip::Attachment.interpolations[key] = block
+ end
+
# The run method takes a command to execute and a string of parameters
# that get passed to it. The command is prefixed with the :command_path
# option from Paperclip.options. If you have many commands to run and
# they are in different paths, the suggested course of action is to
# symlink them so they are all in the same directory.
#
# If the command returns with a result code that is not one of the
# expected_outcodes, a PaperclipCommandLineError will be raised. Generally
# a code of 0 is expected, but a list of codes may be passed if necessary.
def run cmd, params = "", expected_outcodes = 0
- output = `#{%Q[#{path_for_command(cmd)} #{params} 2>#{bit_bucket}].gsub(/\s+/, " ")}`
+ command = %Q<#{%Q[#{path_for_command(cmd)} #{params}].gsub(/\s+/, " ")}>
+ command = "#{command} 2>#{bit_bucket}" if Paperclip.options[:swallow_stderr]
+ output = `#{command}`
unless [expected_outcodes].flatten.include?($?.exitstatus)
raise PaperclipCommandLineError, "Error while running #{cmd}"
end
output
end
@@ -202,11 +207,12 @@
define_method "#{name}?" do
attachment_for(name).file?
end
validates_each(name) do |record, attr, value|
- value.send(:flush_errors) unless value.valid?
+ attachment = record.attachment_for(name)
+ attachment.send(:flush_errors) unless attachment.valid?
end
end
# Places ActiveRecord-style validations on the size of the file assigned. The
# possible options are:
@@ -248,17 +254,20 @@
# may not expect. For example, JPEG images are given image/pjpeg and
# PNGs are image/x-png, so keep that in mind when determining how you
# match. Allows all by default.
# * +message+: The message to display when the uploaded file has an invalid
# content type.
+ # 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 = {}
attachment_definitions[name][:validations][:content_type] = lambda do |attachment, instance|
valid_types = [options[:content_type]].flatten
unless attachment.original_filename.blank?
unless valid_types.blank?
content_type = attachment.instance_read(:content_type)
- unless valid_types.any?{|t| t === content_type }
+ unless valid_types.any?{|t| content_type.nil? || t === content_type }
options[:message] || "is not one of the allowed file types."
end
end
end
end