Commit 701abb01 by Jon Yurek

If spawn is installed, paperclip will take advantage of it.

parent 84f0d614
...@@ -36,6 +36,7 @@ module Paperclip ...@@ -36,6 +36,7 @@ module Paperclip
@storage = options[:storage] @storage = options[:storage]
@whiny = options[:whiny_thumbnails] @whiny = options[:whiny_thumbnails]
@convert_options = options[:convert_options] || {} @convert_options = options[:convert_options] || {}
@background = options[:background].nil? ? instance.respond_to?(:spawn) : options[:background]
@processors = options[:processors] || [:thumbnail] @processors = options[:processors] || [:thumbnail]
@options = options @options = options
@queued_for_delete = [] @queued_for_delete = []
...@@ -254,15 +255,15 @@ module Paperclip ...@@ -254,15 +255,15 @@ module Paperclip
private private
def logger def logger #:nodoc:
instance.logger instance.logger
end end
def log message def log message #:nodoc:
logger.info("[paperclip] #{message}") if logging? logger.info("[paperclip] #{message}") if logging?
end end
def logging? def logging? #:nodoc:
Paperclip.options[:log] Paperclip.options[:log]
end end
...@@ -283,7 +284,7 @@ module Paperclip ...@@ -283,7 +284,7 @@ module Paperclip
@validation_errors @validation_errors
end end
def normalize_style_definition def normalize_style_definition #:nodoc:
@styles.each do |name, args| @styles.each do |name, args|
unless args.is_a? Hash unless args.is_a? Hash
dimensions, format = [args, nil].flatten[0..1] dimensions, format = [args, nil].flatten[0..1]
...@@ -305,7 +306,7 @@ module Paperclip ...@@ -305,7 +306,7 @@ module Paperclip
end end
end end
def initialize_storage def initialize_storage #:nodoc:
@storage_module = Paperclip::Storage.const_get(@storage.to_s.capitalize) @storage_module = Paperclip::Storage.const_get(@storage.to_s.capitalize)
self.extend(@storage_module) self.extend(@storage_module)
end end
...@@ -321,8 +322,19 @@ module Paperclip ...@@ -321,8 +322,19 @@ module Paperclip
def post_process #:nodoc: def post_process #:nodoc:
return if @queued_for_write[:original].nil? return if @queued_for_write[:original].nil?
return if callback(:before_post_process) == false background do
return if callback(:"before_#{name}_post_process") == false return if fire_events(:before)
post_process_styles
return if fire_events(:after)
end
end
def fire_events(which)
return true if callback(:"#{which}_post_process") == false
return true if callback(:"#{which}_#{name}_post_process") == false
end
def post_process_styles
log("Post-processing #{name}") log("Post-processing #{name}")
@styles.each do |name, args| @styles.each do |name, args|
begin begin
...@@ -336,11 +348,19 @@ module Paperclip ...@@ -336,11 +348,19 @@ module Paperclip
(@errors[:processing] ||= []) << e.message if @whiny (@errors[:processing] ||= []) << e.message if @whiny
end end
end end
callback(:"after_#{name}_post_process")
callback(:after_post_process)
end end
def callback which # When processing, if the spawn plugin is installed, processing can be done in
# a background fork or thread if desired.
def background(&blk)
if instance.respond_to?(:spawn) && @background
instance.spawn(&blk)
else
blk.call
end
end
def callback which #:nodoc:
instance.run_callbacks(which, @queued_for_write){|result, obj| result == false } instance.run_callbacks(which, @queued_for_write){|result, obj| result == false }
end end
......
...@@ -210,6 +210,42 @@ class AttachmentTest < Test::Unit::TestCase ...@@ -210,6 +210,42 @@ class AttachmentTest < Test::Unit::TestCase
end end
end end
context "When spawn is not defined on the instance" do
setup do
rebuild_model :styles => {:foo => true}
@dummy = Dummy.new
@file = StringIO.new("12345")
end
should "not call spawn on the instance when assigned a file" do
@dummy.expects(:spawn).times(0)
@dummy.avatar.expects(:post_process_styles)
# This is pretty ugly, but mocha expectations make the object
# respond_to? things it wouldn't have. Gotta get around it.
class << @dummy
def respond_to_with_spawn?(method)
(method == :spawn) ? false : respond_to_without_spawn?(method)
end
alias_method_chain :respond_to?, :spawn
end
@dummy.avatar = @file
end
end
context "When spawn is defined on the instance" do
setup do
Dummy.any_instance.stubs(:spawn)
rebuild_model :styles => {:foo => true}
@dummy = Dummy.new
@file = StringIO.new("12345")
end
should "not call spawn on the instance when assigned a file" do
@dummy.expects(:spawn)
@dummy.avatar = @file
end
end
context "An attachment with no processors defined" do context "An attachment with no processors defined" do
setup do setup do
rebuild_model :processors => [], :styles => {:something => 1} rebuild_model :processors => [], :styles => {:something => 1}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment