lib/paperclip.rb in paperclip-2.3.16 vs lib/paperclip.rb in paperclip-2.4.0
- old
+ new
@@ -37,11 +37,13 @@
require 'paperclip/interpolations'
require 'paperclip/style'
require 'paperclip/attachment'
require 'paperclip/storage'
require 'paperclip/callback_compatibility'
+require 'paperclip/missing_attachment_styles'
require 'paperclip/railtie'
+require 'logger'
require 'cocaine'
# The base module that gets included in ActiveRecord::Base. See the
# documentation for Paperclip::ClassMethods for more useful information.
module Paperclip
@@ -89,34 +91,53 @@
# Paperclip.run("echo", "something", :expected_outcodes => [0,1,2,3])
#
# 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
+ def run(cmd, *params)
if options[:image_magick_path]
Paperclip.log("[DEPRECATION] :image_magick_path is deprecated and will be removed. Use :command_path instead")
end
Cocaine::CommandLine.path = options[:command_path] || options[:image_magick_path]
Cocaine::CommandLine.new(cmd, *params).run
end
- def processor name #:nodoc:
- name = name.to_s.camelize
- load_processor(name) unless Paperclip.const_defined?(name)
- processor = Paperclip.const_get(name)
- unless processor.ancestors.include?(Paperclip::Processor)
- raise PaperclipError.new("Processor #{name} was not found")
+ def processor(name) #:nodoc:
+ @known_processors ||= {}
+ if @known_processors[name.to_s]
+ @known_processors[name.to_s]
+ else
+ name = name.to_s.camelize
+ load_processor(name) unless Paperclip.const_defined?(name)
+ processor = Paperclip.const_get(name)
+ @known_processors[name.to_s] = processor
end
- processor
end
def load_processor(name)
if defined?(Rails.root) && Rails.root
require File.expand_path(Rails.root.join("lib", "paperclip_processors", "#{name.underscore}.rb"))
end
end
+ def clear_processors!
+ @known_processors.try(:clear)
+ end
+
+ # You can add your own processor via the Paperclip configuration. Normally
+ # Paperclip will load all processors from the
+ # Rails.root/lib/paperclip_processors directory, but here you can add any
+ # existing class using this mechanism.
+ #
+ # Paperclip.configure do |c|
+ # c.register_processor :watermarker, WatermarkingProcessor.new
+ # end
+ def register_processor(name, processor)
+ @known_processors ||= {}
+ @known_processors[name.to_s] = processor
+ end
+
def each_instance_with_attachment(klass, name)
class_for(klass).all.each do |instance|
yield(instance) if instance.send(:"#{name}?")
end
end
@@ -126,13 +147,17 @@
def log message
logger.info("[paperclip] #{message}") if logging?
end
def logger #:nodoc:
- defined?(ActiveRecord::Base) ? ActiveRecord::Base.logger : Rails.logger
+ @logger ||= options[:logger] || Logger.new(STDOUT)
end
+ def logger=(logger)
+ @logger = logger
+ end
+
def logging? #:nodoc:
options[:log]
end
def class_for(class_name)
@@ -262,13 +287,15 @@
write_inheritable_attribute(:attachment_definitions, {})
end
end
attachment_definitions[name] = {:validations => []}.merge(options)
+ Paperclip.classes_with_attachments << self
after_save :save_attached_files
- before_destroy :destroy_attached_files
+ before_destroy :prepare_for_destroy
+ after_destroy :destroy_attached_files
define_paperclip_callbacks :post_process, :"#{name}_post_process"
define_method name do |*args|
a = attachment_for(name)
@@ -326,11 +353,11 @@
# Options:
# * +if+: A lambda or name of a method on the instance. Validation will only
# 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"
+ message = options[:message] || :empty
validates_presence_of :"#{name}_file_name",
:message => message,
:if => options[:if],
:unless => options[:unless]
end
@@ -400,12 +427,19 @@
end
def destroy_attached_files
Paperclip.log("Deleting attachments.")
each_attachment do |name, attachment|
- attachment.send(:queue_existing_for_delete)
attachment.send(:flush_deletes)
end
end
+
+ def prepare_for_destroy
+ Paperclip.log("Scheduling attachments for deletion.")
+ each_attachment do |name, attachment|
+ attachment.send(:queue_existing_for_delete)
+ end
+ end
+
end
end