Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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

  • Loading branch information...
commit 701abb01d20921c7f60f19b2dd7015c37bbe87fc 1 parent 84f0d61
@jyurek jyurek authored
Showing with 66 additions and 10 deletions.
  1. +30 −10 lib/paperclip/attachment.rb
  2. +36 −0 test/attachment_test.rb
View
40 lib/paperclip/attachment.rb
@@ -36,6 +36,7 @@ def initialize name, instance, options = {}
@storage = options[:storage]
@whiny = options[:whiny_thumbnails]
@convert_options = options[:convert_options] || {}
+ @background = options[:background].nil? ? instance.respond_to?(:spawn) : options[:background]
@processors = options[:processors] || [:thumbnail]
@options = options
@queued_for_delete = []
@@ -254,15 +255,15 @@ def instance_read(attr)
private
- def logger
+ def logger #:nodoc:
instance.logger
end
- def log message
+ def log message #:nodoc:
logger.info("[paperclip] #{message}") if logging?
end
- def logging?
+ def logging? #:nodoc:
Paperclip.options[:log]
end
@@ -283,7 +284,7 @@ def validate #:nodoc:
@validation_errors
end
- def normalize_style_definition
+ def normalize_style_definition #:nodoc:
@styles.each do |name, args|
unless args.is_a? Hash
dimensions, format = [args, nil].flatten[0..1]
@@ -305,7 +306,7 @@ def normalize_style_definition
end
end
- def initialize_storage
+ def initialize_storage #:nodoc:
@storage_module = Paperclip::Storage.const_get(@storage.to_s.capitalize)
self.extend(@storage_module)
end
@@ -321,8 +322,19 @@ def extra_options_for(style) #:nodoc:
def post_process #:nodoc:
return if @queued_for_write[:original].nil?
- return if callback(:before_post_process) == false
- return if callback(:"before_#{name}_post_process") == false
+ background do
+ 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}")
@styles.each do |name, args|
begin
@@ -336,11 +348,19 @@ def post_process #:nodoc:
(@errors[:processing] ||= []) << e.message if @whiny
end
end
- callback(:"after_#{name}_post_process")
- callback(:after_post_process)
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 }
end
View
36 test/attachment_test.rb
@@ -210,6 +210,42 @@ class Paperclip::Test < Paperclip::Processor; 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
setup do
rebuild_model :processors => [], :styles => {:something => 1}
Please sign in to comment.
Something went wrong with that request. Please try again.