Skip to content
This repository has been archived by the owner on Oct 5, 2018. It is now read-only.

added support for callbacks from gogiel/delayed_paperclip #196

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/delayed_paperclip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ def options
:background_job_class => DelayedPaperclip::ProcessJob,
:url_with_processing => true,
:processing_image_url => nil,
:queue => "paperclip"
:queue => "paperclip",
:post_processing_callback => nil,
:pre_processing_callback => nil,
:post_update_callback => nil
}
end

Expand Down Expand Up @@ -54,7 +57,10 @@ def process_in_background(name, options = {})
:only_process => only_process_default,
:url_with_processing => DelayedPaperclip.options[:url_with_processing],
:processing_image_url => DelayedPaperclip.options[:processing_image_url],
:queue => DelayedPaperclip.options[:queue]
:queue => DelayedPaperclip.options[:queue],
:post_processing_callback => nil,
:pre_processing_callback => nil,
:post_update_callback => nil
}.each do |option, default|
paperclip_definitions[name][:delayed][option] = options.key?(option) ? options[option] : default
end
Expand Down
22 changes: 22 additions & 0 deletions lib/delayed_paperclip/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,14 @@ def delayed_only_process
end

def process_delayed!
run_callback :pre_processing_callback
self.job_is_processing = true
self.post_processing = true
reprocess!(*delayed_only_process)
run_callback :post_processing_callback
self.job_is_processing = false
update_processing_column
run_callback :post_update_callback
end

def processing_image_url
Expand Down Expand Up @@ -85,5 +88,24 @@ def update_processing_column
end
end

def run_callback(callback_name)
execute_callback delayed_options[callback_name]
end

def execute_callback(callback)
case callback
when Proc
callback.call self
when Symbol, String
method = instance.method callback
arity = method.arity
if arity == 1
method.call self
else
method.call
end
end
end

end
end
5 changes: 4 additions & 1 deletion spec/delayed_paperclip/attachment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
:only_process => [],
:url_with_processing => true,
:processing_image_url => nil,
:queue => "paperclip"
:queue => "paperclip",
:post_processing_callback => nil,
:post_update_callback => nil,
:pre_processing_callback => nil
})
end
end
Expand Down
20 changes: 16 additions & 4 deletions spec/delayed_paperclip/class_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
:only_process => [],
:url_with_processing => true,
:processing_image_url => nil,
:queue => "paperclip"}
:queue => "paperclip",
:post_processing_callback => nil,
:post_update_callback => nil,
:pre_processing_callback => nil }
}
}
end
Expand All @@ -31,7 +34,10 @@
:only_process => [],
:url_with_processing => true,
:processing_image_url => nil,
:queue => "custom"}
:queue => "custom",
:post_processing_callback => nil,
:post_update_callback => nil,
:pre_processing_callback => nil }
}
})
end
Expand All @@ -48,7 +54,10 @@
:only_process => [],
:url_with_processing => true,
:processing_image_url => "/processing/url",
:queue => "paperclip"}
:queue => "paperclip",
:post_processing_callback => nil,
:post_update_callback => nil,
:pre_processing_callback => nil }
}
}
end
Expand All @@ -68,7 +77,10 @@
:only_process => [:small, :large],
:url_with_processing => true,
:processing_image_url => nil,
:queue => "paperclip"}
:queue => "paperclip",
:post_processing_callback => nil,
:post_update_callback => nil,
:pre_processing_callback => nil }
}
}
end
Expand Down
12 changes: 9 additions & 3 deletions spec/delayed_paperclip_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
DelayedPaperclip.options.should == {:background_job_class => DelayedPaperclip::ProcessJob,
:url_with_processing => true,
:processing_image_url => nil,
:queue => "paperclip"}
:queue => "paperclip",
:post_processing_callback => nil,
:post_update_callback => nil,
:pre_processing_callback => nil }
end
end

Expand Down Expand Up @@ -58,9 +61,12 @@
:only_process => [],
:url_with_processing => true,
:processing_image_url => nil,
:queue => "paperclip"}
:queue => "paperclip",
:post_processing_callback => nil,
:post_update_callback => nil,
:pre_processing_callback => nil }
}
})
end
end
end
end