Skip to content

Commit

Permalink
If spawn is installed, paperclip will take advantage of it.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Yurek committed Jan 4, 2009
1 parent 84f0d61 commit 701abb0
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 10 deletions.
40 changes: 30 additions & 10 deletions lib/paperclip/attachment.rb
Expand Up @@ -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 = []
Expand Down Expand Up @@ -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

Expand All @@ -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]
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down
36 changes: 36 additions & 0 deletions test/attachment_test.rb
Expand Up @@ -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}
Expand Down

0 comments on commit 701abb0

Please sign in to comment.